/[drupal]/contributions/modules/graphstat/graphstat_render.inc
ViewVC logotype

Diff of /contributions/modules/graphstat/graphstat_render.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Sun Jan 25 21:08:34 2009 UTC revision 1.1.2.1, Sun Jan 25 21:08:34 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id: graphstat_render.inc,v 1.13.2.1 2009/01/25 14:32:28 profix898 Exp $
3    
4    function graphstat_render_graph($group, $key) {
5      global $user;
6    
7      $filter = isset($_SESSION['graphstat_filter'][$group]) ? $_SESSION['graphstat_filter'][$group] : NULL;
8      $cache = cache_get('graphstat:'. $user->uid);
9      $width = $cache ? $cache->data['width'] : 500;
10      $height = $cache ? $cache->data['height'] : 300;
11      $time = format_date(time(), 'custom', 'H');
12    
13      $cid = 'graphstat:'. md5($group . $key . $filter) .':'. $width .':'. $time;
14      if ($cache = cache_get($cid)) {
15        $image = $cache->data;
16      }
17      else {
18        // Cache Miss: we need to generate the graphs from scratch
19        $graphs = module_invoke_all('graphstat');
20        $graphs = $graphs[$group];
21        $data = module_invoke_all('graphstat_data');
22        $data = isset($data[$group]) ? $data[$group] : array();
23        // If a filter is defined then invoke the callback function
24        // to fetch the filtered graph data
25        if (isset($graphs['#filter'])) {
26          $function = $graphs['#filter']['#callback'];
27          if (function_exists($function)) {
28            $function($data, $filter);
29          }
30        }
31        $data = isset($data[$key]) ? $data[$key] : array();
32        // If no graph data available then render a placeholder image
33        if (empty($data)) {
34          if (function_exists('imagepng')) {
35            $img = imagecreate($width, $height);
36            imagefill($img, 0, 0, imagecolorallocate($img, 240, 240, 240));
37            imageline($img, 0, 0, $width, $height, imagecolorallocate($img, 128, 128, 128));
38            imageline($img, 0, $height, $width, 0, imagecolorallocate($img, 128, 128, 128));
39            imagepng($img);
40            imagedestroy($img);
41          }
42          exit();
43        }
44    
45        //
46        $renderer = variable_get('graphstat_renderer', 'graphstat');
47        $graph = module_invoke($renderer, 'graphstat_renderer', $graphs[$key], $data, $width, $height);
48    
49        //
50        if (is_array($graph)) {
51          $image = $graph['image'];
52          if (isset($graph['#type'])) {
53            drupal_set_header($graph['#type']);
54          }
55        }
56        else {
57          $image = $graph;
58          drupal_set_header('Content-type: image/png');
59        }
60    
61        // Put $image into the cache
62        cache_set($cid, $image, 'cache', CACHE_TEMPORARY);
63      }
64    
65      // Print out the image and make sure to exit immediatly
66      print $image;
67      exit();
68    }
69    
70    /**
71     * Implementation of hook_graphstat_renderer().
72     * (renders the actual graph images, in this case with the PHPLOT library)
73     */
74    function graphstat_graphstat_renderer($graph, $data, $width, $height) {
75      require_once(drupal_get_path('module', 'graphstat') .'/phplot.php');
76    
77      // Create instance of PHPLOT and configure graph paramters
78      $plot =& new PHPlot($width, $height);
79      $graph['#type'] = isset($graph['#type']) ? $graph['#type'] : 'linepoints';
80      $plot->SetPlotType($graph['#type']);
81      $plot->SetTitle(isset($graph['#title']) ? $graph['#title'] : '');
82      $plot->SetXTitle(isset($graph['#xlabel']) ? $graph['#xlabel'] : '');
83      $plot->SetXTickLabelPos('none');
84      $plot->SetYTitle(isset($graph['#ylabel']) ? $graph['#ylabel'] : '');
85      $plot->SetXLabelAngle(90);
86      if (isset($graph['#legend'])) {
87        $plot->SetLegend($graph['#legend']);
88      }
89      // Push graph data into the plot renderer
90      if (!is_array(current($data))) {
91        $series = $data;
92        $data = array();
93        foreach ($series as $x => $y) {
94          $data[] = array($x, $y);
95        }
96      }
97      $plot->SetDataValues($data);
98      // Draw the graph into a buffer $image
99      ob_start();
100      $plot->DrawGraph();
101      $image = ob_get_contents();
102      ob_end_clean();
103    
104      return $image;
105    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2