linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file BarPlot.class.php

Documentation is available at BarPlot.class.php

  1. <?php
  2. /*
  3.  * This work is hereby released into the Public Domain.
  4.  * To view a copy of the public domain dedication,
  5.  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
  6.  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  7.  *
  8.  */
  9.  
  10. require_once dirname(__FILE__)."/Plot.class.php";
  11.  
  12. /**
  13.  * BarPlot
  14.  *
  15.  * @package linea21.externals
  16.  * @subpackage artichow
  17.  */
  18. class awBarPlot extends awPlot {
  19.  
  20.   /**
  21.    * Labels on your bar plot
  22.    *
  23.    * @var Label 
  24.    */
  25.   var $label;
  26.  
  27.   /**
  28.    * Bar plot identifier
  29.    *
  30.    * @var int 
  31.    */
  32.   var $identifier;
  33.  
  34.   /**
  35.    * Bar plot number
  36.    *
  37.    * @var int 
  38.    */
  39.   var $number;
  40.  
  41.   /**
  42.    * Bar plot depth
  43.    *
  44.    * @var int 
  45.    */
  46.   var $depth;
  47.  
  48.   /**
  49.    * For moving bars
  50.    *
  51.    * @var int 
  52.    */
  53.   var $move;
  54.  
  55.   /**
  56.    * Bars shadow
  57.    *
  58.    * @var Shadow 
  59.    */
  60.   var $barShadow;
  61.  
  62.   /**
  63.    * Bars border
  64.    *
  65.    * @var Border 
  66.    */
  67.   var $barBorder;
  68.  
  69.   /**
  70.    * Bars padding
  71.    *
  72.    * @var Side 
  73.    */
  74.   var $barPadding;
  75.  
  76.   /**
  77.    * Bars space
  78.    *
  79.    * @var int 
  80.    */
  81.   var $barSpace = 0;
  82.  
  83.   /**
  84.    * Bars background
  85.    *
  86.    * @var Color, Gradient
  87.    */
  88.   var $barBackground;
  89.    
  90.   /**
  91.    * Construct a new awBarPlot
  92.    *
  93.    * @param array $values Some numeric values for Y axis
  94.    * @param int $identifier Plot identifier
  95.    * @param int $number Bar plot number
  96.    * @param int $depth Bar plot depth in pixels
  97.    */
  98.   function awBarPlot($values, $identifier = 1, $number = 1, $depth = 0) {
  99.  
  100.     parent::awPlot();
  101.  
  102.     $this->label = new awLabel;
  103.  
  104.     $this->barPadding = new awSide(0.08, 0.08, 0, 0);
  105.     $this->barShadow = new awShadow(SHADOW_RIGHT_TOP);
  106.     $this->barBorder = new awBorder;
  107.  
  108.     $this->setValues($values);
  109.  
  110.     $this->identifier = (int)$identifier;
  111.     $this->number = (int)$number;
  112.     $this->depth = (int)$depth;
  113.  
  114.     $this->move = new awSide;
  115.  
  116.     // Hide vertical grid
  117.     $this->grid->hideVertical(TRUE);
  118.  
  119.   }
  120.  
  121.   /**
  122.    * Change bars padding
  123.    * This method is not compatible with awBarPlot::setBarPadding()
  124.    *
  125.    * @param float $left Left padding (between 0 and 1)
  126.    * @param float $right Right padding (between 0 and 1)
  127.    */
  128.   function setBarPadding($left = NULL, $right = NULL) {
  129.     $this->barPadding->set($left, $right);
  130.   }
  131.  
  132.   /**
  133.    * Change bars size
  134.    * This method is not compatible with awBarPlot::setBarPadding()
  135.    *
  136.    * @param int $width Bars size (between 0 and 1)
  137.    */
  138.   function setBarSize($size) {
  139.     $padding = (1 - $size) / 2;
  140.     $this->barPadding->set($padding, $padding);
  141.   }
  142.  
  143.   /**
  144.    * Move bars
  145.    *
  146.    * @param int $x 
  147.    * @param int $y 
  148.    */
  149.   function move($x, $y) {
  150.     $this->move->set($x, NULL, $y, NULL);
  151.   }
  152.  
  153.   /**
  154.    * Change bars space
  155.    *
  156.    * @param int $space Space in pixels
  157.    */
  158.   function setBarSpace($space) {
  159.     $this->barSpace = (int)$space;
  160.   }
  161.  
  162.   /**
  163.    * Change line background color
  164.    *
  165.    * @param $color 
  166.    */
  167.   function setBarColor($color) {
  168.     $this->barBackground = $color;
  169.   }
  170.  
  171.   /**
  172.    * Change line background gradient
  173.    *
  174.    * @param $gradient 
  175.    */
  176.   function setBarGradient($gradient) {
  177.     $this->barBackground = $gradient;
  178.   }
  179.  
  180.   /**
  181.    * Get the line thickness
  182.    *
  183.    * @return int 
  184.    */
  185.   function getLegendLineThickness() {
  186.   }
  187.  
  188.   /**
  189.    * Get the line type
  190.    *
  191.    * @return int 
  192.    */
  193.   function getLegendLineStyle() {
  194.   }
  195.  
  196.   /**
  197.    * Get the color of line
  198.    *
  199.    * @return Color 
  200.    */
  201.   function getLegendLineColor() {
  202.   }
  203.  
  204.   /**
  205.    * Get the background color or gradient of an element of the component
  206.    *
  207.    * @return Color, Gradient
  208.    */
  209.   function getLegendBackground() {
  210.     return $this->barBackground;
  211.   }
  212.  
  213.   /**
  214.    * Get a mark object
  215.    *
  216.    * @return Mark 
  217.    */
  218.   function getLegendMark() {
  219.   }
  220.  
  221.   function drawComponent($driver, $x1, $y1, $x2, $y2, $aliasing) {
  222.  
  223.     $count = count($this->datay);
  224.     $max = $this->getRealYMax(NULL);
  225.     $min = $this->getRealYMin(NULL);
  226.  
  227.     // Find zero for bars
  228.     if($this->xAxisZero and $min <= 0 and $max >= 0) {
  229.       $zero = 0;
  230.     } else if($max < 0) {
  231.       $zero = $max;
  232.     } else {
  233.       $zero = $min;
  234.     }
  235.  
  236.     // Get base position
  237.     $zero = awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint(0, $zero));
  238.  
  239.     // Distance between two values on the graph
  240.     $distance = $this->xAxis->getDistance(0, 1);
  241.  
  242.     // Compute paddings
  243.     $leftPadding = $this->barPadding->left * $distance;
  244.     $rightPadding = $this->barPadding->right * $distance;
  245.  
  246.     $padding = $leftPadding + $rightPadding;
  247.     $space = $this->barSpace * ($this->number - 1);
  248.  
  249.     $barSize = ($distance - $padding - $space) / $this->number;
  250.     $barPosition = $leftPadding + $barSize * ($this->identifier - 1);
  251.  
  252.     for($key = 0; $key < $count; $key++) {
  253.  
  254.       $value = $this->datay[$key];
  255.           
  256.       if($value !== NULL) {
  257.  
  258.         $position = awAxis::toPosition(
  259.         $this->xAxis,
  260.         $this->yAxis,
  261.         new awPoint($key, $value)
  262.         );
  263.             
  264.         $barStart = $barPosition + ($this->identifier - 1) * $this->barSpace + $position->x;
  265.         $barStop = $barStart + $barSize;
  266.  
  267.         $t1 = min($zero->y, $position->y);
  268.         $t2 = max($zero->y, $position->y);
  269.  
  270.         if(round($t2 - $t1) == 0) {
  271.           continue;
  272.         }
  273.  
  274.         $p1 = new awPoint(
  275.         round($barStart) + $this->depth + $this->move->left,
  276.         round($t1) - $this->depth + $this->move->top
  277.         );
  278.  
  279.         $p2 = new awPoint(
  280.         round($barStop) + $this->depth + $this->move->left,
  281.         round($t2) - $this->depth + $this->move->top
  282.         );
  283.  
  284.         $this->drawBar($driver, $p1, $p2);
  285.  
  286.       }
  287.  
  288.     }
  289.  
  290.     // Draw labels
  291.     foreach($this->datay as $key => $value) {
  292.           
  293.       if($value !== NULL) {
  294.  
  295.         $position = awAxis::toPosition(
  296.         $this->xAxis,
  297.         $this->yAxis,
  298.         new awPoint($key, $value)
  299.         );
  300.             
  301.         $point = new awPoint(
  302.         $barPosition + ($this->identifier - 1) * $this->barSpace + $position->x + $barSize / 2 + 1 + $this->depth,
  303.         $position->y - $this->depth
  304.         );
  305.  
  306.         $this->label->draw($driver, $point, $key);
  307.  
  308.       }
  309.           
  310.     }
  311.  
  312.   }
  313.  
  314.   function getXAxisNumber() {
  315.     return count($this->datay) + 1;
  316.   }
  317.   // ça bidouille à fond ici !
  318.   function getXMax() {
  319.     return array_max($this->datax) + 1;
  320.   }
  321.  
  322.   function getXCenter() {
  323.     return TRUE;
  324.   }
  325.  
  326.   function drawBar($driver, $p1, $p2) {
  327.  
  328.     // Draw shadow
  329.     $this->barShadow->draw(
  330.     $driver,
  331.     $p1,
  332.     $p2,
  333.     SHADOW_OUT
  334.     );
  335.  
  336.     if(abs($p2->y - $p1->y) > 1) {
  337.           
  338.       $this->barBorder->rectangle(
  339.       $driver,
  340.       $p1,
  341.       $p2
  342.       );
  343.           
  344.       if($this->barBackground !== NULL) {
  345.             
  346.         $size = $this->barBorder->visible() ? 1 : 0;
  347.  
  348.         $b1 = $p1->move($size, $size);
  349.         $b2 = $p2->move(-1 * $size, -1 * $size);
  350.  
  351.         // Draw background
  352.         $driver->filledRectangle(
  353.         $this->barBackground,
  354.         new awLine($b1, $b2)
  355.         );
  356.  
  357.       }
  358.  
  359.     }
  360.   }
  361.  
  362. }
  363.  
  364. registerClass('BarPlot');
  365. ?>

Documentation generated on Thu, 03 May 2012 15:02:08 +0200 by phpDocumentor 1.4.1