Mage_Adminhtml_Block_Dashboard_Graph Class Reference

Inheritance diagram for Mage_Adminhtml_Block_Dashboard_Graph:

Mage_Adminhtml_Block_Dashboard_Abstract Mage_Adminhtml_Block_Widget Mage_Adminhtml_Block_Template Mage_Core_Block_Template Mage_Core_Block_Abstract Varien_Object Mage_Adminhtml_Block_Dashboard_Tab_Amounts Mage_Adminhtml_Block_Dashboard_Tab_Orders

List of all members.

Public Member Functions

 __construct ()
 setDataRows ($rows)
 addSeries ($seriesId, array $options)
 getSeries ($seriesId)
 getAllSeries ()
 getChartUrl ($directUrl=true)
 setAxisLabels ($axis, $labels)
 setHtmlId ($htmlId)
 getHtmlId ()

Public Attributes

const API_URL = 'http://chart.apis.google.com/chart'

Protected Member Functions

 _getTabTemplate ()
 getRowsData ($attributes, $single=false)
 _getPow ($number)
 getWidth ()
 getHeight ()

Protected Attributes

 $_allSeries = array()
 $_axisLabels = array()
 $_axisMaps = array()
 $_dataRows = array()
 $_simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
 $_extendedEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.'
 $_width = '587'
 $_height = '300'
 $_encoding = 'e'
 $_htmlId = ''


Detailed Description

Definition at line 35 of file Graph.php.


Constructor & Destructor Documentation

__construct (  ) 

Constructor

By default is looking for first argument as array and assignes it as object attributes This behaviour may change in child classes

Reimplemented from Varien_Object.

Reimplemented in Mage_Adminhtml_Block_Dashboard_Tab_Amounts, and Mage_Adminhtml_Block_Dashboard_Tab_Orders.

Definition at line 55 of file Graph.php.

00056     {
00057         parent::__construct();
00058         $this->setTemplate('dashboard/graph.phtml');
00059     }


Member Function Documentation

_getPow ( number  )  [protected]

Definition at line 375 of file Graph.php.

00376     {
00377         $pow = 0;
00378         while ($number >= 10) {
00379             $number = $number/10;
00380             $pow++;
00381         }
00382         return $pow;
00383     }

_getTabTemplate (  )  [protected]

Definition at line 61 of file Graph.php.

00062     {
00063         return 'dashboard/graph.phtml';
00064     }

addSeries ( seriesId,
array options 
)

Definition at line 71 of file Graph.php.

00072     {
00073         $this->_allSeries[$seriesId] = $options;
00074     }

getAllSeries (  ) 

Definition at line 85 of file Graph.php.

00086     {
00087         return $this->_allSeries;
00088     }

getChartUrl ( directUrl = true  ) 

setting skip step

skipping some x labels for good reading

Format date

Definition at line 90 of file Graph.php.

