格式化数据从数据库到Highcharts系列格式

时间:2017-07-13 07:53:51

标签: javascript jquery json 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/

我在上面的小提琴链接中手动给出了系列。

如何使用变量数据事件

生成序列

1 个答案:

答案 0 :(得分: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