生成嵌套的json格式,如nvd3 StackAreaChart数据

时间:2016-04-29 02:59:06

标签: javascript php json nvd3.js angular-nvd3

我试图制作嵌套的json nvd3 StackedAreaChart用途:

    [
        {
            "key" : "North America" ,
            "values" : [ [ 1025409600000 , 23.041422681023] , [ 1028088000000 , 19.854291255832] ]

        },

        {
            "key" : "Africa" ,
            "values" : [ [ 1025409600000 , 7.9356392949025] , [ 1028088000000 , 7.4514668527298] ]

        },

    ]

来源:http://plnkr.co/edit/CIGW0o?p=preview

我想使用数据库中的数据。

我怎样才能实现那种json?我对嵌套json不太熟悉,而且我在json的结构中注意到的一件事是values对象中的值是一个普通的整数。它没有像"1025409600000"那样引用。当我试图对它进行评估时,图表无法正确读取数据。

问题

  1. 如何生成类似json的嵌套nvd3用途?我进行了一些研究,但没有任何反应。我发现了一些像我想的那样,但是不能让它发挥作用。 Here以及此one

  2. 是否可以unquote来自嵌套json结构的值?如果是,怎么样?

  3. 这是我目前正在做的事情:

            <?php
    
            require_once('conn.php');
    
            $sql = "SELECT ua.user_id,(UNIX_TIMESTAMP(dt.transac_date)*1000) AS transac_date,
                            CONCAT(ui.fname,' ',ui.lname) AS fullname,
                            SUM((dt.item_price - dt.item_srp) * dt.sold) as profit,
                            SUM((dt.item_price) * dt.sold) as total_sales
                    FROM dsp_transactions dt
                    INNER JOIN user_acct ua ON dt.user_id=ua.user_id
                    INNER JOIN user_info ui ON ua.ui_id=ui.ui_id
                    GROUP BY ua.user_id";
            $qry = $con->query($sql);
    
            $data = array();
            if($qry->num_rows > 0) {
                while($row = $qry->fetch_object()) {
                    $data[] = array(
    
                        'key' => $row->fullname,
                        'values' => $row->user_id
    
                        );
                }
            } else {
                $data[] = null;
            }
    
            $con->close();
    
            echo json_encode($data);
    
            ?>
    

    这给了我这个价值:

    [{"key":"Juan Dela Cruz","values":["1461772800000","5665.00"]},{"key":"Maria Gonzales","values":["1461772800000","275.00"]},{"key":"Apolinario Mabini","values":["1461772800000","100.00"]}]
    

    提前致谢:)

    修改

    有关更多信息,我希望发生类似这样的事情:

             dsp      |    sales     |   profit    |     date    
        --------------+--------------+-------------+--------------
            Juan      |    500       |     100     |    04/24/2016
        --------------+--------------+-------------+--------------
            Maria     |    600       |     200     |    04/24/2016
        --------------+--------------+-------------+--------------
           Apolinario |    700       |     300     |    04/24/2016
        --------------+--------------+-------------+--------------
            Juan      |    550       |     150     |    04/25/2016
    

    将以json格式

    返回
        [
            {
                "key" : "Juan",
                "values" : [ ["04/24/2016", "500"], ["04/25/2016", "550"] ]  // "values" loop twice because "juan" has two sales
    
            },
            {
                "key" : "Maria",
                "values" : [ ["04/24/2016", "600"] ]
    
            },
            {
                "key" : "Apolinario",
                "values" : [ ["04/24/2016", "700"] ]
    
            }
        ]
    

2 个答案:

答案 0 :(得分:1)

返回的用户ID将转换为字符串。你可以使用php将它们转换回浮点数:

答案 1 :(得分:1)

我认为问题始于你如何处理查询返回的数据。据我所知,您希望将每个人的所有交易日期和销售(或利润)分组。因此,您需要在编码之前操纵数组。

看看这个代码片段是否运行,请告诉我是否会触发错误,但基本上,这是我看到的解决问题的逻辑:

<?php

require_once('conn.php');

$sql = "SELECT ua.user_id,(UNIX_TIMESTAMP(dt.transac_date)*1000) AS transac_date,
                CONCAT(ui.fname,' ',ui.lname) AS fullname,
                SUM((dt.item_price - dt.item_srp) * dt.sold) as profit,
                SUM((dt.item_price) * dt.sold) as total_sales
        FROM dsp_transactions dt
        INNER JOIN user_acct ua ON dt.user_id=ua.user_id
        INNER JOIN user_info ui ON ua.ui_id=ui.ui_id
        GROUP BY transac_date, ua.user_id";
$qry = $con->query($sql);

$data = array();
$individual_row = array();
if($qry->num_rows > 0) 
{
    while($row = pg_fetch_object($qry)) {
        //we find if there is an existing row with the person
        $indexOfIndividualRow = array_search($row->dsp, array_column($data, 'key'));
        //if no rows of the person are added yet
        if(empty($indexOfIndividualRow)&& !is_numeric($indexOfIndividualRow))
        {
            $individual_row['key'] = $row->dsp;
            $individual_row['values'] = array(array($row->date, $row->sales));
            array_push($data, $individual_row);
        }
        //if there is a row with person as key
        else 
        {
            //if there is a 'values' key
            if(isset($data[$indexOfIndividualRow]['values'])){
                array_push($data[$indexOfIndividualRow]['values'], array($row->date, $row->sales));
            }
            //else if there is no 'values' key
            else $data[$indexOfIndividualRow]['values'] = array($row->date, $row->sales);
        }
    }
}
else {
    $data[] = null;
}

$con->close();

echo json_encode($data);
?>