0

How to convert the plotly binding click event for a pie chart?

What I currently have below is for a plotly scatter graph, but I am unsure how to use the same concept for a pie chart.

var myPlot = document.getElementById('myDiv'),
    d3 = Plotly.d3,
    N = 16,
    x = d3.range(N),
    y = d3.range(N).map( d3.random.normal() ),
    data = [ { x:x, y:y, type:'scatter',
            mode:'markers', marker:{size:16} } ],
    layout = {
        hovermode:'closest',
        title:'Click on Points'
     };

Plotly.newPlot('myDiv', data, layout);

myPlot.on('plotly_click', function(data){
    var pts = '';
    for(var i=0; i < data.points.length; i++){
        pts = 'x = '+data.points[i].x +'\ny = '+
            data.points[i].y.toPrecision(4) + '\n\n';
    }
    alert('Closest point clicked:\n\n'+pts);
});

1 Answer 1

1
myPlot.on('plotly_click', function(data){
    var pts = '';
    for(var i=0; i < data.points.length; i++){
        pts = 'label(country) = '+ data.points[0].label + '\nvalue(%) = ' + data.points[0].value;
    }
    alert('Closest point clicked:\n\n'+pts);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe you could share the rest of the code where you initialize "myPlot". This answer does not show how to bind the event to a Pie Chart, it is the same binding as in your question with a change in string "pts".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.