Domanda

I want to bind a function to a click event for a data point in Highcharts. Clicking a data point has to call a function, but the arguments to the function are dynamic (i.e. there are several graphs drawn, and the "car" variable is generated anew for each graph).

How to bind "getCarDate()" to click event properly? This does not work (car simply holds the last assigned value instead of a different value for each plot):

                         plotOptions: {
                            series: {
                                cursor: 'pointer',
                                point: {
                                    events: {
                                        click: function (e) {
                                            getCarDate(this.x, car)
                                        }
                                    }
                                },
                                marker: {
                                    lineWidth: 1
                                }
                            }
                        },
È stato utile?

Soluzione

One trick you could use here would be to stash the value into the Highcharts options:

$('#container'+i).highcharts({

    myData: car,

    plotOptions: {
        series: {
            cursor: 'pointer',
            point:{                 
                events: {
                    click: function(event) {     
                       alert(this.series.chart.userOptions.myData);
                    }
                }
           }
        }
    },

Here's a fiddle.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top