Line chart different background colors
Hello,
I am looking for a way to achieve this using Chart.js :
My concern is having 2 different background color. I don't know how to do this.
I managed to do everything else by creating new Scales (x is a TimeScale, y is a LinearScale).
Here is what I got today :
As you can see, the only difference is having a background, and line color dynamic depends on the value in dataset.
Do you have any advice to do this ?
btw, I'm using angular-chart.js currently in alpha with chart.js on beta-2. plan to migrate to last release soon when angular-chart will be stable.
Thanks
I don't think is currently possible. It would require an updated Chart.element.Line.prototype.draw method that drew different backgrounds
You could use 2 different datasets. One that contains data for red background and the other for blue background.
Hello, Thanks for the tips, I ended up using, indeed, 2 different datasets. Only problem was: What to put on the second dataset when there is data on the first one ? Best is to put "null".
Also, on transition point, data must be on both datasets or there will be a break in the graph
Working solution:
Closing since I don't think we're going to change the drawing methods to support this since it hasn't been heavily requested and there are good work arounds
Options are
- use two datasets
- set the fill as a CanvasGradient object
- change
Chart.elements.Line.prototype.drawto include this
what if we want to achieve different colors for positive and negative values? we can only override the draw?
Do you have a jsfiddle/codepen link to the working solution?
1+, is there a link to an example how to do that?
+1, any tips for doing this ?
any example for coloring using gradient ?
https://github.com/chartjs/Chart.js/issues/4895
there is an example
https://jsfiddle.net/egamegadrive16/zjdwr4fh/
#4895
there is an example
https://jsfiddle.net/egamegadrive16/zjdwr4fh/
Anyway this works with the fill color? I got different dashed lines working, but not the fill sadly
https://jsfiddle.net/haeydc7m/2/
Just in case someone comes here like me....
Latest versions of ChartJS allow you to customize line segments individually, which can be used to change the color and also the style (dashed etc) of a specific segment.
const config = {
type: 'line',
data: {
labels: Utils.months({count: 7}),
datasets: [{
label: 'My First Dataset',
data: [65, 59, NaN, 48, 56, 57, 40],
borderColor: 'rgb(75, 192, 192)',
segment: {
borderColor: ctx => skipped(ctx, 'rgb(0,0,0,0.2)') || down(ctx, 'rgb(192,75,75)'),
borderDash: ctx => skipped(ctx, [6, 6]),
}
}]
},
options: genericOptions
};
see segments for more info

No need for a workaround. @Hedva I'm sure you won't need it anymore, but your example would then look like https://jsfiddle.net/dsmcx94y/