Trigger legend click event in nvd3 using jquery?
Asked Answered
R

2

6

I'm using nvd3 for a multi-bar chart, and I'd like to make the chart redraw when the user clicks the other html on my page. I tried using jQuery to select the "Stream0" legend circle on the nvd3 homepage (http://nvd3.org/) and click it using this snippet in the console:

$($('g.nv-series')[0]).click()

For reasons that I hope will be immediately obvious to people more knowledgeable about javascript, nothing happens. Is it something to do with event delegation?

http://nvd3.org/

Rid answered 29/8, 2013 at 2:47 Comment(1)
This question should help.Verlie
J
10

you can try this:

chart.legend.dispatch.legendClick = function(d, i){
//redraw
};

It will append your own method to the legend; it works for pie chart not sure if works for the line chart;

Jacquelinjacqueline answered 10/10, 2013 at 6:40 Comment(1)
This code runs when the user click the legend, but it is unclear how it could trigger a click event.At
A
2

Maybe there is some help in this. Two charts, one pie, one stack but only showing legends on pie. The data is not identical but the legends are.. Want to update both on clicking pie legends.

        chart.legend.dispatch.on('stateChange.pie', function(d,i){
            setTimeout(function() {
                stackedAreaChart.dispatch.changeState(d,i);
                stackedAreaChart.update();
            }, 100);
        }); 

Note: using the ".pie" will extend the (library) stateChange event (not overwrite it) The other chart stackedAreaChart has to be in the scope. Note there is a changeState event and a stateChange, best is to look at the un-minified nvd3 js file..

Affliction answered 23/10, 2014 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.