• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

survey/phplot/phplot_data.php

Go to the documentation of this file.
00001 <?php
00002 /* $Id: phplot_data.php 6558 2005-01-26 16:20:31Z smeyer $
00003  * 
00004  * Copyright (C) 2000 Afan Ottenheimer.  Released under
00005  * the GPL and PHP licenses as stated in the the README file which
00006  * should have been included with this document.
00007 
00008  * This is an subclass for phplot.php and should only be
00009  * called after phplot.ini has been called. This extends
00010  * phplot by adding additional routines that can be used
00011  * to modify the data arrays.
00012  *
00013  * Data must be a *numerical* array, this is enforced in SetDataValues() 
00014  */
00015 
00016 require_once("phplot.php");
00017 
00018 class PHPlot_Data extends PHPlot 
00019 {
00023     function PHPlot_Data($which_width=600, $which_height=400, $which_output_file=NULL, $which_input_file=NULL)
00024     { 
00025         if (! isset($this->img)) { 
00026             $this->PHPlot($which_width, $which_height, $which_output_file, $which_input_file);
00027         }
00028     }
00029     
00036     function DoScaleData($even, $show_in_legend) 
00037     {
00038         $offset = 0;        // We use this not to read labels in text-data
00039             
00040         if ($this->data_type == 'text-data') {
00041             $offset = 1;
00042         } elseif ($this->data_type != 'data-data') {
00043             $this->DrawError('wrong data type!!');
00044             return FALSE;
00045         }
00046 
00047         // Determine maxima for each data row in array $max
00048         // Put maximum of the maxima in $maxmax
00049         $maxmax = 0;
00050         for($i=0; $i < $this->num_data_rows; $i++) {
00051             $rowsize = count($this->data[$i]);
00052             for ($j=$offset; $j < $rowsize; $j++) {
00053                 if ($this->data[$i][$j] > @ $max[$j])
00054                     $max[$j] = $this->data[$i][$j];
00055                 if (@ $max[$j] > $maxmax) 
00056                     $maxmax = $max[$j];
00057             }
00058         }
00059         
00060         // determine amplification factor $amplify
00061         $end = count($max) + $offset;
00062         for ($i=$offset; $i < $end; $i++) {
00063             if ($max[$i] == 0 || $max[$i] == $maxmax) {
00064                 $amplify[$i] = 1;  // no divide by zero
00065             } else {
00066                 if ($even) {
00067                     $amp = pow(10,round(log10($maxmax / $max[$i]))-1);
00068                     if ($amp * $max[$i] * 5 < $maxmax) {
00069                         $amp *= 5;
00070                     } elseif ($amp * $max[$i] * 2 < $maxmax) {
00071                         $amp *= 2;
00072                     }
00073                 } else {
00074                     $amp = $maxmax / $max[$i];
00075                     $digits = floor(log10($amp));
00076                     $amp = round($amp/pow(10,$digits-1))*pow(10,$digits-1);
00077                 }
00078                 $amplify[$i] = $amp;
00079             }
00080             if ($amplify[$i] != 1 && $show_in_legend) 
00081                 @ $this->legend[$i] .= "*$amplify[$i]";
00082         }
00083 
00084         // Amplify data
00085         // On my machine, running 1000 iterations over 1000 rows of 12 elements each,
00086         // the for loops were 43.2% faster (MBD)
00087         for ($i = 0; $i < $this->num_data_rows; $i++) {
00088             $rowsize = count($this->data[$i]);
00089             for ($j=$offset; $j < $rowsize; $j++) {
00090                 $this->data[$i][$j] *= $amplify[$j];
00091             }
00092         }
00093 
00094         //Re-Scale Vertical Ticks if not already set
00095         if ( ! $this->y_tick_increment) {
00096             $this->SetYTickIncrement() ;
00097         }
00098 
00099         return TRUE;
00100     } //function DoScaleData
00101 
00102 
00117     function DoMovingAverage($datarow, $interval, $show=TRUE, $color=NULL, $width=NULL)
00118     {
00119         $off = 1;               // Skip record #0 (data label) 
00120         
00121         $this->PadArrays();
00122         
00123         if ($interval == 0) {
00124             $this->DrawError('DoMovingAverage(): interval can\'t be 0');
00125             return FALSE;
00126         }
00127 
00128         if ($datarow >= $this->records_per_group) {
00129             $this->DrawError("DoMovingAverage(): Data row out of bounds ($datarow >= $this->records_per_group)");
00130             return FALSE;
00131         }
00132         
00133         if ($this->data_type == 'text-data') {
00134             // Ok. No need to set the offset to skip more records.
00135         } elseif ($this->data_type == 'data-data') {
00136             $off++;             // first Y value at $data[][2]
00137         } else {
00138             $this->DrawError('DoMovingAverage(): wrong data type!!');
00139             return FALSE;
00140         }
00141         
00142         // Set color:
00143         if ($color) {
00144             array_push($this->ndx_data_colors, $this->SetIndexDarkColor($color));
00145         } else {
00146             array_push($this->ndx_data_colors, $this->SetIndexDarkColor($this->data_colors[$datarow]));
00147         }
00148         // Set line width:
00149         if ($width) {
00150             array_push($this->line_widths, $width);
00151         } else {    
00152             array_push($this->line_widths,  $this->line_widths[$datarow] * 2);
00153         }
00154         // Show in legend?
00155         if ($show) {
00156             $this->legend[$this->records_per_group-1] = "(MA[$datarow]:$interval)";
00157         }
00158 
00159         $datarow += $off;
00160         for ($i = 0; $i < $this->num_data_rows; $i++) {
00161             $storage[$i % $interval] = @ $this->data[$i][$datarow];
00162             $ma = array_sum($storage);
00163             $ma /= count($storage);
00164             array_push($this->data[$i], $ma);   // Push the data onto the array
00165             $this->num_recs[$i]++;              // Tell the drawing functions it is there
00166         }
00167         $this->records_per_group++;
00168 //        $this->FindDataLimits();
00169         return TRUE;
00170     } //function DoMovingAverage()
00171 
00172 
00178     function DoExponentialMovingAverage($datarow, $perc, $show_in_legend)
00179     {
00180         if ($this->data_type == 'text-data') {
00181             $datarow++;
00182         } elseif ($this->data_type != 'data-data') {
00183             $this->DrawError('DoWeightedMovingAverage(): wrong data type!!');
00184             return FALSE;
00185         }
00186         
00187         if ($show_in_legend) {
00188             $this->legend[$datarow] .= " (MA: $interval)";
00189         }
00190 
00191         $storage[0] = $this->data[0][$datarow];
00192         for ($i=1;$i < $this->num_data_rows; $i++) {
00193             $storage[$i] = @ $storage[$i-1] + $perc * ($this->data[$i][$datarow] - $storage[$i-1]);
00194             $ma = array_sum($storage);
00195             $ma /= count($storage);
00196             $this->data[$i][$datarow] = $ma;
00197         }
00198         return TRUE;
00199     } // function DoExponentialMovingAverage()
00200 
00201     
00205     function DoRemoveDataSet($index) 
00206     {
00207         $offset = 1;
00208         if ($this->data_type == 'data-data') {
00209             $offset++;
00210         } elseif ($this->data_type != 'text-data') {
00211             $this->DrawError('wrong data type!!');
00212             return FALSE;
00213         }
00214     
00215         $index += $offset;
00216         foreach ($this->data as $key=>$val) {
00217             foreach ($val as $key2=>$val2) {
00218                 if ($key2 >= $index) {
00219                     if (isset($this->data[$key][$key2+1])) {
00220                         $this->data[$key][$key2] = $this->data[$key][$key2+1];
00221                     } else {
00222                         unset($this->data[$key][$key2]);
00223                     }
00224                 }
00225             }
00226         }
00227     } // function DoRemoveDataSet
00228     
00229     
00234     function DoDivision($x,$y) 
00235     {
00236         $offset = 1;
00237         if ($this->data_type == 'data-data') {
00238             $offset++;
00239         } elseif ($this->data_type != 'text-data') {
00240             $this->DrawError('wrong data type!!');
00241             return FALSE;
00242         }
00243     
00244         $x += $offset; $y += $offset;
00245         reset($this->data);
00246         while (list($key, $val) = each($this->data)) {
00247             if ($this->data[$key][$y] == 0) {
00248                 $this->data[$key][$x] = 0;
00249             } else {
00250                 $this->data[$key][$x] /= $this->data[$key][$y];
00251             }
00252         }
00253     
00254         $this->DoRemoveDataSet($y-$offset);
00255     } // function DoDivision
00256 
00257 } // class PHPlot_Data extends PHPlot
00258 ?>

Generated on Fri Dec 13 2013 09:06:39 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1