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"];
}
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
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:
Give an identifying class to the chart using className attribute
chart: {
className: 'LifeCycleMasterChart'
}
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;
}
Use the auxiliary function wherever you need it
var detailChart = getChartReferenceByClassName('LifeCycleDetailChart');
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.
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: