admin管理员组文章数量:1023195
I'm having trouble getting consistent bar widths in Flot. I have charts that can have anywhere between 1 and 10 data points. When there are 10 data points, the bars are the right size. However when there are less than 10 data points, the bars in the chart start getting fatter. The following two images demonstrate this.
The bars are decently sized when there are 10 items.
The bar bees incredibly large when there is only 1 item.
I am aware of the barWidth
property and I've tried changing it. The problem is that it only affects the size of the bars in the chart with 10 items but it leaves the chart with the fat bar alone.
I have also read somewhere that I could try changing the height of the div
containing the chart, but I would like to avoid this as I want the charts to have a fixed dimension regardless of items displayed.
I'm having trouble getting consistent bar widths in Flot. I have charts that can have anywhere between 1 and 10 data points. When there are 10 data points, the bars are the right size. However when there are less than 10 data points, the bars in the chart start getting fatter. The following two images demonstrate this.
The bars are decently sized when there are 10 items.
The bar bees incredibly large when there is only 1 item.
I am aware of the barWidth
property and I've tried changing it. The problem is that it only affects the size of the bars in the chart with 10 items but it leaves the chart with the fat bar alone.
I have also read somewhere that I could try changing the height of the div
containing the chart, but I would like to avoid this as I want the charts to have a fixed dimension regardless of items displayed.
- 1 I do not know flot but a possible quick and dirty workaround is : making 9 more invisible(border and bg color = chart bg If possible) bars and adding them. – Volkan Commented Mar 26, 2013 at 10:05
- That is very quick and dirty :) Thanks for the suggestion and I think that's the route I'll go down if there isn't a more elegant solution. – CadentOrange Commented Mar 26, 2013 at 10:32
2 Answers
Reset to default 6The problem is that barWidth is expressed in axis units. As you reduce the number of bars the y axis automatically scales, until with only one bar it's going from 0 to 1, and the default bar width of 0.8 takes up most of the vertical space.
You can solve this in two ways:
Calculate the barWidth to scale with the number of series, i.e. 0.8 * numSeries / maxSeries. If maxSeries is 10, then the barWidth scales from 0.8 for 10 series to 0.08 for one series.
Set a fixed min & max on the y axis, with max being numSeries + (maxSeries - numSeries) / 2 and min being -(maxSeries - numSeries) / 2. This reserves space above and below the bars even as their number drops below maxSeries.
Both of these require that you have some idea of the maximum number of series that will be present.
I think you must pass somewhere labels to your yaxis (ticks property). Assume that your labels are in myLabels array. So if there is only 1 label, add new empty label before and after it to make your only bar smaller:
var myLabels = [];
// add a fake label (in my production code I grab labels from the server)
myLabels.push([0, 'My only label']); // This label has index=0
if (myLabels.length == 1) {
myLabels.splice(0, 0, [-1, '']); // (-1) is the new index lower than my label's index
myLabels.push([1, '']); // (1) is the new index greater than my label's index
}
// In configuration of a plot:
// [...]
yaxis: {
ticks: myLabels,
// [...]
},
I'm having trouble getting consistent bar widths in Flot. I have charts that can have anywhere between 1 and 10 data points. When there are 10 data points, the bars are the right size. However when there are less than 10 data points, the bars in the chart start getting fatter. The following two images demonstrate this.
The bars are decently sized when there are 10 items.
The bar bees incredibly large when there is only 1 item.
I am aware of the barWidth
property and I've tried changing it. The problem is that it only affects the size of the bars in the chart with 10 items but it leaves the chart with the fat bar alone.
I have also read somewhere that I could try changing the height of the div
containing the chart, but I would like to avoid this as I want the charts to have a fixed dimension regardless of items displayed.
I'm having trouble getting consistent bar widths in Flot. I have charts that can have anywhere between 1 and 10 data points. When there are 10 data points, the bars are the right size. However when there are less than 10 data points, the bars in the chart start getting fatter. The following two images demonstrate this.
The bars are decently sized when there are 10 items.
The bar bees incredibly large when there is only 1 item.
I am aware of the barWidth
property and I've tried changing it. The problem is that it only affects the size of the bars in the chart with 10 items but it leaves the chart with the fat bar alone.
I have also read somewhere that I could try changing the height of the div
containing the chart, but I would like to avoid this as I want the charts to have a fixed dimension regardless of items displayed.
- 1 I do not know flot but a possible quick and dirty workaround is : making 9 more invisible(border and bg color = chart bg If possible) bars and adding them. – Volkan Commented Mar 26, 2013 at 10:05
- That is very quick and dirty :) Thanks for the suggestion and I think that's the route I'll go down if there isn't a more elegant solution. – CadentOrange Commented Mar 26, 2013 at 10:32
2 Answers
Reset to default 6The problem is that barWidth is expressed in axis units. As you reduce the number of bars the y axis automatically scales, until with only one bar it's going from 0 to 1, and the default bar width of 0.8 takes up most of the vertical space.
You can solve this in two ways:
Calculate the barWidth to scale with the number of series, i.e. 0.8 * numSeries / maxSeries. If maxSeries is 10, then the barWidth scales from 0.8 for 10 series to 0.08 for one series.
Set a fixed min & max on the y axis, with max being numSeries + (maxSeries - numSeries) / 2 and min being -(maxSeries - numSeries) / 2. This reserves space above and below the bars even as their number drops below maxSeries.
Both of these require that you have some idea of the maximum number of series that will be present.
I think you must pass somewhere labels to your yaxis (ticks property). Assume that your labels are in myLabels array. So if there is only 1 label, add new empty label before and after it to make your only bar smaller:
var myLabels = [];
// add a fake label (in my production code I grab labels from the server)
myLabels.push([0, 'My only label']); // This label has index=0
if (myLabels.length == 1) {
myLabels.splice(0, 0, [-1, '']); // (-1) is the new index lower than my label's index
myLabels.push([1, '']); // (1) is the new index greater than my label's index
}
// In configuration of a plot:
// [...]
yaxis: {
ticks: myLabels,
// [...]
},
本文标签: javascriptConsistent bar widths in flot chartsStack Overflow
版权声明:本文标题:javascript - Consistent bar widths in flot charts - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745576986a2157079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论