question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bar chart color depending on the value.

See original GitHub issue

Hello,

I’m doing a bar chart and I’d like that the bar with positive values had green color and red the negative values. I guess there must be a not so complicated solution for this because it’s a very logical request, but I can’t find it.

`var myChart = new Chart(ctx, { type: ‘bar’, data: { labels: info.dates, datasets: [{ label: info.label + ’ ’ + info.unit, data: info.numbers, backgroundColor: ‘green’, borderWidth: 0, fill: true, pointRadius:0, pointHitRadius: 10
}] }, options: { scales: {

    		xAxes:[{
    			ticks:{
    				
    			},
    			gridLines:{
    				tickMarkLength: 10,
    				drawBorder: false,
    				display: false,
    			} 
    		}],
        	yAxes:[{
        		gridLines:{
            		
        			drawBorder: false,
        			display: false,
        		},
            	ticks: {
            		beginAtZero:true, 
            	}
        	}]
   	 	}
	}
});

`

Thanks a lot.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
kluplaucommented, Jan 22, 2019

The solution from @BVazquezAlvarez doesn’t tell you what the green means, because there is only a single dataset, and therefore a single legend.

You have to do it with two datasets, filter the data based on how the coloring should work, and then stack it like this: https://jsfiddle.net/Blueblau/wqs4c60o/

3reactions
BVazquezAlvarezcommented, Mar 2, 2018

You can color each individual bar using an Array in backgroundColor.

Just loop through your elements and color each bar accordingly:

Fiddle-> https://jsfiddle.net/uq99110j/6/

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to color chart bars based on their values
This chart example shows bars colored differently depending on the preceding value. If a value is larger than the previous one, it will...
Read more >
Create a color grouping chart (color bars by value) in Excel
In some cases, you may need to create a column or bar chart with different colors for the bars based on values. As...
Read more >
Change chart colors based on values (like conditional ...
00:00 Change chart series colors based on value by hide/unhide Excel trick 00:09 Column chart series change color based on season00:35 ...
Read more >
Vary the colors of same-series data markers in a chart
In a chart, click to select the data series for which you want to change the colors. · On the Format tab, in...
Read more >
How to Change Bar Chart Color Based on Value
To change the bar chart color based on value, you need to group first. For example, to make a bar chart with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found