Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Line chart different background colors

Open peterzebird opened this issue 10 years ago • 12 comments

Hello,

I am looking for a way to achieve this using Chart.js : helpchartjscolor

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 : helpchartjsnocolor

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

peterzebird avatar May 02 '16 10:05 peterzebird

I don't think is currently possible. It would require an updated Chart.element.Line.prototype.draw method that drew different backgrounds

etimberg avatar May 04 '16 01:05 etimberg

You could use 2 different datasets. One that contains data for red background and the other for blue background.

milesich avatar May 07 '16 23:05 milesich

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 averagegraphbugnull

Working solution: averagegraphfixed

peterzebird avatar Jun 23 '16 13:06 peterzebird

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

  1. use two datasets
  2. set the fill as a CanvasGradient object
  3. change Chart.elements.Line.prototype.draw to include this

etimberg avatar Sep 17 '16 01:09 etimberg

what if we want to achieve different colors for positive and negative values? we can only override the draw?

CodePlayer7 avatar May 19 '17 05:05 CodePlayer7

Do you have a jsfiddle/codepen link to the working solution?

nehasuvarna avatar Jun 20 '17 19:06 nehasuvarna

1+, is there a link to an example how to do that?

kerner1000 avatar Nov 06 '17 16:11 kerner1000

+1, any tips for doing this ?

agaricus-campestris avatar Nov 14 '17 16:11 agaricus-campestris

any example for coloring using gradient ?

ben2323 avatar Jan 04 '18 23:01 ben2323

https://github.com/chartjs/Chart.js/issues/4895

there is an example

https://jsfiddle.net/egamegadrive16/zjdwr4fh/

virusdefender avatar Sep 29 '18 05:09 virusdefender

#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/

Hedva avatar Jun 22 '20 14:06 Hedva

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 image

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/

Anubarak avatar Jul 13 '21 14:07 Anubarak