如何准备数据格式以显示在c3js图表中?

时间:2016-01-29 06:02:23

标签: php charts

我想显示百分比取决于c3js图表中的日期和时间序列。

我有四个部门ID,我这样查询结果。

$dept_id_arr = array();
$date_arr = array();
$percent_arr =  array();
$sql = ........;
$rsl = mysql_query($sql);
while($get = mysql_fetch_assoc($rsl)){
    $date_arr[] = $get['date'];
    $dept_id_arr[] = $get['department_id'];
    $percent_arr[] = $get['total_percent'];
}

当我用var_dump()打印这个数据时,我就是这样,

string(66) "["2015-11-17","2015-11-17","2015-11-18","2015-11-20","2015-11-23"]"

string(22) "["1","3","1","1","2"]"

string(46) "["0.5700","0.0000","0.5700","0.0000","0.5700"]" 

我想将此值更改为

[{
        "date": "2015-11-17",
        "department1": "0.5",
        "department2": "0.9",
        "department3": "4",
        "department4": "3",
    }, {
        "date": "2015-11-18",
        "department1": "0.5",
        "department2": "0",
        "department3": "0",
        "department4": "0",
    }, {
        "date": "2015-11-19",
        "department1": "0.5",
        "department2": "0.3",
        "department3": "5",
        "department4": "2",
    }]

因为我需要在c3js图表中显示该数据。但是我有问题,当我将数据更改为该格式时,因为某些日期在数组中相同。

1 个答案:

答案 0 :(得分:1)

您可以通过以下代码执行所有操作:

{{1}}