Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get access to a Highcharts chart through a DOM-Container?

When I render a highcharts-chart to a div container, how can I get access to the chart object through the div-Container?

I don't want to make the chart variable global.

        var chart = new Highcharts.Chart({             chart: {                 renderTo: "testDivId",                                 ... 

I want to access the chart outside of the context above like this (pseudocode), to call functions:

var chart = Highcharts.Chart("testDivId"); //access from id chart.redraw(); 
like image 726
Manuel Avatar asked Oct 24 '12 13:10

Manuel


People also ask

How can I tell if Highcharts are loaded?

To determine that chart is fully loaded you can use load event, because load event is invoked when all elements are drown (API reference https://api.highcharts.com/highcharts/chart.events.load). However when you are using series animation this runs in a timeout, then load event is invoked before animation will finish.

How do I show no data available in Highcharts?

Just don't create the highchart and insert text saying "No Data Available". Without any code, this is the most help you're likely to get.

What is Highcharts library?

Highcharts is a software library for charting written in pure JavaScript, first released in 2009. The license is proprietary. It is free for personal/non-commercial uses and paid for commercial applications.


2 Answers

Highcharts 3.0.1

Users can use the highcharts plugin

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

Highcharts 2.3.4

Read from the Highcharts.charts array, for version 2.3.4 and later, the index of the chart can be found from the data on the <div>

 var index=$("#container").data('highchartsChart');  var chart=Highcharts.charts[index]; 

All versions

Track charts in a global object/map by container id

var window.charts={};  function foo(){   new Highcharts.Chart({...},function(chart){         window.charts[chart.options.chart.renderTo] = chart;   }); }  function bar(){   var chart=window.charts["containerId"]; } 

Read Mode @ Highcharts Tips - Accessing Chart Object From a Container ID

P.S.

Some additions were made in the newer versions of Highcharts since writing this answer and have been taken from answers from @davertron, @Moes and @Przy, please upvote their comments/answers as they deserve the credit for these. Adding them here as this accepted answer would be incomplete without these

like image 182
Jugal Thakkar Avatar answered Oct 13 '22 06:10

Jugal Thakkar


you can do this

var chart = $("#testDivId").highcharts(); 

check example on fiddler

like image 24
Nerdroid Avatar answered Oct 13 '22 04:10

Nerdroid