题意:"如何防止空条在 Chart.js 条形图中占用宽度"
问题背景:
I'm building a bar chart in Chart.js. I have multiple data series, but often, not all of them will have a value. When this happens, even though the bars aren't there, Chart.js seems to want to allocate space for them:
"我正在使用 Chart.js 构建一个条形图。我有多个数据系列,但通常并不是所有的数据系列都有值。当这种情况发生时,尽管条形图没有显示这些条,Chart.js 似乎仍然想为它们分配空间:"
Notice how the green and blue bars above are always left aligned relative to the labels, and how the pink and yellow are always on the right. How do I make the bars appear centered relative to their labels?
"注意上面的绿色和蓝色条总是相对于标签左对齐,而粉色和黄色条总是位于右侧。我该如何让这些条相对于它们的标签居中显示呢?"
问题解决:
@Drarig29's answer is correct -- null
values plus skipNull
removes the extra bars. However, if you're using barPercentage
, your bars will fluctuate in width (varies from being a 1/5th of the width to 100% of the width, in my case, depending on how many bars there are), so you must set a maxBarThickness
on the dataset to prevent them from growing.
"@Drarig29 的回答是正确的——`null` 值加上 `skipNull` 可以去除多余的条形。然而,如果你使用了 `barPercentage`,你的条形宽度会波动(在我的例子中,宽度从 1/5 到 100% 不等,取决于有多少条形),因此你必须在数据集中设置 `maxBarThickness` 来防止它们变大。"