需要从工具提示中删除数据值('Varde 7:50'中的数字,我试图删除'50')。尝试了很多方法但却无法弄明白。这是我目前的代码。它包括从我需要的饼图链接出来的方法。
1)需要从工具提示说明中删除数据值。所以只是没有数据值的标签。 2)需要饼图的每个部分链接到外部URL。
仅供参考 - 由于某些原因,我的网站上的代码段运行正常,但编辑器中却没有?请参阅页面底部的工作版:http://soccer.virnative.com/soccer/player-development-model
请帮忙! 原始代码来自: chart.js - link to other page when click on specific section in chart
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"
}
}
});
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 ...
}
}
<canvas id="pieChart" width="400" height="400"></canvas>
答案 0 :(得分:5)
您可以使用callback
来控制工具提示的标签。
如果您只想显示标签文字,请将其添加到Access-Control-Allow-Origin: *
:
options
顺便说一句:您的代码段不起作用,因为您没有包含chart.js。我复制了你的代码片段,并在下面添加了我的更改:
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 ...
}
}