您首先需要知道,您可以编辑的有关图表的所有信息都存储在包含图表的变量中(myChart
通常称为,但是my2Bar
在您的小提琴中)。
If you want to globally change the graph, you will need to edit attributes in myChart.config.options
.
If you want to change a specific chart, you will need to edit attributes in myChart.config.data
.
In this case, you need to change a specific chart (which is the horizontal bar).
config
, you will finally see that bars in your chart are drawn using attributes stored in myChart.config.data.datasets[0]._meta[0].data[
n
]._model
( n
being the nth rectangle drawn, top to bottom).
Some attributes you can find there :
base
: The X position where the rectangle is starting to be drawn (0 in your xAxe for instance).x
: The rectangle is being drawn until this X position.height
: The height of the drawn rectangle.To edit these values, you just need to loop in your different rectangles (the n
in the above path).
But you just can't do it manually on the config
of your variable. If you do this, it won't work since your chart is responsive (on resize, it will redraw the chart using the former options).
Plugins let you handle all the events that are triggered while creating, updating, rendering your graph.
然后,在您的beforeRender
事件中(在初始化之后但在绘图之前触发),您需要在不同的矩形中循环以编辑值以影响它们的绘制方式:
beforeRender: function(chart) {
for (var i = 0; i < chart.config.data.datasets[0]._meta[0].data.length; i++) {
// Change both `3` values to change the height & width of the point
chart.config.data.datasets[0]._meta[0].data[i]._model["base"] = chart.config.data.datasets[0]._meta[0].data[i]._model["x"] + 3;
chart.config.data.datasets[0]._meta[0].data[i]._model["height"] = 3;
}
}
这是最终结果的jsFiddle。
不幸的是,我无法制作圆点,而不是方形点。
我还制作了另一个jsFiddle,其中所有点都链接在一起,这使其看起来像是水平折线图(当然可以改进,但这是一个好的开始)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句