5

The following code works:

var options1 = {
    chart: {
        renderTo: 'container1'
    },
    series: [{}]
    };

$.getJSON('tokyo.jsn', function(data){
        options1.series[0].data = data;
        var chart = new Highcharts.Chart(options1);
    });

I want to be able to add a number of data series, so I am trying to take the reference to ‘new Highcharts’ out of the getJSON, but I don't seem to get it right. This following code does not work:

$.getJSON('tokyo.jsn', function(data){
    options1.series[0].data = data;
});
var chart = new Highcharts.Chart(options1);

I have also tried tackling it a different way but again the following code does not work:

var chart1 = new Highcharts.Chart({
    chart: {
        renderTo: 'container1'
    },
    series: [{}]
});

$.getJSON('tokyo.jsn', function(data){
    chart1.series[0].data = data;
});

Can anyone point me in the correct direction. I need to be able to support multiple data series by doing a second getJSON call like the following:

$.getJSON('sydney.jsn', function(data){
    options1.series[1].data = data;
});

The JSON code I'm using is as follows:

[ 7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6 ]

Thanks

Albin
  • 4,180
  • 2
  • 27
  • 19
lachlan
  • 63
  • 1
  • 1
  • 6

2 Answers2

4

$.getJSON is an asynchronous request. Once you receive the data, then you can pass it to Highcharts, thus you have to call that code from within the callback function of $.getJSON().

Try this, use a helper function to process your data and draw the chart, see drawChart() below:

var options1 = {
    chart: {
        renderTo: 'container1'
    },
    series: []
};

var drawChart = function(data, name) {
    // 'series' is an array of objects with keys: 
    //     - 'name' (string)
    //     - 'data' (array)
    var newSeriesData = {
        name: name,
        data: data
    };

    // Add the new data to the series array
    options1.series.push(newSeriesData);

    // If you want to remove old series data, you can do that here too

    // Render the chart
    var chart = new Highcharts.Chart(options1);
};

$.getJSON('tokyo.jsn', function(data){
    drawChart(data, 'Tokyo');
});
$.getJSON('sydney.jsn', function(data){
    drawChart(data, 'Sydney');
});

See fiddle: http://jsfiddle.net/amyamy86/pUM7s/

Amy
  • 7,388
  • 2
  • 20
  • 31
  • You need first remove previous chart, before creating new one in the same container. Also, you nca simple use `addSeries()` method instead. – Paweł Fus Apr 22 '13 at 10:21
  • Hi sweetamylase. I wasn't sure how to contact you so sorry if this is inappropriate! I have an issue: http://stackoverflow.com/questions/35802637/dotnet-highchart-cost-not-being-plotted-against-the-correct-date-asp-net-c-sharp and I have no idea how to solve it. You seem to be very good at what you do and I was wondering if you would be able to assist me? Thank you! – Stuart Mar 07 '16 at 00:42
4

You can use solution used by Highcharts in that example: http://www.highcharts.com/stock/demo/compare

Or first create empty chart, without any series, and then use addSeries() function in each callback, see: http://api.highcharts.com/highcharts#Chart.addSeries()

Paweł Fus
  • 44,795
  • 3
  • 61
  • 77