Add an ajaxSource property and reloadAjax method to the Series object for more streamlined JSON load
This was part of Highcharts 1.x, but was deprecated in 2.0, as it is cleaner to let jQuery handle all Ajax stuff. Best practice is to first load the Ajax data, then display the chart as shown in the how-to.
-
Jeremy Pyne commented
--- highcharts.src.js (revision 182)
+++ highcharts.new.js (working copy)
@@ -8347,10 +8347,55 @@
series.getColor();
series.getSymbol();
- // set the data
- series.setData(options.data, false);
-
- },
+ if (options.ajaxSource) {
+ // Start with any static data or a blank array as init needs to return some sort of data object and the JSON could take a bit of time.
+ series.setData(options.data, false);
+ chart.showLoading();
+
+ // Read the chart data from a JSON query.
+ $.getJSON(options.ajaxSource, null, function(json) {
+ // Put the new data into the series.
+ series.setData(json.aaData);
+ chart.hideLoading();
+ });
+ } else {
+ // set the data
+ series.setData(options.data, false);
+ }
+ },
+
+ /* Reload the data in the chart from the ajaxSource. */
+ reloadAjax: function(appendData, shift, newSource) {
+ var series = this,
+ options = series.options;
+
+ // Use the default ajax source.
+ if (typeof newSource != 'undefined')
+ options.ajaxSource = newSource;
+
+ // Overwrite existing data be default.
+ if (typeof appendData == 'undefined')
+ appendData = false;
+
+ if(appendData) {
+ // Read the chart data from a JSON query.
+ $.getJSON(options.ajaxSource, null, function(json) {
+ for(p in json.aaData) {
+ // Add each point to the series.
+ series.addPoint(json.aaData[p], false, shift);
+ }
+ chart.redraw();
+ });
+ } else {
+ chart.showLoading();
+ // Read the chart data from a JSON query.
+ $.getJSON(options.ajaxSource, null, function(json) {
+ // Put the new data into the series.
+ series.setData(json.aaData);
+ chart.hideLoading();
+ });
+ }
+ },
/** -
Jeremy Pyne commented
Add a ajaxSource property that can be set on in the Series options to automatically read the series.data value from a JSON request.
Add code to the init method to automatically read fromt he ajaxSource if set.
Add a reloadAjax method to the Series to reload the data from the ajaxSource and either append or replace the existing data.(Patch included in the next post.)