chart.js Line chart with different background colors for each section

后端 未结 4 1021
天涯浪人
天涯浪人 2020-12-09 22:41

Lets say I have a Line chart with mon-fri for 4 weeks. I want that these 4 weeks are diveded in sections. I want the first monday to friday have a white background color. Th

4条回答
  •  情深已故
    2020-12-09 23:21

    Some people here have requested something that works for later versions, here's my hacked together solution that works on ChartJS 2.7.2 (EDIT: Apr 2020: Also 2.9.3) and could probably be adapted. Chart.types.Line.extend used in the answer above, doesn't seem to be valid in v2.

    graph with background relvant to x

    I managed to figure this out with help from this thread to get the plugin code, and also found this thread useful for gathering co-ordinates of the data points.

    With some work this fiddle should allow you to pass the label array keys as start/stop positions via the following code (where 0 and 1 are the keys):

    var start = meta.data[0]._model.x;
    var stop  = meta.data[1]._model.x;
    

    You could loop this, along with the ctx.fillRect function to draw multiple rectangles.

    Here's the working fiddle: http://jsfiddle.net/oe2606ww/436/

提交回复
热议问题