score:144

Accepted answer

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, @nerdroid and @frzy, please upvote their comments/answers as they deserve the credit for these. adding them here as this accepted answer would be incomplete without these

score:1

@elo's answer is correct and upvoted, though i had to tidy it a little to make it clearer:

const mychartel = document.getelementbyid('the-id-name');
const mychart = highcharts.charts[mychartel.getattribute('data-highcharts-chart')];

mychart then becomes a live highcharts object that exposes all current props present in the chart that's rendered in the mychartel. since mychart is a highcharts object, one can chain prototype methods right after it, extend it or refer to it.

mychart.gettable();
mychart.downloadxls();
settimeout(() => highcharts.fireevent(mychart, "redraw"), 10);

one can also get mychart through .highcharts(), which is a jquery plugin:

var mychart = $("#the-id-name").highcharts();

the jquery plugin approach above requires jquery to be loaded before the plugin is used, and of course the plugin itself. it was the absence of this plugin that got me into looking for alternative ways to accomplish the same with pure vanilla javascript.

by using the pure js approach i was able to do what i needed (the second code snippet) without having to rely on jquery:

score:2

... and with the help of a colleague... a better way to do it is...

getchartreferencebyclassname(classname) {
    var foundchart = $('.' + classname + '').eq(0).parent().highcharts();

    return foundchart;
}

score:4

var chart1; // globally available
$(document).ready(function() {
      chart1 = new highcharts.chart({
         chart: {
            renderto: 'container',
            type: 'bar'
         },
         title: {
            text: 'fruit consumption'
         },
         xaxis: {
            categories: ['apples', 'bananas', 'oranges']
         },
         yaxis: {
            title: {
               text: 'fruit eaten'
            }
         },
         series: [{
            name: 'jane',
            data: [1, 0, 4]
         }, {
            name: 'john',
            data: [5, 7, 3]
         }]
      });
   });

the var chart1 is global so you can use to access de highchart object doesnt matter wich is the container

chart1.redraw();

score:5

without jquery (vanilla js):

let chartdom = document.getelementbyid("testdivid");
let chart = highcharts.charts[highcharts.attr(chartdom, 'data-highcharts-chart')]

score:9

i found another way of doing it... mainly because i'm using highcharts that are embedded in outsystems platform, and i don't have a way to control the way charts are created.

the way that i found was the following:

  1. give an identifying class to the chart using classname attribute

    chart: {
        classname: 'lifecyclemasterchart'
    }
    
  2. define an auxiliary function to get the chart by class name

    function getchartreferencebyclassname(classname) {
    var cssclassname = classname;
    var foundchart = null;
    
    $(highcharts.charts).each(function(i,chart){    
        if(chart.container.classlist.contains(cssclassname)){
            foundchart = chart;
            return;
        }
    });
    
    return foundchart;
    

    }

  3. use the auxiliary function wherever you need it

    var detailchart = getchartreferencebyclassname('lifecycledetailchart');
    

hope it helps you!

score:10

simply with pure js :

var obj = document.getelementbyid('#container')
    highcharts.charts[obj.getattribute('data-highcharts-chart')];

score:22

var $chartcont = $('#container').highcharts({...}),
    chartobj = highcharts.charts[$chartcont.data('highchartschart')];

chartcont is jquery object. chartobj is highchart chart object.

this is using highcharts 3.01

score:45

you can do this

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

check example on fiddler


Related Query