[英](chart.js v3.5.1) change bar background, label font color on click
我想更改条形背景、标签字体颜色和标签字体粗细。 但只有条形背景颜色有效。
如何在点击时更改字体颜色和字体粗细?
请帮帮我
const backgroundColor = ['rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)'];
const fontColor = ['rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)'];
const fontWeight = [400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400];
const barChart = new Chart(
document.getElementById("barChart"), {
type: 'bar',
data: {
labels: ["jan", "fab", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
datasets: [{
data: [8, 2, 3, 0, 5, 2, 2, 0, 0, 6, 10, 2],
backgroundColor: backgroundColor,
borderRadius: 6,
borderSkipped: false,
barThickness: 20,
}]
},
options:{
onClick: function(evt){
const points = barChart.getElementsAtEventForMode(evt, 'nearest', { intersect: true }, true);
if (points.length) {
const firstPoint = points[0];
for (let i=0; i<backgroundColor.length; i++) {
backgroundColor[i] = 'rgb(197,197,197)';
fontColor[i] = 'rgb(153,153,153)';
fontWeight[i] = 400;
}
backgroundColor[firstPoint.index] = '#2157e4';
fontColor[firstPoint.index] = 'rgb(32,32,32)';
fontWeight[firstPoint.index] = 600;
this.update();
}
},
scales: {
y: {
display: false
},
x: {
grid: {
color: 'white',
drawBorder: false
},
ticks: {
color: fontColor,
font: {
family: "'Pretendard', sans-serif",
weight: fontWeight
},
}
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.