2017-02-18 46 views
0

我想動態地爲我的對象中的每個鍵設置數據類型。如何使用我的數據對象中的鍵設置c3.js圖表​​數據類型?

物體看起來像:

{"x":["2016-01-1", "2016-01-02", etc], "uniqueKey":[4234, 4324, etc], "uniqueKey2":[432, 543, etc], ... } 

鍵和名稱將取決於我搶什麼數據變化的數字,但我需要爲每一個關鍵的圖表數據類型。

function show_chart1(data) { 

    var json = JSON.parse(data); 

    var chart = c3.generate({ 
     bindto: '#chart', 
     data: { 
      x: 'x', 
      xFormat: '%Y-%m-%d', 

      json: json, 

      #### need these to be the unique key names 
      types: { 
       uniqueKey: 'area-spline', 
       uniqueKey2: 'area-spline' 
       ... 
      }, 
      groups: [[uniqueKey, uniqueKey2, etc]] 
     }, 
     axis: { 
      x: { 
       type: 'timeseries', 
       tick: { 
        format: '%Y-%m-%d' 
       } 
      } 
     }  
    }); 
}; 

我該怎麼做?

回答

1

您可以爲您的類型/組建立另一個對象。如果你沒有一些圖表變化,你可以使用type: 'area-spline'而不是給每個條目相同的類型。

當你知道你唯一的密鑰或他們的樣子(reffering到正則表達式),你可以這樣做:

var typeDefinition = {} 
for(var key in json){ 

    // here you can use if or switch to apply certain types 
    if(key <your statement>){ 
     typeDefinition[key] = 'area-spline'; 
    } 

} 
<...> 
types: typeDefinition 
<...> 
相關問題