[英]In chartJS, change label color onhover
I would like to change the color of the label on-hovering over the data bar in chartJS.我想改变 label 悬停在 chartJS 数据栏上的颜色。 But the color change is not responsive.但是颜色变化没有反应。 Below shows the onhover code that I am using.下面显示了我正在使用的悬停代码。 Does anyone have any ideas how to change the color of the label on-hover?有谁知道如何更改悬停时 label 的颜色?
var chart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5", "Label 6", "Label 7"],
datasets: [{
data: [2000, 4000, 6000, 8000, 10000, 12000, 14000],
backgroundColor: ["#73BFB8", "#73BFB8", "#73BFB8", "#73BFB8", "#73BFB8", "#73BFB8", "#73BFB8"],
}]
},
options: {
responsive: true,
legend: {
display: false,
},
scales: {
yAxes: [{
gridLines: {
display: true,
drawTicks: true,
drawOnChartArea: false
},
}],
xAxes: [{
gridLines: {
display: true,
drawTicks: false,
tickMarkLength: 5,
drawBorder: false
},
}]
},
onHover: function (e, item) {
if (item.length > 0) {
var i = item[0]._index;
var $ctx = item[0]._chart.ctx;
var label = item[0]._chart.config.data.labels[i];
var data = item[0]._chart.config.data.datasets[0]._meta[0].data[i];
var startOfBarGraph = data._xScale.left;
var offset = 9;
var textWidth = $ctx.measureText(label).width;
var x = startOfBarGraph - offset - textWidth - 3;
var y = data._model.y;
var fontHeight = $ctx.measureText(label).actualBoundingBoxAscent + $ctx.measureText(label).actualBoundingBoxDescent;
$ctx.fillStyle = 'white';
$ctx.fillRect(x, y + 3 - fontHeight, textWidth, fontHeight + 4);
$ctx.fillStyle = 'red';
$ctx.font = "12px Arial";
$ctx.fillText(label, x, y + 3);
console.log("onHover", item, e.type);
}
}
}
});
I would like the axis label to look similar to the image below我希望轴 label 看起来类似于下图
inside of your dataset datasets: [{ data: [2000, 4000, 6000, 8000, 10000, 12000, 14000], backgroundColor: ["#73BFB8", "#73BFB8", "#73BFB8", "#73BFB8", "#73BFB8", "#73BFB8", "#73BFB8"], }]在数据集内部: [{ 数据:[2000, 4000, 6000, 8000, 10000, 12000, 14000], backgroundColor: ["#73BFB8", "#73BFB8", "#73BFB8", "#73BFB8", " #73BFB8", "#73BFB8", "#73BFB8"], }]
try adding hoverBackgroundColor:尝试添加 hoverBackgroundColor:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.