2016-08-05 46 views
0

我試圖更新「餡餅」圖表類型的系列數據的選項:如何通過一系列繪製在highcharts選項

我使用導出按鈕顯示選項更改圖表類型,所有其他類型的圖表工作除了需要不同格式的系列數據的餡餅之外。

exporting: { 
        buttons: { 
         lineButton: { 
          text: 'line', 
          onclick: function() { 
           for(i=0;i<this.series.length;i++) { 
            this.series[i].update({ 
             type: "line" 
            }); 
           } 
          } 
         }, 
         barButton: { 
          text: 'bar', 
          onclick: function() { 
           for(i=0;i<this.series.length;i++) { 
            this.series[i].update({ 
             type: "column" 
            }); 
           } 
          } 
         }, 
         pieButton: { 
          text: 'pie', 
          onclick: function() { 
          var pieSeries = []; 

          $.each(category_totals, function(j, k) { 
           pieSeries.push({ name: j , y: k }); 
          }); 

          for(i=0;i<this.series.length;i++) { 
           this.series[i].remove();  
          } 

          this.series = [{ 
           name: title, 
           colorByPoint: true, 
           data: pieSeries 
          }]; 

          this.series[0].update({ 
            type: "pie" 
           }); 
         } 
        } 
       } 
... 

而且我得到這個錯誤:Uncaught TypeError: this.series[0].update is not a function

+0

一個簡單的問題,爲什麼你就不能定義類型在其中定義系列?即:this.series = [name:title, colorByPoint:true, data:pieSeries, type:'pie' }]'? –

+0

@RahulSharma嘗試過。同樣的錯誤。 – ImpendingDoom

+0

同樣的錯誤?你的代碼中是否還有'this.series [0] .update'語句?如果你按我在第一條評論中提到的那樣做,你就不需要這個陳述。 –

回答

0

1.

for(i=0;i<this.series.length;i++) { 
    this.series[i].remove();  
} 

上面的代碼將不會刪除一系列項目:see here

2. 正確的方式來增加系列是: this.addSeries({...});

3. 最後的工作代碼:

... 
pieButton: { 
    text: 'pie', 
    onclick: function() { 
     var pieSeries = []; 

     $.each(category_totals, function(j, k) { 
      pieSeries.push({ name: j , y: k }); 
     }); 

     while(this.series.length > 0) { 
      this.series[0].remove(true); 
     } 


     this.addSeries({ 
      name: title, 
      colorByPoint: true, 
      data: pieSeries, 
      type: 'pie' 
     }); 

     // As Rahul Sharma pointed out in comments above, 
     // you can pass the "type" option to 
     // addSeries method, making this call redundant 
     // this.series[0].update({ 
     //  type: "pie" 
     // }); 
    } 
} 
... 
+0

看看我們的答案時間:p –

+0

@RahulSharma更新了您的建議。 – ImpendingDoom

+0

@RahulSharma用'正確的方式刪除系列'更新你的答案,我會接受你的,而不是我的。 – ImpendingDoom

1

的問題是,你依次取出從圖表的系列,在每個調用圖表重繪和for循環結束,圖表不會有任何系列。當你做

this.series = [{ 
       name: title, 
       colorByPoint: true, 
       data: pieSeries 
}] 

要修改的JavaScript對象,因此update方法,當你嘗試做

this.series[0].update({ 
         type: "pie" 
}); 

因爲你試圖調用Highcharts方法的通用JavaScript對象上不可用。 你應該做的是

this.addSeries({ 
       name: title, 
       colorByPoint: true, 
       data: pieSeries, 
       type: 'pie' 
}) 

此外,建議:通false參數來remove方法,以便它不會重繪每次。只需添加新系列時重新繪製。 所以上面的調用看起來像

this.addSeries({ 
       name: title, 
       colorByPoint: true, 
       data: pieSeries, 
       type: 'pie' 
}, true) 
相關問題