我很惊讶地发现几乎没有关于这个话题的信息。
我有一个ChartJS饼状图,我想在单击饼图的一个切片后进行下钻。
你会怎样做呢?
谢谢
我很惊讶地发现几乎没有关于这个话题的信息。
我有一个ChartJS饼状图,我想在单击饼图的一个切片后进行下钻。
你会怎样做呢?
谢谢
$(document).ready(function() {
var canvas = document.getElementById("myChart");
var ctx = canvas.getContext("2d");
var myNewChart = new Chart(ctx, {
type: 'pie',
data: data
});
canvas.onclick = function(evt) {
var activePoints = myNewChart.getElementsAtEvent(evt);
if (activePoints[0]) {
var chartData = activePoints[0]['_chart'].config.data;
var idx = activePoints[0]['_index'];
var label = chartData.labels[idx];
var value = chartData.datasets[0].data[idx];
var color = chartData.datasets[0].backgroundColor[idx]; //Or any other data you wish to take from the clicked slice
alert(label + ' ' + value + ' ' + color); //Or any other function you want to execute. I sent the data to the server, and used the response i got from the server to create a new chart in a Bootstrap modal.
}
};
});
这个方案非常完美。不过,我会使用AJAX将信息发送到服务器,并在Bootstrap模态框中显示一个新的图表,而不是弹出警告信息。