00091     {
00092         $params = array(
00093             'cht' => 'lc',
00094             'chf' => 'bg,s,f4f4f4|c,lg,90,ffffff,0.1,ededed,0',
00095             'chm' => 'B,f4d4b2,0,0,0',
00096             'chco' => 'db4814'
00097         );
00098 
00099         $this->_allSeries = $this->getRowsData($this->_dataRows);
00100 
00101         foreach ($this->_axisMaps as $axis => $attr){
00102             $this->setAxisLabels($axis, $this->getRowsData($attr, true));
00103         }
00104 
00105         $gmtOffset = Mage::getSingleton('core/date')->getGmtOffset();
00106 
00107         list ($dateStart, $dateEnd) = Mage::getResourceModel('reports/order_collection')
00108             ->getDateRange($this->getDataHelper()->getParam('period'), '', '', true);
00109 
00110         $dates = array();
00111         $datas = array();
00112 
00113         while($dateStart->compare($dateEnd) < 0){
00114             switch ($this->getDataHelper()->getParam('period')) {
00115                 case '24h':
00116                     $d = $dateStart->toString('yyyy-MM-dd HH:00');
00117                     $dateStart->addHour(1);
00118                     break;
00119                 case '7d':
00120                 case '1m':
00121                     $d = $dateStart->toString('yyyy-MM-dd');
00122                     $dateStart->addDay(1);
00123                     break;
00124                 case '1y':
00125                 case '2y':
00126                     $d = $dateStart->toString('yyyy-MM');
00127                     $dateStart->addMonth(1);
00128                     break;
00129             }
00130             foreach ($this->getAllSeries() as $index=>$serie) {
00131                 if (in_array($d, $this->_axisLabels['x'])) {
00132                     $datas[$index][] = (float)array_shift($this->_allSeries[$index]);
00133                 } else {
00134                     $datas[$index][] = 0;
00135                 }
00136             }
00137             $dates[] = $d;
00138         }
00139 
00140         /**
00141          * setting skip step
00142          */
00143         if (count($dates) > 8 && count($dates) < 15) {
00144             $c = 1;
00145         } else if (count($dates) >= 15){
00146             $c = 2;
00147         } else {
00148             $c = 0;
00149         }
00150         /**
00151          * skipping some x labels for good reading
00152          */
00153         $i=0;
00154         foreach ($dates as $k => $d) {
00155             if ($i == $c) {
00156                 $dates[$k] = $d;
00157                 $i = 0;
00158             } else {
00159                 $dates[$k] = '';
00160                 $i++;
00161             }
00162         }
00163 
00164         $this->_axisLabels['x'] = $dates;
00165         $this->_allSeries = $datas;
00166 
00167         //Google encoding values
00168         if ($this->_encoding == "s") {
00169             // simple encoding
00170             $params['chd'] = "s:";
00171             $dataDelimiter = "";
00172             $dataSetdelimiter = ",";
00173             $dataMissing = "_";
00174         } else {
00175             // extended encoding
00176             $params['chd'] = "e:";
00177             $dataDelimiter = "";
00178             $dataSetdelimiter = ",";
00179             $dataMissing = "__";
00180         }
00181 
00182         // process each string in the array, and find the max length
00183         foreach ($this->getAllSeries() as $index => $serie) {
00184             $localmaxlength[$index] = sizeof($serie);
00185             $localmaxvalue[$index] = max($serie);
00186             $localminvalue[$index] = min($serie);
00187         }
00188 
00189         if (is_numeric($this->_max)) {
00190             $maxvalue = $this->_max;
00191         } else {
00192             $maxvalue = max($localmaxvalue);
00193         }
00194         if (is_numeric($this->_min)) {
00195             $minvalue = $this->_min;
00196         } else {
00197             $minvalue = min($localminvalue);
00198         }
00199 
00200         $maxlength = max($localmaxlength);
00201         if ($minvalue >= 0 && $maxvalue >= 0) {
00202             $miny = 0;
00203             if ($maxvalue > 10) {
00204                 $p = pow(10, $this->_getPow($maxvalue));
00205                 $maxy = (ceil($maxvalue/$p))*$p;
00206                 $yLabels = range($miny, $maxy, $p);
00207             } else {
00208                 $maxy = ceil($maxvalue+1);
00209                 $yLabels = range($miny, $maxy, 1);
00210             }
00211             $yrange = $maxy;
00212             $yorigin = 0;
00213         }
00214 
00215         $chartdata = array();
00216 
00217         foreach ($this->getAllSeries() as $index => $serie) {
00218             $thisdataarray = $serie;
00219             if ($this->_encoding == "s") {
00220                 // SIMPLE ENCODING
00221                 for ($j = 0; $j < sizeof($thisdataarray); $j++) {
00222                     $currentvalue = $thisdataarray[$j];
00223                     if (is_numeric($currentvalue)) {
00224                         $ylocation = round((strlen($this->_simpleEncoding)-1) * ($yorigin + $currentvalue) / $yrange);
00225                         array_push($chartdata, substr($this->_simpleEncoding, $ylocation, 1) . $dataDelimiter);
00226                     } else {
00227                         array_push($chartdata, $dataMissing . $dataDelimiter);
00228                     }
00229                 }
00230                 // END SIMPLE ENCODING
00231             } else {
00232                 // EXTENDED ENCODING
00233                 for ($j = 0; $j < sizeof($thisdataarray); $j++) {
00234                     $currentvalue = $thisdataarray[$j];
00235                     if (is_numeric($currentvalue)) {
00236                         if ($yrange) {
00237                          $ylocation = (4095 * ($yorigin + $currentvalue) / $yrange);
00238                         } else {
00239                           $ylocation = 0;
00240                         }
00241                         $firstchar = floor($ylocation / 64);
00242                         $secondchar = $ylocation % 64;
00243                         $mappedchar = substr($this->_extendedEncoding, $firstchar, 1) . substr($this->_extendedEncoding, $secondchar, 1);
00244                         array_push($chartdata, $mappedchar . $dataDelimiter);
00245                     } else {
00246                         array_push($chartdata, $dataMissing . $dataDelimiter);
00247                     }
00248                 }
00249                 // ============= END EXTENDED ENCODING =============
00250             }
00251             array_push($chartdata, $dataSetdelimiter);
00252         }
00253         $buffer = implode('', $chartdata);
00254 
00255         $buffer = rtrim($buffer, $dataSetdelimiter);
00256         $buffer = rtrim($buffer, $dataDelimiter);
00257         $buffer = str_replace(($dataDelimiter . $dataSetdelimiter), $dataSetdelimiter, $buffer);
00258 
00259         $params['chd'] .= $buffer;
00260 
00261         $labelBuffer = "";
00262         $valueBuffer = array();
00263         $rangeBuffer = "";
00264 
00265         if (sizeof($this->_axisLabels) > 0) {
00266             $params['chxt'] = implode(',', array_keys($this->_axisLabels));
00267             $indexid = 0;
00268             foreach ($this->_axisLabels as $idx=>$labels){
00269                 if ($idx == 'x') {
00270                     /**
00271                      * Format date
00272                      */
00273                     foreach ($this->_axisLabels[$idx] as $_index=>$_label) {
00274                         if ($_label != '') {
00275                             switch ($this->getDataHelper()->getParam('period')) {
00276                                 case '24h':
00277                                     $this->_axisLabels[$idx][$_index] = $this->formatTime($_label, 'short', false);
00278                                     break;
00279                                 case '7d':
00280                                 case '1m':
00281                                     $this->_axisLabels[$idx][$_index] = $this->formatDate($_label);
00282                                     break;
00283                                 case '1y':
00284                                 case '2y':
00285                                     $formats = Mage::app()->getLocale()->getTranslationList('datetime');
00286                                     $format = isset($formats['yyMM']) ? $formats['yyMM'] : 'MM/yyyy';
00287                                     $format = str_replace(array("yyyy", "yy", "MM"), array("Y", "y", "m"), $format);
00288                                     $this->_axisLabels[$idx][$_index] = date($format, strtotime($_label));
00289                                     break;
00290                             }
00291                         } else {
00292                             $this->_axisLabels[$idx][$_index] = '';
00293                         }
00294 
00295                     }
00296 
00297                     $tmpstring = implode('|', $this->_axisLabels[$idx]);
00298 
00299                     $valueBuffer[] = $indexid . ":|" . $tmpstring;
00300                     if (sizeof($this->_axisLabels[$idx]) > 1) {
00301                         $deltaX = 100/(sizeof($this->_axisLabels[$idx])-1);
00302                     } else {
00303                         $deltaX = 100;
00304                     }
00305                 } else if ($idx == 'y') {
00306                     $valueBuffer[] = $indexid . ":|" . implode('|', $yLabels);
00307                     if (sizeof($yLabels)-1) {
00308                         $deltaY = 100/(sizeof($yLabels)-1);
00309                     } else {
00310                         $deltaY = 100;
00311                     }
00312                     // setting range values for y axis
00313                     $rangeBuffer = $indexid . "," . $miny . "," . $maxy . "|";
00314                 }
00315                 $indexid++;
00316             }
00317             $params['chxl'] = implode('|', $valueBuffer);
00318         };
00319 
00320         // chart size
00321         $params['chs'] = $this->getWidth().'x'.$this->getHeight();
00322 
00323         if (isset($deltaX) && isset($deltaY)) {
00324             $params['chg'] = $deltaX . ',' . $deltaY . ',1,0';
00325         }
00326 
00327         // return the encoded data
00328         if ($directUrl) {
00329             $p = array();
00330             foreach ($params as $name => $value) {
00331                 $p[] = $name . '=' .urlencode($value);
00332             }
00333             return self::API_URL . '?' . implode('&', $p);
00334         } else {
00335             $params = array('ga' => urlencode(base64_encode(serialize($params))));
00336             return $this->getUrl('*/*/tunnel', $params);
00337         }
00338     }

