GoogleChartData.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2008 by Denis M. Gabaidulin                             *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class GoogleChartData extends BaseGoogleChartData
00016     {
00017         protected $name = 'chd';
00018         
00019         private $sets = array();
00020         
00021         private $dataScaling = false;
00022         
00023         private $normalize = false;
00024         
00028         public static function create()
00029         {
00030             return new self;
00031         }
00032         
00036         public function setDataScaling($scale = true)
00037         {
00038             $this->dataScaling = (true === $scale);
00039             
00040             return $this;
00041         }
00042         
00043         public function withDataScaling()
00044         {
00045             return $this->dataScaling;
00046         }
00047         
00051         public function setNormalize($orly = true)
00052         {
00053             $this->normalize = (true === $orly);
00054             
00055             return $this;
00056         }
00057         
00058         public function isNormalized()
00059         {
00060             return $this->normalize;
00061         }
00062         
00066         public function addDataSet(GoogleChartDataSet $set)
00067         {
00068             $this->sets[] = $set;
00069             
00070             return $this;
00071         }
00072         
00073         public function getDataSetByIndex($index)
00074         {
00075             if (!isset($this->sets[$index]))
00076                 throw new WrongArgumentException(
00077                     "Dataset with index {$index} not found"
00078                 );
00079             
00080             return $this->sets[$index];
00081         }
00082         
00083         public function getCount()
00084         {
00085             return count($this->sets);
00086         }
00087         
00088         public function toString()
00089         {
00090             Assert::isNotNull($this->encoding, 'Data encdoing Required.');
00091             
00092             $boundString = null;
00093             
00094             $dataStrings = $bounds = array();
00095             
00096             if ($this->dataScaling)
00097                 $boundString = '&'.GoogleChartDataScaling::getParamName().'=';
00098             
00099             if ($this->normalize)
00100                 $this->normalize();
00101             
00102             foreach ($this->sets as $set) {
00103                 $this->encoding->setMaxValue($set->getMax() + 1);
00104                 $dataStrings[] = $this->encoding->encode($set);
00105                 
00106                 if ($this->dataScaling)
00107                     $bounds[] = $set->getMin().','.$set->getMax();
00108             }
00109             
00110             if ($this->dataScaling)
00111                 $boundString .= implode(',', $bounds);
00112             
00113             $dataString = implode('|', $dataStrings);
00114             
00115             $encodingString = $this->encoding->toString();
00116             
00117             return $this->name.'='.$encodingString.$dataString.$boundString;
00118         }
00119         
00120         public function getMaxSteps()
00121         {
00122             $max = 0;
00123             
00124             foreach ($this->sets as $set) {
00125                 if ($max < $set->getStepSize())
00126                     $max = $set->getStepSize();
00127             }
00128             
00129             return $max;
00130         }
00131         
00135         private function normalize()
00136         {
00137             $maxSteps = $this->getMaxSteps();
00138             
00139             foreach ($this->sets as $set) {
00140                 if ($maxSteps > $set->getStepSize())
00141                     $set->setMax(
00142                         $maxSteps * $set->getBase()
00143                     );
00144             }
00145             
00146             return $this;
00147         }
00148     }
00149 ?>