【发布时间】:2022-10-24 13:58:47
【问题描述】:
我一直在尝试将 chart.js 添加到我的 Django 项目中,到目前为止效果很好。我用两片做了一个甜甜圈图。现在我想让这些切片中的每一个在点击时都有单独的动作,例如重定向到新的一面。
这些是我的图表设置:
var config = {
type: 'doughnut',
data: {
datasets: [{
data: {{ data|safe }}, // Error because django and js are being mixed
backgroundColor: [
'#ff0000', '#008000'
],
label: 'Population'
}],
labels: {{ labels|safe }}
},
options: {
responsive: true
}
};
这是渲染和我在点击时执行操作的功能:
window.onload = function() {
var ctx = document.getElementById('pie-chart').getContext('2d');
var myPieChart = new Chart(ctx, config);
$('#myChart').on('click', function(event) {
var activePoints = myPieChart.getElementsAtEvent(event)
if(activePoints[0]){
console.log("Helo 1")
}
else {
console.log("helo 2")
}
})
};
我在其他页面上看到了我的解决方案,但它根本不起作用。我错过了什么吗?如果是,你能帮忙吗?
【问题讨论】:
标签: javascript python html django chart.js