使用相同的密钥PHP MySQL D3追加值

时间:2015-07-28 03:01:46

标签: javascript php mysql d3.js

这可能是一个愚蠢的问题,但我正在努力解决这个问题。

我需要将数据从mysql数据库转换为d3折线图。

我该怎么做:

[{"key":"Branch1","values":["21","1961"]},{"key":"Branch2","values":["21","1961"]},{"key":"Branch2","values":["22","20040"]}]

进入这个:

[{"key":"Branch1","values":["21","1961"]},{"key":"Branch2","values":[["21","1961"],["22","20040"]]}]

我有一个PHP函数可以创建第一个结果。这是函数

function getChartData(){
$currentDate = $this->getCurrentDate();
$query = "select t2.name as 'office', DAY(t1.datetime_created) as 'day', t1.datetime_created as 'date', SUM(t1.amount) as 'amount' FROM money_main_records t1, offices t2 WHERE t1.id_branch = t2.office_id AND MONTH(t1.datetime_created) = ". $currentDate['month_number'] . " GROUP BY t2.name, DAY(t1.datetime_created)";
$result =$this->db->query($query);
$series=array();
foreach ($result->result() as $item){
  $serie= array (
     "key" => $item->office,
     "values" => array (
          $item->day,
          $item->amount
      )
  );
  array_push($series,$serie);
}
return $series;
}

非常感谢任何帮助!谢谢!

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

$data = [];
foreach ($result->result() as $item){
    // Group offices
    $data[$item->office][] = [
                  $item->day,
                  $item->amount
              ];

}

$series = [];
// Browse all offices
foreach($data as $officeKey=>$officeArrays){
    $series[] = ['key'=>$officeKey, 'values'=> $officeArrays];
}

return $series;

无法测试我的代码,它可能不是最佳解决方案,但我希望它能为您提供帮助。

答案 1 :(得分:0)

你可以用jQuery做这样的事情。



var data = [{
  "key": "Branch1",
  "values": ["21", "1961"]
}, {
  "key": "Branch2",
  "values": ["21", "1961"]
}, {
  "key": "Branch2",
  "values": ["22", "20040"]
}];

$('#data').html(JSON.stringify(data));

var groupedData = [];

// Iterate through your data
$.each(data, function(index, item) {
  if (groupedData.length > 0) {
    // Check if item exists
    var itemExists = $.grep(groupedData, function(groupItem, groupIndex) {
      return groupItem.key == item.key;
    });

    // If item doesn't exist, add it
    if (itemExists.length == 0) {
      groupedData.push({
        key: item.key,
        values: [item.values]
      });
    }
    // Otherwise, append it to existing item's values array
    else {
      var valuesToAdd = [];
      $.each(item.values, function(valIndex, value) {
        valuesToAdd.push(value);
      });

      itemExists[0].values.push(valuesToAdd);
    }
  } else {
    groupedData.push({
      key: item.key,
      values: [item.values]
    });
  }
});

$('#groupedData').html(JSON.stringify(groupedData));

console.log('-- Grouped --');
console.log(groupedData);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Your JSON:
<br/>
<br/>
<div id='data'></div>

<hr/>JSON You want:
<br/>
<br/>
<div id='groupedData'></div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

也许是这样的:

$branches = array();

foreach(getChartData() as $branch)
{
   if(empty($branches[$branch['key']]))
    $branches[$branch['key']] = array();

   $branches[$branch['key']] = array_merge($branches[$branch['key']],$branch['values']);
}

echo json_encode(array($branches));

输出以下内容:

[{"Branch1":["21","1961"],"Branch2":["21","1961","22","20040"]}]

或者也许这样呢? :

$branches = array();

foreach(getChartData() as $branch)
{
   if(empty($branches[$branch['key']]))
    $branches[$branch['key']] = array();

   $branches[$branch['key']][] = $branch['values'];
}

echo json_encode(array($branches));

输出:

[{"Branch1":[["21","1961"]],"Branch2":[["21","1961"],["22","20040"]]}]