Maximum width for column in bar chart

后端 未结 6 1057
天命终不由人
天命终不由人 2021-02-14 09:35

I\'m looking for a way of limiting the column width in a chart, I\'m sure this ought to be relatively easy but I cant find a way of doing it.

I\'m populating a chart fro

6条回答
  •  误落风尘
    2021-02-14 09:56

    Thought I'd share that I came up with a slightly different answer to this. I didn't want to hard code in a maximum bar width because 1) it wasn't responsive to different screen sizes and 2) it also required playing with the x-coordinate attribute or accepting some irregular spacing.

    Instead, I just set a minimum number of bars, based on the point where the bars became too wide (in my case, I found that less than 12 bars made my chart look weird). I then adjusted the scaleBand's range attribute, proportionately, if there were less than that number of bars. So, for example, if the minimum was set to 12 and there were only 5 items in the data, rather than rendering each of them at 1/5th of the full width, I scaled the range down to 5/12ths of the original width.

    Basically, something like this:

    // x is a scaleBand() that was previously defined, and this would run on update
    
    var minBarSlots = 12;
    
    if (data.length < minBarSlots) {
        x.range([0, width*(data.length/minBarSlots)])
    }
    else {
        x.range([0, width])
    }`
    

提交回复
热议问题