Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger legend click event in nvd3 using jquery?

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/

like image 726
twneale Avatar asked Aug 29 '13 02:08

twneale


2 Answers

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;

like image 182
Jeremy Bao Avatar answered Sep 21 '22 05:09

Jeremy Bao


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..

like image 45
Thrullerinn Avatar answered Sep 21 '22 05:09

Thrullerinn