[英]Display character after y-axis data value in onhover pop up Chart.js
I'm new to Chart.js and I'm having a little bit of trouble customizing the onHover() pop up.我是 Chart.js 的新手,在自定义 onHover() 弹出窗口时遇到了一些麻烦。 I want to add a percent symbol after the y axis data value on the pop up, so what would be displayed will look something like this, label: value%.我想在弹出窗口的 y 轴数据值后添加一个百分号,这样显示的内容将如下所示,label: value%。 I have tried using scaleLabel and ticks with now luck.我已经尝试使用 scaleLabel 和 ticks,现在运气不错。
Right now this is how it looks:现在这是它的样子:
and here is my code:这是我的代码:
let chart = new Chart(ctx, {
type: 'line',
data: {
labels: dates,
datasets: [
{
label: 'Casos confirmados',
backgroundColor: 'rgb(224, 224, 224)',
borderColor: 'rgb(0, 0, 0)',
data: confirmedCases
}
]
},
options: {
scales: {
yAxes: [{
gridLines: {
drawTicks: true,
},
ticks: {
display : true,
// Include a percent sign in the ticks
callback: function(value, index, values) {
return value + '%';
}
},
scaleLabel: {
display: true,
labelString: '%'
}
}]
}
}
});
If anyone could give me any pointers I would greatly appreciate it.如果有人能给我任何指示,我将不胜感激。
You can define tooltips.callbacks.label
function inside the chart options as follows:您可以在图表选项中定义tooltips.callbacks.label
function 如下:
tooltips: {
callbacks: {
label: (tooltipItem, data) => data.datasets[tooltipItem.datasetIndex].label + ': ' + tooltipItem.yLabel + '%'
}
},
Please take a look at your amended code and see how it works.请查看您修改后的代码,看看它是如何工作的。
let chart = new Chart('myChart', { type: 'line', data: { labels: ['A', 'B', 'C', 'D', 'E', ], datasets: [{ label: 'Casos confirmados', backgroundColor: 'rgb(224, 224, 224)', borderColor: 'rgb(0, 0, 0)', data: [5, 6, 4, 3, 6] }] }, options: { tooltips: { callbacks: { label: (tooltipItem, data) => data.datasets[tooltipItem.datasetIndex].label + ': ' + tooltipItem.yLabel + '%' } }, scales: { yAxes: [{ ticks: { beginAtZero: true, callback: value => value + '%' }, scaleLabel: { display: true, labelString: '%' } }] } } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script> <canvas id="myChart" height="90"></canvas>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.