score:6
you can color each individual bar by accessing window.myobjbar.datasets[0].bars[0].fillcolor
property, then update the chart
in your case, you can loop through your elements and color each bar accordingly
var bars = myobjbar.datasets[0].bars;
for(i=0;i<bars.length;i++){
var color="green";
//you can check for bars[i].value and put your conditions here
bars[i].fillcolor = color;
}
myobjbar.update(); //update the chart
a working jsfiddle
score:0
you could do something like that
changecolor(loc_health);
function changecolor(random_value){
switch(random_value){
case = 1:
datasets[0].fillcolor = "#000";
break;
case = 2:
//etc
}
myobjbar.update();
}
calling update() on your chart instance will re-render the chart with any updated values, allowing you to edit the value of multiple existing points, then render those in one animated render loop.
score:4
you could instead check the value before you create the chart:
var context = document.getelementbyid('healthratings').getcontext('2d');
var colors = []
for(var i = 0; i < loc_health.length; i++){
var color;
switch(loc_health[i]){
case 1:
color = "red";
break;
case 2:
color = "orange";
break;
case 3:
color = "yellow";
break;
case 4:
color = "green";
break;
//etc..
}
colors[i] = color;
}
window.myobjbar = new chart(context).bar({
labels : loc_id_grab,
datasets : [{
backgroundcolor: colors,
strokecolor : "rgba(151,187,205,1)",
pointcolor : "rgba(151,187,205,1)",
pointstrokecolor : "#fff",
data : loc_health
}]
}, {
scaleoverride : true,
scalesteps : 10,
scalestepwidth : 1,
scalestartvalue : 0,
barshowstroke : false,
barstrokewidth : 1,
showtooltips : false,
barvaluespacing : 2,
animation : false,
responsive : true,
maintainaspectratio : true
});
Source: stackoverflow.com
Related Query
- chart.js bar chart color change based on value
- chart.js bar chart change color based on value
- Chart Js - Globally set bar chart color based on value
- Unable to change y-axis value dynamically based on data for Stacked bar chart in reactjs
- How to change line segment color based on label value in chart.js?
- How to use segment property to color line / border color based on value in chart js?
- ChartJS 2.6 - Change color of bar in Bar chart programmatically
- Change bar color depending on value
- chart js bar chart add animation to change color
- chart.js label color change based on value
- Chart JS change pointBackgroundColor based on data value
- Chart JS - Change the color of the last element in a Bar Chart
- Chart.js Change color of the values *inside* the bars of a Bar chart
- How to change bar color acording to label value in chartjs
- Chartjs Bar Chart add background color to value labels
- Cannot change the color of column chart according to its value
- Chart js bar with the highest value is a different color among the rest - React
- How to make bars change color based on value in chartsjs
- How to change bar color on a angular-chart and chart js bar graph with multiple datasets?
- Change Step Area Color Based on Value
- Change Vertical Line and Color bar chart in bar chart.js
- chartjs datalabels change font and color of text displaying inside pie chart
- Chart.js change color of Bar
- Chart.js bar chart : Grid color and hide label
- Different color for each column in angular-chartjs bar chart
- angular-chart.js bar chart with 100% opacity color
- Remove the label and show only value in tooltips of a bar chart
- how to change Y axis value dynamically based on user input in Chartjs for Line chart?
- Chart.js: Minimum value for x-axis at horizontal stacked bar chart
- ChartJS click on bar and change it's background color
More Query from same tag
- Chart.Mvc colliding with System.Web.Helpers.Chart
- Two data sets with different time instances on the same ChartJs chart
- how to only show zero grid axes at center and hide all other gridlines in chart js
- Can you set seperate colors for different axes in Chart.js?
- Javascript array not initialized
- how to show data value on bar chart body rather than using tooltip?
- I dont know how to sum expenses based on category in sequelize
- Chart.js show tooltips on page load
- ChartJS click on bar and change it's background color
- Scatter plot not showing anything chartjs needs some debugging
- Chartjs error : time scale: "time.format" is deprecated. Please use "time.parser" instead
- How to add left padding for my charts done in ChartJs and my Google Map so it is not glued to the limit of the page on the left
- chart.js API returns bad x and y points
- Changing color of specific chartjs point
- Multiple chart on one page using chart.js and Flask app
- Why is my Line Chart not displaying (Chart.JS)?
- Chart.js passing value data dynamic
- How populate chart.js with sql data?
- Passing data to a chart javascript object and array
- How to create vertical arbitrary line in financial chart?
- bootstrap ui+angular chart: is it possible to disable graph's auto refreshing?
- Re-write chart.js functionality in ng2-charts
- ChartJS: how to change data in hover box?
- Chart.js two Y-Axis, one with negative values
- Chart.js ng2-charts colors in pie chart not showing
- chart.js increase value on click
- Cannot read property 'transition' null chartjs
- Hide empty bars in Grouped Stacked Bar Chart - chart.js
- How do I hide line outside the min/max (scale area) in chartjs 2.0?
- How to render data using chartjs in expressJS