[英]Using chart.js, how do I align bars instead of centering them when there are only a few bars to display?
As some background: I have a horizontal bar chart with a dynamic number of bars.作为一些背景:我有一个带有动态条数的水平条形图。 I have set min and max bar widths for each of the datasets.我为每个数据集设置了最小和最大条形宽度。
By default, chart.js aligns the bars in the center of chart.默认情况下,chart.js 将条形图居中对齐。 Instead I would like to align the bars to the left (or in the top as I am using a horizontal bar chart).相反,我想将条形图向左对齐(或在顶部对齐,因为我使用的是水平条形图)。
Thinking of the bars as HTML elements, this would be the equivalent of applying the css: float:left
to each bar.将条形视为 HTML 个元素,这相当于将 css: float:left
应用于每个条形。 Is there anyway to do this in chart.js?在 chart.js 中有没有办法做到这一点? I still want the axes to extend to the end of the container, it just looks funny to have only a couple bars with a ton of space in between them (I also don't want really fat bars).我仍然希望轴延伸到容器的末端,看起来很有趣,只有几个条,它们之间有大量空间(我也不想要非常粗的条)。
Thanks for any help!谢谢你的帮助!
Note: This is a duplicate of 56361398注意:这是56361398的副本
EDIT : See codepen for example (one bar is centered in the chart, I would like it aligned to the left, next to the y axis).编辑:例如,参见codepen (一个条在图表中居中,我希望它向左对齐,在 y 轴旁边)。
I have checked the settings and there is no setting to left-align the bar charts.我检查了设置,没有设置让条形图左对齐。 I doubt it is easy to implement since Chart.JS is a canvas
under the hood.我怀疑它是否容易实现,因为 Chart.JS 是一个canvas
引擎盖下。
Also, technically speaking the chart is in fact "left-aligned" however it is just one slim bar.此外,从技术上讲,该图表实际上是“左对齐”的,但它只是一个细条。 The bar could very well use the whole bar space in red:酒吧可以很好地使用红色的整个酒吧空间:
The best workaround is to create empty labels
that will later be populated if needed.最好的解决方法是创建空labels
,稍后将在需要时填充这些标签。 If you need to add more bars just check if a label name is ""
(empty) and then overwrite it.如果您需要添加更多条,只需检查 label 名称是否为""
(空),然后覆盖它。 Otherwise push
the new label.否则push
新的 label。
Example: https://codepen.io/adelriosantiago/pen/vYLYXPx?editors=0110示例: https://codepen.io/adelriosantiago/pen/vYLYXPx?editors=0110
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.