2011-10-05 94 views
1

想象每一行作为X轴和每列作为一系列(在图表中的线):来自mysql的PHP结果集设置为图表系列?

--------------------------------- 
year | apple_price | banana_price 
--------------------------------- 
2010 2.5   1.7 
2011 2.6   2.0 

array // MySQL results set 
    0 => 
    array 
     'year' => 2010 
     'apple_price' => 2.5 
     'banana_price' => 1.7 
    1 => 
    array 
     'year' => 2011 
     'apple_price' => 2.6 
     'banana_price' => 2.0 

我需要MySQL的阵列变换的东西更适合于我的图表API和JSON。你将如何实现这一点?将可能转置结果集? EDIT这当然应该是动态的:第一列是X轴,保持将是系列(在图表中线)

array // indexed 
    0 => 
    array // associative 
     'name' => 'apple_price' 
     'data' => [[2010, 2.5], [2011, 2.6]] // indexed array of array 
    1 => 
    array // associative 
     'name' => 'banana_price' 
     'data' => [[2010, 1.7], [2011, 2.0]] // indexed array of array 

EDIT:第一快速和肮脏的工作溶液,试图获得一个更好的:

public static function mysqlResultSetToChartSeries($data) 
{ 
    // Return empty array if 0 rows or columns < 2 
    if (!isset($data[0]) || !count($data[0]) > 1) return array(); 

    // Get all keys (columns names) 
    $keys = array_keys($data[0]); 

    // Temp array for storing col names and values 
    $tmp = array(); 
    foreach($keys as $k) $tmp[$k] = array_map(function($r) use ($k){ 
     return $r[$k]; 
    }, $data); 

    // X axis 
    $x = array_shift($tmp); 

    $series = array_map(function($k, $v) use ($x) { 
     return array(
      'name' => $k, 
      'data' => array_map(function($xaxis, $yaxis) use ($x) { 
       return array($xaxis, $yaxis); 
      }, array_values($x), $v) 
     ); 
    }, array_keys($tmp), array_values($tmp)); 

    return $series; 
} 
+0

确实是这样的格式,您希望它?如果是这样,那根本就不会那么难。我只是想确定那就是你想要的。 –

+0

@LeviMorrison是的,是的。我知道,它应该很容易,但在编写丑陋的嵌套循环之前,我正在寻找一些默认函数来转置结果集。感谢您的帮助。 – gremo

+0

我不确定这是否是您想要存储数据的方式,但不会有预定义的函数(不是我发现的)。我建议你为此使用正式的PHP类。我们在PHP世界中太懒惰了。 –

回答

0

类:

<?php 
class ChartData { 

    private $data; 
    private $columns; 
    private $xAxis; 

    /** 
    * @param string $xAxis The key of the array that is the x-axis. 
    * @param array $columns An array of column names. 
    */ 
    public function __construct($xAxis, array $columns) { 
      $this->data = array(); 
     $this->xAxis = $xAxis; 

     //we need to build an index to know how to get 
     //from a key to an index. 
     $i = 0; 
     foreach ($columns as &$key) { 
      if ($key != $xAxis) { 
       $this->columns[$key] = $i++; 
      } 
     } 
     foreach ($this->columns as $key => &$value) { 
      $this->data[$value] = array(
       'name' => $key, 
       'data' => array() 
      ); 
     } 
    } 

    /** 
    * @param array $data An associative array mapping data names to values. Must include the index 'year' and otherwise match $columns 
     */ 
    public function add(array $data) { 
     foreach ($data as $key => &$value) { 
      if ($key != $this->xAxis) { 
       $this->data[$this->columns[$key]]['data'][] = array($data[$this->xAxis], $value); 
      } 
     } 
    } 

    /** 
    * @return array An associative array in the format for the Charts API. 
    */ 
    public function getArray() { 
     return $this->data; 
    } 

} 
?> 

使用方法:

$pricesData = array(
    array(
     'year' => '2010', 
     'jamba' => '2.5', 
     'juice' => '1.7' 
    ), 
    array(
     'year' => '2011', 
     'jamba' => '2.6', 
     'juice' => '2.0' 
    ) 
); 

$prices = new ChartData('year', array('year', 'jamba', 'juice')); 
foreach ($pricesData as &$priceData) { 
    $prices->add($priceData); 
} 
print_r($prices->getArray()); 

注:

  • 我的实现需要你告诉它x轴的将是什么。你这样做在构造函数
  • 它是动态的。你可以有任意多的列,只要你想..
  • 它的目的是一次构建一个记录。这意味着你要么:
    • 传递数据库抓取循环中的每条记录。 (推荐用于性能)
    • 循环遍历预构建数组并传递每个值。 (图示的例子中,不是很有效,decouples类好听)
+0

对不起,误解了。我忘了说全部应该是动态的,我不能认为列名是'年','apple_price'或'banana_price'。查询可以改变,但第一列将始终是X轴(这就是为什么在结果数组中没有年份)。剩余的列是系列,意味着它们是图表中的行。去这里:http://www.highcharts.com/demo/看看代码:你会看到系列数组。 – gremo

+0

@Gremo现在我设置它的方式,它不会影响x轴的位置,因为您告诉它哪个值是轴。此外,您可以拥有任意数量的列。 –