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.
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();
Users can use the highcharts plugin
var chart=$("#container").highcharts();
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];
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
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
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
Without jQuery (vanilla js):
let chartDom = document.getElementById("testDivId");
let chart = Highcharts.charts[Highcharts.attr(chartDom, 'data-highcharts-chart')]
... 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;
}
@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
: