Is there any onReady
(or similar) ready event for HighCharts?
Currently, HighCharts only offers addSeries
, click
, load
The Highcharts.Chart constructor takes a callback argument that is called "when the chart object is finished loading and rendering". The chart object is passed as an argument to the callback.
$("#the-chart").highcharts(options, function (chart) {
alert("The chart is ready now");
console.log("chart", chart);
});
Chart (Object options, Function callback) This is the constructor for creating a new chart object.
Parameters
options: Object
The chart options, as documented under the heading "The options object"in the left menu.callback: Function
A function to execute when the chart object is finished loading and rendering. In most cases the chart is built in one thread, but in Internet Explorer version 8 or lessthe chart is sometimes initiated before the document is ready, and in thesecases the chart object will not be finished directly after callingnew Highcharts.Chart(). As a consequence, code that relies on the newly built Chart object should always run in the callback. Defining a chart.event.load handler is equivalent.Returns
- Chart
Indeed the delayed call is not a very good approach. The load
event is working properly, but the current chart is referred by the this
keyword, i.e.
// create the chart
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
events: {
load: function(event) {
//When is chart ready?
console.log(this); //this refers to the loaded chart.
}
}
},
xAxis: {
},
series: [{
animation: false,
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
Demo
Hope this helps :)
This is definitely less elegant than the accepted answer, but still works fine with a few lines of code. The essence is to simply poll all chart container HTML elements.
The code below assumes you have one or more Highcharts attached to elements having class="chart":
var chartsToLoad = [];
$('.chart').each(function(n,elem) {
chartsToLoad[n] = window.setInterval(function() {
if(typeof($(elem).highcharts()) !== 'undefined') {
// It's loaded now, go ahead...
$(elem).highcharts().doSomeHighchartsStuffHere()
// Permanently stop polling
window.clearInterval(chartsToLoad[n]);
}
}, 100); // Check every 100ms
});