gpt4 book ai didi

javascript - 如果条形图高于混合图表 chart.js 中的平均分,如何更改条形图的颜色

转载 作者:行者123 更新时间:2023-12-01 13:10:07 24 4
gpt4 key购买 nike

img of graph that i have created

在此引用图片中,如果它超过线条的 AvgScor,我想将条形的颜色更改为绿色

我正在使用 chart.js 混合图

在这个 img 中,从左数第四个高于平均得分

var ctx = document.getElementById("overAllScore").getContext('2d');

var totalScoreData = [60,30,50,75,45,41]; // Add data values to array
var averageData = [61,45,55,70,46,52] // Add data values to array
var labels = ["A"," Q", "C","C","A","p"]; // Add labels to array

var overAllScore = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Scores', // Name the series
data: totalScoreData,
backgroundColor: 'rgba(255, 99, 132, 0.2)',

borderWidth: 1
},
{
label: 'AvgScore',
data: averageData,
backgroundColor: '#f443368c',
borderColor: '#f443368c',

borderWidth: 1, // Specify bar border width
type: 'line',
fill: false
}]
},
options: {
responsive: true, // Instruct chart js to respond nicely.
maintainAspectRatio: false,
scales: {
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
steps: 10,
max: 100
}
}]
}

}

});
<canvas id="overAllScore" style="display: block; width: 765px; height: 382px;" width="765" height="382" class="chartjs-render-monitor"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>

最佳答案

好的,让我们首先组合一个最小的可重现示例(在问题中,请确保有人可以在此处单击“运行代码片段”或打开指向 codePen/code Sandbox 等的链接的代码-您应该将虚拟数据提供给允许其他人立即开始使用示例,就像您在屏幕截图中看到的一样):

var ctx = document.getElementById("overAllScore").getContext('2d');

var totalScoreData = [60,30,50,75,45,41]; // Add data values to array
var averageData = [61,45,55,70,46,52] // Add data values to array
var labels = ["A"," Q", "C","C","A","p"]; // Add labels to array

function colorGenerator() {
return totalScoreData.map((child,index) => {
if (child >= averageData[index]){
return 'rgba(5, 250, 10, 0.2)'
} else {
return 'rgba(255, 99, 132, 0.2)'
}
})
}

var overAllScore = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Scores', // Name the series
data: totalScoreData,
backgroundColor: colorGenerator, //set the colors with a function
borderWidth: 1
},
{
label: 'AvgScore',
data: averageData,
backgroundColor: '#f443368c',
borderColor: '#f443368c',

borderWidth: 1, // Specify bar border width
type: 'line',
fill: false
}]
},
options: {
responsive: true, // Instruct chart js to respond nicely.
maintainAspectRatio: false,
scales: {
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
steps: 10,
max: 100
}
}]
}

}

});

///from chart.js docs:
/*
var chartData = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
type: 'line',
label: 'Dataset 1',
borderColor: window.chartColors.blue,
borderWidth: 2,
fill: false,
data: [
1,2,3,4,5,6,7
]
}, {
type: 'bar',
label: 'Dataset 2',
backgroundColor: window.chartColors.red,
data: [
2,3,4,5,6,7,8
],
borderColor: 'white',
borderWidth: 2
}]

};
window.onload = function() {
var ctx = document.getElementById('canvas').getContext('2d');
window.myMixedChart = new Chart(ctx, {
type: 'bar',
data: chartData,
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js Combo Bar Line Chart'
},
tooltips: {
mode: 'index',
intersect: true
}
}
});
};
*/
<canvas id="overAllScore" style="display: block; width: 765px; height: 382px;" width="765" height="382" class="chartjs-render-monitor"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>


接下来,我们可以想出一种根据其值和平均值来更改条形颜色的方法。

结果是这样的:

enter image description here

为了实现这一点,我们需要执行以下操作:

首先,设置两个数据数组:
var totalScoreData = [60,30,50,75,45,41];
var averageData = [61,45,55,70,46,52]

然后修改新的 Chart 构造函数:
var overAllScore = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Scores', // Name the series
data: totalScoreData,
backgroundColor: colorGenerator, //set the colors with a function
borderWidth: 1
},
{
label: 'AvgScore',
data: averageData,
backgroundColor: '#f443368c',
borderColor: '#f443368c',

borderWidth: 1, // Specify bar border width
type: 'line',
fill: false
}]
}, ...

这里要注意的关键是 backgroundColor: colorGenerator,对于分数数据集。这意味着根据函数分配颜色。

这个函数看起来像这样:
function colorGenerator() {
return totalScoreData.map((child,index) => {
if (child >= averageData[index]){
return 'rgba(5, 250, 10, 0.2)' //green
} else {
return 'rgba(255, 99, 132, 0.2)' //red
}
})
}

它的作用是获取 totalScoreData数组并映射它,无论它高于相同索引处的相应平均数据 - averageData[index]然后返回绿色,否则返回红色。

就是这样 :)

关于javascript - 如果条形图高于混合图表 chart.js 中的平均分,如何更改条形图的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60666936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com