Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a reference to a Highcharts chart

I render a Highcharts chart in a div with id container like so:

new Highcharts.Chart({
    chart: {
        renderTo: 'container',
        type: 'line',
        zoomType: 'x'
    },

    xAxis: {
        categories: ['Jan', 'Feb']
    },

    series: [{
        data: [29.9, 71.5]
    }]
});
<div id="container" class="chart"></div>

Notice that I haven't captured a reference to the chart object in a variable. At some point after the chart has been rendered is it possible to get a reference to the chart object from the ID of the element it has been rendered to (container in this case)?

like image 411
Dónal Avatar asked Sep 21 '25 09:09

Dónal


1 Answers

With jQuery

var Mychart=$("#container").data('highchartsChart');

or

var Mychart=$("#container").highcharts();

jsfiddle


or via the Highcharts' charts array ([0] if your chart is the first)

var Mychart = Highcharts.charts[0];
like image 88
WhiteLine Avatar answered Sep 23 '25 00:09

WhiteLine