1

I like to draw multiple charts on one page with using the same options but with different json data for each chart. I like to do this with as little code as possible, I really need to omit code duplication.

Here is the example of the first chart:

$(function() {

    $.getJSON('login.php', function(data) {  
        var options= {
            chart : {
                renderTo : 'container'
            },
            rangeSelector : {
                enabled:false
            },
            series : data
        };

        var chart = new Highcharts.StockChart(options);
    });
});

How could I use this above code to create multiple charts using different getJSON data?

Ricardo Alvaro Lohmann
  • 26,031
  • 7
  • 82
  • 82
user1471980
  • 10,127
  • 48
  • 136
  • 235
  • possible duplicate of [Manage multiple highchart charts in a single webpage](http://stackoverflow.com/questions/8253590/manage-multiple-highchart-charts-in-a-single-webpage) – Ricardo Alvaro Lohmann Aug 12 '13 at 20:55

2 Answers2

1

You could create a helper function:

$(function() {

    var genOptions = function(data) {
        return {
            chart : {
                renderTo : 'container'
            },
            rangeSelector : {
                enabled:false
            },
            series : data
        };
    };

    $.getJSON('login.php', function(data) {  
        var options = genOptions(data);
        var chart = new Highcharts.StockChart(options);
    });

    $.getJSON('secondpage.php', function(data) {  
        var options = genOptions(data);
        var chart = new Highcharts.StockChart(options);
    });
});
Blam
  • 2,888
  • 3
  • 25
  • 39
0

You can follow the example as @blam, but with a slight change. Make sure the chart.renderTo value is different for each chart. From the above example I notice only one container value.