[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/Graph/Chart/ -> pie.php (source)

   1  <?php
   2  /**
   3   * Pie graph implementation for the Horde_Graph package.
   4   *
   5   * $Horde: framework/Graph/Graph/Chart/pie.php,v 1.10.10.4 2006/01/01 21:28:18 jan Exp $
   6   *
   7   * Copyright 2002-2006 Chuck Hagenbuch <chuck@horde.org>
   8   *
   9   * See the enclosed file COPYING for license information (LGPL). If you
  10   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  11   *
  12   * @author  Chuck Hagenbuch <chuck@horde.org>
  13   * @since   Horde 3.0
  14   * @package Horde_Graph
  15   */
  16  class Horde_Graph_Chart_pie {
  17  
  18      var $graph;
  19  
  20      var $_dataset;
  21      var $_padding = 20;
  22      var $_outline = true;
  23      var $_colors = array('tan', 'palegoldenrod', 'olivedrab', 'blue', 'red', 'green', 'yellow',
  24                           'orange', 'gray', 'purple');
  25      /**
  26        "255,153,0",
  27        "0,204,153",
  28        "204,255,102",
  29        "255,102,102",
  30        "102,204,255",
  31        "204,153,255",
  32        "255,0,0",
  33        "51,0,255",
  34        "255,51,153",
  35        "204,0,255",
  36        "255,255,51",
  37        "51,255,51",
  38        "255,102,0");
  39      */
  40  
  41      function Horde_Graph_Chart_pie(&$graph, $params)
  42      {
  43          $this->graph = &$graph;
  44  
  45          foreach ($params as $param => $value) {
  46              $key = '_' . $param;
  47              $this->$key = $value;
  48          }
  49      }
  50  
  51      function draw()
  52      {
  53          $data = $this->graph->_data['y'][$this->_dataset];
  54  
  55          // Initialize some variables.
  56          $diameter = min($this->graph->_graphWidth, $this->graph->_graphHeight) - ($this->_padding * 2);
  57          $radius = $diameter / 2;
  58          $count = count($data);
  59          $xcenter = $this->graph->_graphLeft + $this->_padding + ($this->graph->_graphWidth / 2);
  60          $ycenter = $this->graph->_graphTop + $this->_padding + ($this->graph->_graphHeight / 2);
  61  
  62          // Calculate the sum of all slices.
  63          $sum = 0;
  64          foreach ($data as $x) {
  65              $sum += $x;
  66          }
  67  
  68          // Convert each slice into the corresponding percentage of a
  69          // 360-degree circle.
  70          $degCount = 0;
  71          $slices = array();
  72          $degrees = array();
  73          foreach ($data as $i => $y) {
  74              if ((($y / $sum) * 360) > 0) {
  75                  $degrees[$degCount] = ($y / $sum) * 360;
  76                  $slices[$degCount] = $y;
  77                  $names[$degCount] = isset($this->graph->_data['x'][$i]) ? $this->graph->_data['x'][$i] : '';
  78                  $degCount++;
  79              }
  80          }
  81  
  82          // Draw the baseline.
  83          if ($count > 1) {
  84              $last_angle = 0;
  85              $count = count($degrees);
  86              for ($z = 0; $z < $count; $z++) {
  87                  // Calculate and draw arcs corresponding to each
  88                  // slice.
  89                  $cz = $z % count($this->_colors);
  90                  $this->graph->img->arc($xcenter, $ycenter, $radius, $last_angle, ($last_angle + $degrees[$z]),
  91                                         $this->_outline ? 'black' : $this->_colors[$cz], $this->_colors[$cz]);
  92                  $last_angle = $last_angle + $degrees[$z];
  93              }
  94          } else {
  95              $this->graph->img->circle($xcenter, $ycenter, $radius, 'black', $this->_colors[0]);
  96          }
  97  
  98          // Create the color key and slice labels.
  99          $yBase = $this->graph->_graphTop;
 100          $xBase = 5;
 101          $max = strlen((string)max($data));
 102          for ($z = 0; $z < $degCount; $z++) {
 103              $cz = $z % count($this->_colors);
 104              $percent = ($degrees[$z] / 360) * 100;
 105              $percent = round($percent, 2);
 106              $yBase += 15;
 107  
 108              $this->graph->img->rectangle($xBase, $yBase, 12, 12, 'black', $this->_colors[$cz]);
 109              if ($slices[$z] >= 1000 && $slices[$z] < 1000000) {
 110                  $slices[$z] = $slices[$z] / 1000;
 111                  $slices[$z] = $slices[$z] . 'k';
 112              }
 113              $repeat = $max - strlen($slices[$z]);
 114              if ($repeat < 0) {
 115                  $repeat = 0;
 116              }
 117              $slices[$z] = str_repeat(' ', $repeat) . $slices[$z];
 118  
 119              $this->graph->img->text($slices[$z], $xBase + 20, ($yBase + 1));
 120  
 121              $label = $names[$z] . ' (' . $percent . '%)';
 122              if (strlen($label) > 20) {
 123                  $labels = explode("\n", wordwrap($label, 20));
 124                  foreach ($labels as $i => $label) {
 125                      if ($i > 0) {
 126                          $yBase += 15;
 127                      }
 128                      $this->graph->img->text($label, $xBase + 35 + ($max * 4), ($yBase + 1));
 129                  }
 130              } else {
 131                  $this->graph->img->text($label, $xBase + 35 + ($max * 4), ($yBase + 1));
 132              }
 133          }
 134      }
 135  
 136  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7