2014-09-24 119 views
0

我有幾個陣列的數據,我想添加到高圖系列。當添加第二個或第三個數組得到一個錯誤或setData未定義。第一個系列繪製正確。Highcharts多個數據系列

  $('#container').highcharts({ 
      chart: {}, 
      series: [{ 
       data:[] 
      }] 
     }); 



     function showChart() { 

     var series1 = [1, 52, 5, 98, 5, 929, 1, 9, 48]; 
     var series2 = [1, 92, 35, 8, 25, 729, 61, 29, 38]; 
     var series3 = [1, 59, 75, 26, 25, 829, 11, 19, 48]; 

     var mySeries = []; 

     var chart = $('#container').highcharts(); 

     chart.series[0].setData(series1); 
     chart.series[1].setData(series2); 
     chart.series[2].setData(series3); 
     chart.redraw(); 

    }//end show chart 

回答

2

您正在initally創建圖表與這裏單個空白系列:

series: [{ 
    data:[] 
}] 

chart.series[1](第二系列)和chart.series[2](第三系列),因此未定義。

快速解決您的代碼可能是:

var chart = $('#container').highcharts(); 
var series1 = [1, 52, 5, 98, 5, 929, 1, 9, 48]; 
var series2 = [1, 92, 35, 8, 25, 729, 61, 29, 38]; 
var series3 = [1, 59, 75, 26, 25, 829, 11, 19, 48]; 

chart.series[0].setData(series1, false); // setData on existing series, don't redraw 
chart.addSeries({data: series2}, false); // add new series, don't redraw 
chart.addSeries({data: series3}, true); // add new series, now redraw 

不是布爾參數傳遞給above methods將控制重繪。

示例here