0👍
✅
Solved, i was calling the items inside the library wrong.
var dData1 = 90; ///////CARREGAR DADOS DE FACTURAÇÃO
var dData2 = 70; ///////CARREGAR DADOS DE FACTURAÇÃO
var barChartData = {
labels: ["MÊS", "ANO"],
datasets:
[{
fillColor: "rgba(220,220,220,1)",
strokeColor: "rgba(220,220,220,0.0)",
highlightFill: "rgba(220,220,220,0.0)",
highlightStroke: "rgba(220,220,220,0)",
data: [dData1, dData2]
}]
}
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx).Bar(barChartData, {
responsive: true,
barValueSpacing: 10
});
if (barChartData.datasets[0].data=[dData1] < 75)
{
console.log("vermelho");
myChart.datasets[0].bars[0].fillColor = "rgba(243,124,138,1.00)";
myChart.update();
}
else if (dData1 > 76 && dData1 < 85)
{
console.log("amarelo");
myChart.datasets[0].bars[0].fillColor = "rgba(243,233,7,1.00)";
myChart.update();
}
else if (dData1 > 86)
{
console.log("verde");
myChart.datasets[0].bars[0].fillColor = "rgba(154,226,125,1.00)";
myChart.update();
} else
{
console.log("branco");
myChart.datasets[0].bars[0].fillColor = "rgba(255,255,255,1)";
myChart.update();
};
if (barChartData.datasets[0].data=[dData2] < 75)
{
console.log("vermelho2");
myChart.datasets[0].bars[1].fillColor = "rgba(243,124,138,1.00)";
myChart.update();
}
else if (dData2 > 76 && dData2 < 85)
{
console.log("amarelo2");
myChart.datasets[0].bars[1].fillColor = "rgba(243,233,7,1.00)";
myChart.update();
}
else if (dData2 > 86)
{
console.log("verde2");
myChart.datasets[0].bars[1].fillColor = "rgba(154,226,125,1.00)";
myChart.update();
} else
{
console.log("branco2");
myChart.datasets[0].bars[1].fillColor = "rgba(255,255,255,1)";
myChart.update();
};
1👍
You need to set the colors for the bars
after generating the chart. You’ll need to set the fillColor
, the highlightFill
and _saved.fillColor
(which Chart.js uses to restore the bar color after a tooltip hover)
So you need something like
if (dData1 < 75)
{
barChart.datasets[0].bars[0].fillColor = barChart.datasets[0].bars[0].highlightFill = barChart.datasets[0].bars[0]._saved.fillColor = "#f37c8a";
}
else if (dData1 > 76 && dData1 < 85)
{
barChart.datasets[0].bars[0].fillColor = barChart.datasets[0].bars[0].highlightFill = barChart.datasets[0].bars[0]._saved.fillColor = "#f3e97c";
}
else if (dData1 > 86)
{
barChart.datasets[0].bars[0].fillColor = barChart.datasets[0].bars[0].highlightFill = barChart.datasets[0].bars[0]._saved.fillColor = "#9ae27d";
} else
{
barChart.datasets[0].bars[0].fillColor = barChart.datasets[0].bars[0].highlightFill = barChart.datasets[0].bars[0]._saved.fillColor = "#fff";
};
for the 1st block, where barChart
is your chart object. Note that the above code assumes a single series of 2 elements.
Fiddle – https://jsfiddle.net/2a86gqsa/
Note that you had dData1 < 85
in the second block – I changed it to dData2 < 85
. Seeing that much of the code for the 2 blocks are common you might want to move it to a function.
Source:stackexchange.com