Chart.js-具有线性笛卡尔(数字)X轴的条形图? [英] Chart.js - Bar chart with linear cartesian (numerical) X axis?
问题描述
我正在使用Chart.JS,我想创建一个X轴为线性的条形图.做到这一点的文档很少,而且我还无法获得一套有效的配置.
I'm using Chart.JS and I'd like to create a bar chart where the X axis is linear. The documentation on doing this is scant, and I haven't been able to get a working set of configs.
例如,假设我有以下几对:(1,1),(2,2),(3,3),(5,1),(6,5),(7,1)
.我想绘制一个条形图,其中x值1的高度为1,x值6的高度为5,依此类推.但是,我希望对数中不包含的数字4仍在x轴上显示为0.
So for example, let's say I have the following pairs: (1, 1), (2, 2), (3, 3), (5, 1), (6, 5), (7, 1)
. I want to plot a bar chart where x value 1 has a bar of height 1, x value 6 has a bar of height 5, and so on. But, I want the number 4, which isn't included in my set of pairs, to still be visible on the x axis with value 0.
这是内置功能可以实现的吗?我目前的备份方法是手动将缺失的刻度线手动插入为y值为0的类别,但是如果可以选择的话,那就太好了.
Is this something that'spossible with built-in functionality? My current backup is to just manually insert missing ticks as categories with y-value 0 but it'd be nice if there was just an option for it.
推荐答案
uminder的答案是正确的,但是使用"seconds"的问题却很严重.因为时间单位是您不能在X轴上设置大于59的值.如果这样做,该图表将显示为空白.
uminder's answer is right, but the problem of using "seconds" as the time unit is that you cannot set values greater than 59 on the X axis. If you do, the chart will appear blank.
只需更改时间数据以使用年份,就可以设置所需的任何值.
By just changing the time data to use years you will be able to set any value you want.
time: {
parser: 'Y',
unit: 'year',
displayFormats: {
year: 'Y'
}
}
这篇关于Chart.js-具有线性笛卡尔(数字)X轴的条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!