2017-07-13 74 views
1

我有一個來自數據庫格式數據Highcharts系列格式

var data = [{ 
    "plants_ref_id": "1", 
    "plant_name": "MCW", 
    "employees_data": "1,1,21,21" 
}, { 
    "plants_ref_id": "2", 
    "plant_name": "VGU", 
    "employees_data": "2,3,12,12" 
}]; 

和靜態數組

var incidents = ['Near miss', 'High Potential Near miss', 'First-aid cases', 'LTI']; 

我想這個數據轉換爲特定的格式顯示Highcharts數據作爲https://jsfiddle.net/94uvmL95/1/

我已經在上面的小提琴鏈接手動給出系列。

如何生成與變量系列數據事件

回答

0

試試這個功能

function like_add(data){ 
    var Nearmiss=[]; 
    var HighPotential=[]; 
    var FirstAid=[]; 
    var LTI=[]; 
    $.each(data,function() { 
     var values=this.employees_data.split(','); 
      Nearmiss.push(parseInt(values[0])); 
      HighPotential.push(parseInt(values[1])); 
      FirstAid.push(parseInt(values[2])); 
      LTI.push(parseInt(values[3])); 
    }); 
    var incidents = [Nearmiss, HighPotential, FirstAid,LTI]; 
    return incidents; 
} 
var data =[{"plants_ref_id":"1","plant_name":"MCW","employees_data":"1,1,21,21"},{"plants_ref_id":"2","plant_name":"VGU","employees_data":"2,3,12,12"}]; 
var incidents=like_add(data); 
alert(incidents.toSource()); 

這裏只是檢查你貼是給給輸出變量jsfiddle.net

+0

小提琴事件。如果我傳遞變量到系列沒有圖顯示https://jsfiddle.net/94uvmL95/3/ –

+0

我只是忘了parseInt現在試試這個https://jsfiddle.net/94uvmL95/4/ –