Roman Kolbovich
Roman Kolbovich

Reputation: 89

How to handle click event on AmCharts Stock graph?

I'm trying to do it using amStockCharts v3. I've seen this question: here, but it doesn't seems to work with Stock charts.

How can I handle click event for this chart - http://www.amcharts.com/demos/multiple-data-sets/? I need to be able to get date and value corresponding to a clicked point.

Upvotes: 2

Views: 3028

Answers (1)

gerric
gerric

Reputation: 2297

As you can see here the stockchart itself does not support click events on the graph or items.
However, you can solve this by adding listeners to single panels. (see docs)
As the panels are initialized after the chart chart.panels[x].addListener() won't work.
Instead you have to wrap it in the charts init event like this:

chart.addListener("init", function () {
    for( var x in chart.panels ) {
        chart.panels[x].addListener("clickGraphItem", function(e) {
            alert("index: "
                    + e.item.index
                    + "\nvalue: "
                    + e.item.values["value"]); //valuefield depends on your mapping!!!
        });
    }
});

Important for "clickGraphItem" is, that there must be an actual item to be clicked on. In other words there must be a bullet. If you don't want them to be seen, just set their alpha to zero.

bullet: "round",
bulletAlpha: 0

You can play around with the shape and size of the bullets, to change the click area.
I prepared a fiddle for you.

Upvotes: 5

Related Questions