How to prevent empty bars from taking up width in Chart.js bar chart

题意:"如何防止空条在 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 似乎仍然想为它们分配空间:"

Chart Example

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` 来防止它们变大。"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值