作者:mobiledu2502890657 | 来源:互联网 | 2023-01-05 18:53
1> 小智..:
You can control the label of the tooltip with it's callback
.
If you just want to display the text of the label add this to your options
:
tooltips: {
callbacks: {
label: function(tooltipItems, data) {
return data.labels[tooltipItems.index];
}
}
}
By the way: Your snippet is not working because you didn't include chart.js. I've copied your snippet and added my change below:
tooltips: {
callbacks: {
label: function(tooltipItems, data) {
return data.labels[tooltipItems.index];
}
}
}
var canvasP = document.getElementById("pieChart");
var ctxP = canvasP.getContext('2d');
var myPieChart = new Chart(ctxP, {
type: 'pie',
data: {
labels: ["Värde 1", "Värde 2", "Värde 3", "Värde 4", "Värde 5", "Värde 6", "Värde 7"],
datasets: [{
data: [1, 5, 10, 20, 50, 70, 50],
backgroundColor: ["#64B5F6", "#FFD54F", "#2196F3", "#FFC107", "#1976D2", "#FFA000", "#0D47A1"],
hoverBackgroundColor: ["#B2EBF2", "#FFCCBC", "#4DD0E1", "#FF8A65", "#00BCD4", "#FF5722", "#0097A7"]
}]
},
options: {
legend: {
display: true,
position: "right"
},
tooltips: {
callbacks: {
label: function(tooltipItems, data) {
return data.labels[tooltipItems.index];
}
}
}
}
});
canvasP.Onclick= function(e) {
var slice = myPieChart.getElementAtEvent(e);
if (!slice.length) return; // return if not clicked on slice
var label = slice[0]._model.label;
switch (label) {
// add case for each label/slice
case 'Värde 5':
alert('clicked on slice 5');
window.open('www.example.com/foo');
break;
case 'Värde 6':
alert('clicked on slice 6');
window.open('www.example.com/bar');
break;
// add rests ...
}
}