Listener and ChartWrapper

1,844 views
Skip to first unread message

Peetzer

unread,
Apr 12, 2012, 8:53:14 AM4/12/12
to google-visua...@googlegroups.com
I have two chartwrappers on a dashboard, one contains controlType Linechart and the other Table.

Why does the chartWrapper does not have the getSelection method?

This is tucked away in the visualization...

google.visualization.events.addListener(chart1, 'select', function() {
chart2.visualization.setSelection(chart1.visualization.getSelection());
});

google.visualization.events.addListener(chart2, 'select', function() {
chart1.visualization.setSelection(chart2.visualization.getSelection());
});

asgallant

unread,
Apr 12, 2012, 11:53:28 AM4/12/12
to google-visua...@googlegroups.com
The 'select' events don't bubble up to the wrapper, you need to ping them off the charts built by the wrapper using the ChartWrapper#getChart method, like this (assumes that chart1 and chart2 are chart wrappers):

// since event handlers for one chart require the other chart to be available,
// we must wait for both wrappers' 'ready' events to fire
var ready {chart1falsechart2false};

google.visualization.events.addListener(chart1'ready'function({
    ready.chart1 true;
    // whichever listener fires second should fire set up the 'select' event handlers
    if (ready.chart2{
        createListeners();
    }
});
google.visualization.events.addListener(chart1'ready'function({
    ready.chart2 true;
    // whichever listener fires second should fire set up the 'select' event handlers
    if (ready.chart1{
        createListeners();
    }
});

function createListeners ({
    google.visualization.events.addListener(chart1.getChart()'select'function({
        chart2.getChart().setSelection(chart1.getChart().getSelection());
    });
    google.visualization.events.addListener(chart2.getChart()'select'function({
        chart1.getChart().setSelection(chart2.getChart().getSelection());
    });
} 

If your charts are reasonably fast to draw, it would actually be marginally better to draw the second chart from the 'ready' event handler of the first and then create the 'select' event handlers from the 'ready' event handler of the second wrapper.  If you're drawing these as part of a Dashboard, then you can skip this cross-ready event handling code and create the 'select' event handlers from the Dashboard's 'ready' event handler.

Peetzer

unread,
Apr 12, 2012, 12:38:38 PM4/12/12
to google-visua...@googlegroups.com
Thanks, this looks better. I will try the dashboard on ready. I had the idea that the events did not bubble up all time the way I had.

Peter

Op donderdag 12 april 2012 17:53:28 UTC+2 schreef asgallant het volgende:
Reply all
Reply to author
Forward
0 new messages