getHeight (  )  [protected]

Definition at line 392 of file Graph.php.

00393     {
00394         return $this->_height;
00395     }

getHtmlId (  ) 

Reimplemented from Mage_Adminhtml_Block_Widget.

Definition at line 369 of file Graph.php.

00370     {
00371         return $this->_htmlId;
00372     }

getRowsData ( attributes,
single = false 
) [protected]

Definition at line 340 of file Graph.php.

00341     {
00342         $items = $this->getCollection()->getItems();
00343         $options = array();
00344         foreach ($items as $item){
00345             if ($single) {
00346                 $options[] = $item->getData($attributes);
00347             } else {
00348                 foreach ((array)$attributes as $attr){
00349                     $options[$attr][] = $item->getData($attr);
00350                 }
00351             }
00352         }
00353         return $options;
00354     }

getSeries ( seriesId  ) 

Definition at line 76 of file Graph.php.

00077     {
00078         if (isset($this->_allSeries[$seriesId])) {
00079             return $this->_allSeries[$seriesId];
00080         } else {
00081             return false;
00082         }
00083     }

getWidth (  )  [protected]

Definition at line 386 of file Graph.php.

00387     {
00388         return $this->_width;
00389     }

setAxisLabels ( axis,
labels 
)

Definition at line 357 of file Graph.php.

