Javascript 根据值更改chart.js中的条形图颜色
我想使用chart.js(www.chartjs.org)创建一个条形图。条形图填充颜色应根据值进行更改 例如,如果该值小于10,则颜色为绿色,11-15表示黄色,16-20表示红色Javascript 根据值更改chart.js中的条形图颜色,javascript,colors,charts,bar-chart,Javascript,Colors,Charts,Bar Chart,我想使用chart.js(www.chartjs.org)创建一个条形图。条形图填充颜色应根据值进行更改 例如,如果该值小于10,则颜色为绿色,11-15表示黄色,16-20表示红色 有没有可能用chart.js做到这一点,以及如何做到这一点?我从未使用过chart.js,但这将是我的建议 Chart.defaults.global = { //this has all the information for the chart, I just removed everything else
有没有可能用chart.js做到这一点,以及如何做到这一点?我从未使用过chart.js,但这将是我的建议
Chart.defaults.global = { //this has all the information for the chart, I just removed everything else
// String - Colour of the scale line
scaleLineColor: "rgba(0,0,0,.1)"
}
我很想
if (number < 10) { Chart.defaults.global.scaleLineColor = "rgba(whatever)" }
if(number<10){Chart.defaults.global.scaleLineColor=“rgba(任意)”}
然后把你所有的代码都填进去。希望这有帮助 我没有找到使用chart.js实现这一点的方法,于是改用谷歌可视化 可以这样做:
// first index in chartData array describes the fields in the following indexes,
// The first two are the X and Y values, role: style tells the chart to use the third
// field as the style of that particular bar. role: annotation tells the chart to
// add the contents of the fourth field as a label to each bar.
var chartHeader = [['Time', 'Measurement', { role: 'style' }, { role: 'annotation' }]];
function ConvertJsonToChartData(json) {
var chartData = chartHeader;
for (var key in json) {
var x = json[key].X;
var elapsedTime = json[key].ElapsedTime;
// set style based on value of elapsedTime
var style = 'black';
if (elapsedTime > GlobalSettings['criticalThreshold']) {
style = GlobalSettings['criticalColor'];
} else if(elapsedTime > GlobalSettings['warningThreshold']) {
style = GlobalSettings['warningColor'];
}
// add a new datapoint to the chart
chartData.push([x, elapsedTime, style, elapsedTime]);
}
return chartData;
}
function MakeHourGraph(json) {
var chartData = ConvertJsonToChartData(json);
var data = google.visualization.arrayToDataTable(chartData);
var options = {
title: 'Response Times',
hAxis: { title: 'Hour', titleTextStyle: { color: 'blue' } },
vAxis: { title: 'msecs', titleTextStyle: { color: 'blue' } },
legend: { position: "none" }
};
var chart = new google.visualization.ColumnChart(document.getElementById('hour_chart_div'));
chart.draw(data, options);
}
非常古老的帖子,但对于从谷歌搜索来这里的人来说,是一种用ChartJS实现这一点的方法 您可以将数据转换为两个数据集:
-第一个为正值,将所有负值替换为0
-第二个为负值,将所有正值替换为0
originalSet = [1,-1, 3,-3, 5]
positiveSet = [1, 0, 3, 0, 5]
negativeSet = [0,-1, 0,-3, 0]
然后只需为每套添加您选择的颜色:
datasets: [
{ data: positiveSet, backgroundColor: 'rgba(0,255,0,0.5)' },
{ data: negativeSet, backgroundColor: 'rgba(255,0,0,0.5)' }
]
此方法不仅适用于正/负分隔,而且通常适用于基于值进行样式设置的情况。Chartjs实际上允许将
背景色
作为一个数组,因此您可以根据需要提供一个颜色数组。这意味着您可以在创建数据集的位置创建颜色。就我自己而言,我不喜欢:
backgroundColor: [
<?=$color?>
],
data: [
<?=$tableData;?>
],
这将改变图形中所有条的颜色,我只想改变那些表示大于阈值的值的条的颜色。较低的应该保持颜色。啊,我想我错过了那部分。但很高兴你找到了答案!对不起,我帮不上什么忙了。
var chartColors = {
color1: 'rgba(77, 166, 255, 0.5)',
color2: 'rgba(218, 165, 32, 0.5)',
color3: 'rgba(175, 0, 42, 0.5)'
};
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['value 1', 'value 2', 'value 3', 'value 4'],
datasets: [{
label: 'Dataset 1',
backgroundColor: [
chartColors.color1,
chartColors.color1,
chartColors.color1,
chartColors.color1
],
data: [29, 57, 65, 42]
}],
}
});
var colorChangeValue = 50; //set this to whatever is the deciding color change value
var dataset = myChart.data.datasets[0];
for (var i = 0; i < dataset.data.length; i++) {
if (dataset.data[i] < 30) {
dataset.backgroundColor[i] = chartColors.color1;
}
else if ((dataset.data[i] > 31) && (dataset.data[i] <= 60)){
dataset.backgroundColor[i] = chartColors.color2;
}
else{
dataset.backgroundColor[i] = chartColors.color3;
}
}
myChart.update();