Bar chart color depending on the value.
See original GitHub issueHello,
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:
- Created 6 years ago
- Comments:9 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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/
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/