2011-10-05 65 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軸的位置,因爲您告訴它哪個值是軸。此外,您可以擁有任意數量的列。 –