Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different color for each bar in a bar chart; ChartJS

I'm using ChartJS in a project I'm working on and I need a different color for each bar in a Bar Chart.

Here's an example of the bar chart data set:

var barChartData = {   labels: ["001", "002", "003", "004", "005", "006", "007"],   datasets: [{     label: "My First dataset",     fillColor: "rgba(220,220,220,0.5)",      strokeColor: "rgba(220,220,220,0.8)",      highlightFill: "rgba(220,220,220,0.75)",     highlightStroke: "rgba(220,220,220,1)",     data: [20, 59, 80, 81, 56, 55, 40]   }] }; 

Is there any way to paint each bar differently?

like image 215
BoooYaKa Avatar asked Aug 31 '14 17:08

BoooYaKa


People also ask

How do you change the color of each column in a bar chart?

In a chart, click to select the data series for which you want to change the colors. On the Format tab, in the Current Selection group, click Format Selection. tab, expand Fill, and then do one of the following: To vary the colors of data markers in a single-series chart, select the Vary colors by point check box.

How do I change the color of individual bars in numbers?

Click on a bar in the graph. A 'style' window will appear on the right of the screen. From the style window you can change the colour of the individual bar.

Should a bar graph have different colors?

In a bar graph, make your largest quantity the brightest color with each subsequent category being less and less saturated. Choices like these can really draw attention to where you want it to be. Stick to a few colors and use contrast to highlight important information. Don't use more than six colors together, though.


1 Answers

As of v2, you can simply specify an array of values to correspond to a color for each bar via the backgroundColor property:

datasets: [{   label: "My First dataset",   data: [20, 59, 80, 81, 56, 55, 40],   backgroundColor: ["red", "blue", "green", "blue", "red", "blue"],  }], 

This is also possible for the borderColor, hoverBackgroundColor, hoverBorderColor.

From the documentation on the Bar Chart Dataset Properties:

Some properties can be specified as an array. If these are set to an array value, the first value applies to the first bar, the second value to the second bar, and so on.

like image 128
thanksd Avatar answered Oct 12 '22 23:10

thanksd