Advertisement
SkullCrack

/Graf/ jpgraph_line.php

Jun 27th, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.62 KB | None | 0 0
  1. <?php
  2. /*=======================================================================
  3. // File:    JPGRAPH_LINE.PHP
  4. // Description: Line plot extension for JpGraph
  5. // Created:     2001-01-08
  6. // Author:  Johan Persson (johanp@aditus.nu)
  7. // Ver:     $Id: jpgraph_line.php,v 1.22 2002/04/17 09:31:46 aditus Exp $
  8. //
  9. // License: This code is released under QPL
  10. // Copyright (C) 2001,2002 Johan Persson
  11. //========================================================================
  12. */
  13.  
  14. // constants for the (filled) area
  15. DEFINE("LP_AREA_FILLED", true);
  16. DEFINE("LP_AREA_NOT_FILLED", false);
  17. DEFINE("LP_AREA_BORDER",false);
  18. DEFINE("LP_AREA_NO_BORDER",true);
  19.  
  20. //===================================================
  21. // CLASS LinePlot
  22. // Description:
  23. //===================================================
  24. class LinePlot extends Plot{
  25.     var $filled=false;
  26.     var $fill_color;
  27.     var $mark=null;
  28.     var $step_style=false, $center=false;
  29.     var $line_style=1;  // Default to solid
  30.     var $filledAreas = array(); // array of arrays(with min,max,col,filled in them)
  31.  
  32.  
  33. //---------------
  34. // CONSTRUCTOR
  35.     function LinePlot(&$datay,$datax=false) {
  36.     $this->Plot($datay,$datax);
  37.     $this->mark = new PlotMark();
  38.     $this->mark->SetColor($this->color);
  39.     }
  40. //---------------
  41. // PUBLIC METHODS  
  42.  
  43.     // Set style, filled or open
  44.     function SetFilled($f=true) {
  45.     $this->filled=$f;
  46.     }
  47.    
  48.     function SetStyle($s) {
  49.     $this->line_style=$s;
  50.     }
  51.    
  52.     function SetStepStyle($f=true) {
  53.     $this->step_style = $f;
  54.     }
  55.    
  56.     function SetColor($c) {
  57.     parent::SetColor($c);
  58.     $this->mark->SetColor($this->color);
  59.     }
  60.    
  61.     function SetFillColor($c,$f=true) {
  62.     $this->fill_color=$c;
  63.     $this->filled=$f;
  64.     }
  65.    
  66.     function Legend(&$graph) {
  67.     if( $this->legend!="" ) {
  68.         if( $this->filled ) {
  69.         $graph->legend->Add($this->legend,
  70.         $this->fill_color,$this->mark);
  71.         } else {
  72.         $graph->legend->Add($this->legend,
  73.         $this->color,$this->mark,$this->line_style);
  74.         }
  75.     }  
  76.     }
  77.    
  78.     function SetCenter($c=true) {
  79.     $this->center=$c;
  80.     }  
  81.  
  82.     function AddArea($aMin=0,$aMax=0,$aFilled=LP_AREA_NOT_FILLED,$aColor="gray9",$aBorder=LP_AREA_BORDER) {
  83.       if($aMin > $aMax) {
  84.     // swap
  85.     $tmp = $aMin;
  86.     $aMin = $aMax;
  87.     $aMax = $tmp;
  88.       }
  89.       $this->filledAreas[] = array($aMin,$aMax,$aColor,$aFilled,$aBorder);
  90.     }
  91.    
  92.     // Gets called before any axis are stroked
  93.     function PreStrokeAdjust(&$graph) {
  94.     if( $this->center ) {
  95.         ++$this->numpoints;
  96.         $a=0.5; $b=0.5;
  97.     } else {
  98.         $a=0; $b=0;
  99.     }
  100.     $graph->xaxis->scale->ticks->SetXLabelOffset($a);
  101.     $graph->SetTextScaleOff($b);                       
  102.     $graph->xaxis->scale->ticks->SupressMinorTickMarks();
  103.     }
  104.    
  105.     function Stroke(&$img,&$xscale,&$yscale) {
  106.     $numpoints=count($this->coords[0]);
  107.     if( isset($this->coords[1]) ) {
  108.         if( count($this->coords[1])!=$numpoints )
  109.         JpGraphError::Raise("Number of X and Y points are not equal. Number of X-points:".count($this->coords[1])." Number of Y-points:$numpoints");
  110.         else
  111.         $exist_x = true;
  112.     }
  113.     else
  114.         $exist_x = false;
  115.  
  116.     if( $exist_x )
  117.         $xs=$this->coords[1][0];
  118.     else
  119.         $xs=0;
  120.  
  121.     $img->SetStartPoint($xscale->Translate($xs),
  122.                         $yscale->Translate($this->coords[0][0]));
  123.        
  124.     if( $this->filled ) {
  125.         $cord[] = $xscale->Translate($xs);
  126.         $cord[] = $yscale->Translate($yscale->GetMinVal());
  127.     }
  128.     $xt = $xscale->Translate($xs);
  129.     $yt = $yscale->Translate($this->coords[0][0]);
  130.     $cord[] = $xt;
  131.     $cord[] = $yt;
  132.     $yt_old = $yt;
  133.  
  134.     $this->value->Stroke($img,$this->coords[0][0],$xt,$yt);
  135.  
  136.     $img->SetColor($this->color);
  137.     $img->SetLineWeight($this->weight);
  138.     $img->SetLineStyle($this->line_style);
  139.     for( $pnts=1; $pnts<$numpoints; ++$pnts) {
  140.         if( $exist_x ) $x=$this->coords[1][$pnts];
  141.         else $x=$pnts;
  142.         $xt = $xscale->Translate($x);
  143.         $yt = $yscale->Translate($this->coords[0][$pnts]);
  144.        
  145.         if( $this->step_style ) {
  146.         $img->StyleLineTo($xt,$yt_old);
  147.         $img->StyleLineTo($xt,$yt);
  148.  
  149.         $cord[] = $xt;
  150.         $cord[] = $yt_old;
  151.    
  152.         $cord[] = $xt;
  153.         $cord[] = $yt;
  154.  
  155.         }
  156.         else {
  157.  
  158.         $cord[] = $xt;
  159.         $cord[] = $yt;
  160.                        
  161.         $y=$this->coords[0][$pnts];
  162.         if( is_numeric($y) || (is_string($y) && $y != "-") ) {                 
  163.             $tmp1=$this->coords[0][$pnts];
  164.             $tmp2=$this->coords[0][$pnts-1];                   
  165.             if( is_numeric($tmp1)  && (is_numeric($tmp2) || $tmp2=="-" ) ) {
  166.             $img->StyleLineTo($xt,$yt);
  167.             }
  168.             else {
  169.             $img->SetStartPoint($xt,$yt);
  170.             }
  171.         }
  172.         }
  173.         $yt_old = $yt;
  174.  
  175.         $this->StrokeDataValue($img,$this->coords[0][$pnts],$xt,$yt);
  176.  
  177.     }  
  178.     if( $this->filled ) {
  179.         $cord[] = $xt;
  180.         $cord[] = $yscale->Translate($yscale->GetMinVal());                
  181.         $img->SetColor($this->fill_color); 
  182.         $img->FilledPolygon($cord);
  183.         $img->SetColor($this->color);
  184.         $img->Polygon($cord);
  185.     }
  186.    
  187.     if(!empty($this->filledAreas)) {
  188.  
  189.       $minY = $yscale->Translate($yscale->GetMinVal());
  190.       $factor = ($this->step_style ? 4 : 2);
  191.  
  192.       for($i = 0; $i < sizeof($this->filledAreas); ++$i) {
  193.         // go through all filled area elements ordered by insertion
  194.         // fill polygon array
  195.         $areaCoords[] = $cord[$this->filledAreas[$i][0] * $factor];
  196.         $areaCoords[] = $minY;
  197.  
  198.         $areaCoords =
  199.           array_merge($areaCoords,
  200.               array_slice($cord,
  201.                       $this->filledAreas[$i][0] * $factor,
  202.                       ($this->filledAreas[$i][1] - $this->filledAreas[$i][0] + ($this->step_style ? 0 : 1))  * $factor));
  203.         $areaCoords[] = $areaCoords[sizeof($areaCoords)-2]; // last x
  204.         $areaCoords[] = $minY; // last y
  205.        
  206.         if($this->filledAreas[$i][3]) {
  207.           $img->SetColor($this->filledAreas[$i][2]);
  208.           $img->FilledPolygon($areaCoords);
  209.           $img->SetColor($this->color);
  210.         }
  211.        
  212.         $img->Polygon($areaCoords);
  213.         $areaCoords = array();
  214.       }
  215.     }  
  216.  
  217.     $adjust=0;
  218.     if( $this->filled ) $adjust=2;
  219.     $factor = 1;
  220.     if( $this->step_style ) $factor = 2;
  221.     $this->csimareas="";
  222.     for($i=$adjust; $i<count($cord)/$factor-$adjust; $i+=2) {
  223.       if( is_numeric($this->coords[0][($i-$adjust)/2]) ) {
  224.         $xt=$cord[$i*$factor];
  225.         $yt=$cord[$i*$factor+1];
  226.             if( !empty($this->csimtargets[$i]) ) {
  227.           $this->mark->SetCSIMTarget($this->csimtargets[$i]);
  228.           $this->mark->SetCSIMAlt($this->csimalts[$i]);
  229.           $this->mark->SetCSIMAltVal($this->coords[0][$i]);
  230.         }
  231.         $this->mark->Stroke($img,$xt,$yt); 
  232.         $this->csimareas .= $this->mark->GetCSIMAreas();
  233.        
  234.         $this->mark->Stroke($img,$xt,$yt);
  235.       }
  236.     }
  237.     }
  238. } // Class
  239.  
  240.  
  241. //===================================================
  242. // CLASS AccLinePlot
  243. // Description:
  244. //===================================================
  245. class AccLinePlot extends Plot {
  246.     var $plots=null,$nbrplots=0,$numpoints=0;
  247. //---------------
  248. // CONSTRUCTOR
  249.     function AccLinePlot($plots) {
  250.         $this->plots = $plots;
  251.     $this->nbrplots = count($plots);
  252.     $this->numpoints = $plots[0]->numpoints;       
  253.     }
  254.  
  255. //---------------
  256. // PUBLIC METHODS  
  257.     function Legend(&$graph) {
  258.     foreach( $this->plots as $p )
  259.         $p->Legend($graph);
  260.     }
  261.    
  262.     function Max() {
  263.     $accymax=0;
  264.     list($xmax,$dummy) = $this->plots[0]->Max();
  265.     foreach($this->plots as $p) {
  266.         list($xm,$ym) = $p->Max();
  267.         $xmax = max($xmax,$xm);
  268.         $accymax += $ym;
  269.     }
  270.     return array($xmax,$accymax);
  271.     }
  272.  
  273.     function Min() {
  274.     list($xmin,$ymin)=$this->plots[0]->Min();
  275.     foreach( $this->plots as $p ) {
  276.         list($xm,$ym)=$p->Min();
  277.         $xmin=Min($xmin,$xm);
  278.         $ymin=Min($ymin,$ym);
  279.     }
  280.     return array($xmin,$ymin); 
  281.     }
  282.  
  283.     // To avoid duplicate of line drawing code here we just
  284.     // change the y-values for each plot and then restore it
  285.     // after we have made the stroke. We must do this copy since
  286.     // it wouldn't be possible to create an acc line plot
  287.     // with the same graphs, i.e AccLinePlot(array($pl,$pl,$pl));
  288.     // since this method would have a side effect.
  289.     function Stroke(&$img,&$xscale,&$yscale) {
  290.     $img->SetLineWeight($this->weight);
  291.     // Allocate array
  292.     $coords[$this->nbrplots][$this->numpoints]=0;
  293.     for($i=0; $i<$this->numpoints; $i++) {
  294.         $coords[0][$i]=$this->plots[0]->coords[0][$i];
  295.         $accy=$coords[0][$i];
  296.         for($j=1; $j<$this->nbrplots; ++$j ) {
  297.         $coords[$j][$i] = $this->plots[$j]->coords[0][$i]+$accy;
  298.         $accy = $coords[$j][$i];
  299.         }
  300.     }
  301.     for($j=$this->nbrplots-1; $j>=0; --$j) {
  302.         $p=$this->plots[$j];
  303.         for( $i=0; $i<$this->numpoints; ++$i) {
  304.         $tmp[$i]=$p->coords[0][$i];
  305.         $p->coords[0][$i]=$coords[$j][$i];
  306.         }
  307.         $p->Stroke($img,$xscale,$yscale);
  308.         for( $i=0; $i<$this->numpoints; ++$i)
  309.         $p->coords[0][$i]=$tmp[$i];
  310.         $p->coords[0][]=$tmp;
  311.     }
  312.     }
  313. } // Class
  314.  
  315.  
  316. /* EOF */
  317. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement