Click events on Pie Charts in Chart.js

后端 未结 10 2087
失恋的感觉
失恋的感觉 2020-11-28 03:29

I\'ve got a question regard Chart.js.

I\'ve drawn multiple piecharts using the documentation provided. I was wondering if on click of a certain slice of one of the

10条回答
  •  借酒劲吻你
    2020-11-28 03:48

    Within options place your onclick and call the function you need as an example the ajax you need, I'll leave the example so that every click on a point tells you the value and you can use it in your new function.

        options: {
            plugins: {
                // Change options for ALL labels of THIS CHART
                datalabels: {
                    color: 'white',
                    //backgroundColor:'#ffce00',
                    align: 'start'
                }
            },
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero:true,
                        fontColor: "white"
                    },gridLines: {
                            color: 'rgba(255,255,255,0.1)',
                            display: true
                        }
                }],
                xAxes: [{
                    ticks: {
                        fontColor: "white"
                    },gridLines: {                        
                            display: false
                        }
                }]
            },
            legend: {
            display: false
    
        },
        //onClick: abre     
            onClick:function(e){ 
        var activePoints = myChart.getElementsAtEvent(e); 
        var selectedIndex = activePoints[0]._index; 
        alert(this.data.datasets[0].data[selectedIndex]); 
    
    
    } 
        }
    

提交回复
热议问题