[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/library/ezc/Graph/src/ -> tools.php (source)

   1  <?php
   2  /**
   3   * File containing the abstract ezcGraph class
   4   *
   5   * @package Graph
   6   * @version 1.5
   7   * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
   8   * @license http://ez.no/licenses/new_bsd New BSD License
   9   */
  10  /**
  11   * Toolkit for several operation with graphs
  12   *
  13   * @version 1.5
  14   * @package Graph
  15   * @mainclass
  16   */
  17  class ezcGraphTools
  18  {
  19      /**
  20       * Create an XHtml image map from a chart with gd driver. The name of the 
  21       * image map can be specified and will be ezcGraphImageMap otherwise.
  22       * 
  23       * @param ezcGraphChart $chart 
  24       * @param string $name
  25       * @return string
  26       */
  27      public static function createImageMap( ezcGraphChart $chart, $name = null )
  28      {
  29          if ( $name === null )
  30          {
  31              $name = 'ezcGraphImageMap';
  32          }
  33  
  34          if ( ! ( $chart->driver instanceof ezcGraphGdDriver ) )
  35          {
  36              throw new ezcGraphToolsIncompatibleDriverException( $chart->driver, 'ezcGraphGdDriver' );
  37          }
  38  
  39          $elements = $chart->renderer->getElementReferences();
  40  
  41          if ( !count( $elements ) )
  42          {
  43              throw new ezcGraphToolsNotRenderedException( $chart );
  44          }
  45      
  46          $imageMap = sprintf( "<map name=\"%s\">\n", $name );
  47  
  48          // Iterate over legends elements
  49          if ( isset( $elements['legend'] ) )
  50          {
  51              foreach ( $elements['legend'] as $objectName => $polygones )
  52              {
  53                  $url = $elements['legend_url'][$objectName];
  54  
  55                  if ( empty( $url ) )
  56                  {
  57                      continue;
  58                  }
  59  
  60                  foreach ( $polygones as $shape => $polygone )
  61                  {
  62                      $coordinateString = '';
  63                      foreach ( $polygone as $coordinate )
  64                      {
  65                          $coordinateString .= sprintf( '%d,%d,', $coordinate->x, $coordinate->y );
  66                      }
  67  
  68                      $imageMap .= sprintf( "\t<area shape=\"poly\" coords=\"%s\" href=\"%s\" alt=\"%s\" />\n",
  69                          substr( $coordinateString, 0, -1 ),
  70                          $url,
  71                          $objectName
  72                      );
  73                  }
  74              }
  75          }
  76  
  77          // Iterate over data
  78          foreach ( $elements['data'] as $dataset => $datapoints )
  79          {
  80              foreach ( $datapoints as $datapoint => $polygones )
  81              {
  82                  $url = $chart->data[$dataset]->url[$datapoint];
  83  
  84                  if ( empty( $url ) )
  85                  {
  86                      continue;
  87                  }
  88  
  89                  foreach ( $polygones as $polygon )
  90                  {
  91                      $coordinateString = '';
  92                      foreach ( $polygon as $coordinate )
  93                      {
  94                          $coordinateString .= sprintf( '%d,%d,', $coordinate->x, $coordinate->y );
  95                      }
  96  
  97                      $imageMap .= sprintf( "\t<area shape=\"poly\" coords=\"%s\" href=\"%s\" alt=\"%s\" />\n",
  98                          substr( $coordinateString, 0, -1 ),
  99                          $url,
 100                          $datapoint
 101                      );
 102                  }
 103              }
 104          }
 105  
 106          return $imageMap . "</map>\n";
 107      }
 108  
 109      /**
 110       * Add links to clickable SVG elements in a chart with SVG driver.
 111       * 
 112       * @param ezcGraphChart $chart 
 113       * @return void
 114       */
 115      public static function linkSvgElements( ezcGraphChart $chart )
 116      {
 117          if ( ! ( $chart->driver instanceof ezcGraphSvgDriver ) )
 118          {
 119              throw new ezcGraphToolsIncompatibleDriverException( $chart->driver, 'ezcGraphSvgDriver' );
 120          }
 121  
 122          $fileName = $chart->getRenderedFile();
 123  
 124          if ( !$fileName )
 125          {
 126              throw new ezcGraphToolsNotRenderedException( $chart );
 127          }
 128  
 129          $dom = new DOMDocument();
 130          $dom->load( $fileName );
 131          $xpath = new DomXPath( $dom );
 132  
 133          $elements = $chart->renderer->getElementReferences();
 134  
 135          // Link chart elements
 136          foreach ( $elements['data'] as $dataset => $datapoints )
 137          {
 138              foreach ( $datapoints as $datapoint => $ids )
 139              {
 140                  $url = $chart->data[$dataset]->url[$datapoint];
 141  
 142                  if ( empty( $url ) )
 143                  {
 144                      continue;
 145                  }
 146  
 147                  foreach ( $ids as $id )
 148                  {
 149                      $element = $xpath->query( '//*[@id = \'' . $id . '\']' )->item( 0 );
 150  
 151                      $element->setAttribute( 'style', $element->getAttribute( 'style' ) . ' cursor: ' . $chart->driver->options->linkCursor . ';' );
 152                      $element->setAttribute( 'onclick', "top.location = '{$url}'" );
 153                  }
 154              }
 155          }
 156  
 157          // Link legend elements
 158          if ( isset( $elements['legend'] ) )
 159          {
 160              foreach ( $elements['legend'] as $objectName => $ids )
 161              {
 162                  $url = $elements['legend_url'][$objectName];
 163  
 164                  if ( empty( $url ) )
 165                  {
 166                      continue;
 167                  }
 168  
 169                  foreach ( $ids as $id )
 170                  {
 171                      $element = $xpath->query( '//*[@id = \'' . $id . '\']' )->item( 0 );
 172  
 173                      $element->setAttribute( 'style', $element->getAttribute( 'style' ) . ' cursor: ' . $chart->driver->options->linkCursor . ';' );
 174                      $element->setAttribute( 'onclick', "top.location = '{$url}'" );
 175                  }
 176              }
 177          }
 178  
 179          $dom->save( $fileName );
 180      }
 181  }
 182  
 183  ?>


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7