00358     {
00359         $this->_axisLabels[$axis] = $labels;
00360     }

setDataRows ( rows  ) 

Definition at line 66 of file Graph.php.

00067     {
00068         $this->_dataRows = (array)$rows;
00069     }

setHtmlId ( htmlId  ) 

Definition at line 363 of file Graph.php.

00364     {
00365         $this->_htmlId = $htmlId;
00366     }


Member Data Documentation

$_allSeries = array() [protected]

Definition at line 37 of file Graph.php.

$_axisLabels = array() [protected]

Definition at line 38 of file Graph.php.

$_axisMaps = array() [protected]

Definition at line 39 of file Graph.php.

$_dataRows = array() [protected]

Definition at line 41 of file Graph.php.

$_encoding = 'e' [protected]

Definition at line 51 of file Graph.php.

$_extendedEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.' [protected]

Definition at line 44 of file Graph.php.

$_height = '300' [protected]

Definition at line 49 of file Graph.php.

$_htmlId = '' [protected]

Definition at line 53 of file Graph.php.

$_simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' [protected]

Definition at line 43 of file Graph.php.

$_width = '587' [protected]

Definition at line 48 of file Graph.php.

const API_URL = 'http://chart.apis.google.com/chart'

Definition at line 46 of file Graph.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:22:49 2009 for Magento by  doxygen 1.5.8