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

前端 未结 9 841
轮回少年
轮回少年 2020-12-02 11:55

When i render a highcharts-chart to a div container, how can i get access to the chart object through the div-Container? i dont want to make the chart variable global.

9条回答
  •  不知归路
    2020-12-02 12:18

    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

     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

提交回复
热议问题