2016-04-29 74 views
0

我想产生一个嵌套jsonnvd3 StackedAreaChart用途:产生这样nvd3 StackAreaChart数据嵌套的JSON格式

[ 
     { 
      "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. 如何产生嵌套jsonnvd3用途?我已经进行了一些研究,但没有任何反应。我发现了一些像我猜想的那样,但无法让它工作。 Here和这个one

  2. 是否有可能从json嵌套结构unquote?如果是,如何?

这就是我目前的工作:

 <?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"] ] 

     } 
    ] 

回答

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); 
?> 
+0

感谢您的回复。请参阅我上面的编辑。 –

+0

你试过我的代码吗?我认为这就是我的解决方案想要输出的内容。 –

+0

是的。我刚刚尝试过。但只返回'values'中的第一个循环。我认为这是因为我的'query'只能得到'user's id',而我使用'GROUP BY'。它没有得到“胡安”的两个日期。任何想法如何解决我的'查询'与您的代码?我已经试过任何东西只是为了满足你的代码输出,但不能得到它的工作。 –