ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
GoogleCharts.php
Go to the documentation of this file.
1 <?php
2 /*
3  * sspmod_statistics_Graph_GoogleCharts will help you to create a Google Chart
4  * using the Google Charts API.
5  *
6  * @author Andreas Åkre Solberg <andreas.solberg@uninett.no>
7  * @package SimpleSAMLphp
8  */
10 {
11  private $x, $y;
12 
21  public function __construct($x = 800, $y = 350)
22  {
23  $this->x = $x;
24  $this->y = $y;
25  }
26 
27  private function encodeaxis($axis)
28  {
29  return join('|', $axis);
30  }
31 
32  // t:10.0,58.0,95.0
33  private function encodedata($datasets)
34  {
35  $setstr = array();
36  foreach ($datasets as $dataset) {
37  $setstr[] = self::extEncode($dataset);
38  }
39  return 'e:' . join(',', $setstr);
40  }
41 
42  public static function extEncode($values) // $max = 4095, $min = 0
43  {
44  $extended_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
45  $chardata = '';
46  $delta = 4095;
47  $size = (strlen($extended_table));
48 
49  foreach ($values as $k => $v) {
50  if ($v >= 0 && $v <= 100) {
51  $first = substr($extended_table, intval(($delta * $v / 100) / $size), 1);
52  $second = substr($extended_table, intval(($delta * $v / 100) % $size), 1);
53  $chardata .= "$first$second";
54  } else {
55  $chardata .= '__'; // Value out of max range;
56  }
57  }
58  return($chardata);
59  }
60 
64  private function getFillArea($datasets)
65  {
66  if (count($datasets) < 2) {
67  return '';
68  }
69 
70  $colors = array('eeeeee', 'cccccc', 'aaaaaa', '99eecc');
71  $num = count($datasets) ;
72  $colstr = array();
73  for ($i = 0; $i < $num; $i++) {
74  $colstr[] = 'b' . ',' . $colors[$i] . ',' . ($i) . ',' . ($i+1) . ',0';
75  }
76  return '&chm=' . join('|', $colstr);
77  }
78 
89  public function show($axis, $axispos, $datasets, $maxes)
90  {
91  $labeld = '&chxt=x,y' . '&chxr=0,0,1|1,0,' . $maxes[0];
92  if (count($datasets) > 1) {
93  if (count($datasets) !== count($maxes)) {
94  throw new Exception('Incorrect number of max calculations for graph plotting.');
95  }
96  $labeld = '&chxt=x,y,r' . '&chxr=0,0,1|1,0,' . $maxes[0] . '|2,0,' . $maxes[1];
97  }
98 
99  $url = 'https://chart.apis.google.com/chart?' .
100  // Dimension of graph. Default is 800x350
101  'chs=' . $this->x . 'x' . $this->y .
102 
103  // Dateset values
104  '&chd=' . $this->encodedata($datasets) .
105 
106  // Fill area...
107  '&chco=ff5c00,cca600' .
108  '&chls=1,1,0|1,6,3' .
109 
110  // chart type is linechart
111  '&cht=lc' .
112  $labeld .
113  '&chxl=0:|' . $this->encodeaxis($axis) . # . $'|1:||top' .
114  '&chxp=0,' . join(',', $axispos) .
115  '&chg=' . (2400/(count($datasets[0])-1)) . ',-1,3,3'; // lines
116 
117  return $url;
118  }
119 
120  public function showPie($axis, $datasets)
121  {
122  $url = 'https://chart.apis.google.com/chart?' .
123 
124  // Dimension of graph. Default is 800x350
125  'chs=' . $this->x . 'x' . $this->y .
126 
127  // Dateset values.
128  '&chd=' . $this->encodedata(array($datasets)) .
129 
130  // chart type is linechart
131  '&cht=p' .
132 
133  '&chl=' . $this->encodeaxis($axis);
134 
135  return $url;
136  }
137 
154  public static function roof($max)
155  {
156  $mul = 1;
157 
158  if ($max < 1) {
159  return 1;
160  }
161 
162  $t = intval(ceil($max));
163  while ($t > 100) {
164  $t /= 10;
165  $mul *= 10;
166  }
167 
168  $maxGridLines = 10;
169  $candidates = array(1, 2, 5, 10, 20, 25, 50, 100);
170 
171  foreach ($candidates as $c) {
172  if ($t / $c < $maxGridLines) {
173  $tick_y = $c * $mul;
174  $target_top = intval(ceil($max / $tick_y) * $tick_y);
175  return $target_top;
176  }
177  }
178  }
179 }
show($axis, $axispos, $datasets, $maxes)
Generate a Google Charts URL which points to a generated image.
$size
Definition: RandomTest.php:84
static roof($max)
Takes a input value, and generates a value that suits better as a max value on the Y-axis...
$dataset
Definition: showstats.php:45
__construct($x=800, $y=350)
Constructor.
$datasets
Definition: showstats.php:61
Create styles array
The data for the language used.
getFillArea($datasets)
Return colors between multiple graphs...
$i
Definition: disco.tpl.php:19
$url
$axis
Definition: showstats.php:64
$maxes
Definition: showstats.php:66