ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 {
14  private $x;
15 
19  private $y;
20 
29  public function __construct($x = 800, $y = 350)
30  {
31  $this->x = $x;
32  $this->y = $y;
33  }
34 
35  private function encodeaxis($axis)
36  {
37  return join('|', $axis);
38  }
39 
40  // t:10.0,58.0,95.0
41  private function encodedata($datasets)
42  {
43  $setstr = array();
44  foreach ($datasets as $dataset) {
45  $setstr[] = self::extEncode($dataset);
46  }
47  return 'e:' . join(',', $setstr);
48  }
49 
50  public static function extEncode($values) // $max = 4095, $min = 0
51  {
52  $extended_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
53  $chardata = '';
54  $delta = 4095;
55  $size = (strlen($extended_table));
56 
57  foreach ($values as $k => $v) {
58  if ($v >= 0 && $v <= 100) {
59  $first = substr($extended_table, intval(($delta * $v / 100) / $size), 1);
60  $second = substr($extended_table, intval(($delta * $v / 100) % $size), 1);
61  $chardata .= "$first$second";
62  } else {
63  $chardata .= '__'; // Value out of max range;
64  }
65  }
66  return($chardata);
67  }
68 
79  public function show($axis, $axispos, $datasets, $maxes)
80  {
81  $labeld = '&chxt=x,y' . '&chxr=0,0,1|1,0,' . $maxes[0];
82  if (count($datasets) > 1) {
83  if (count($datasets) !== count($maxes)) {
84  throw new Exception('Incorrect number of max calculations for graph plotting.');
85  }
86  $labeld = '&chxt=x,y,r' . '&chxr=0,0,1|1,0,' . $maxes[0] . '|2,0,' . $maxes[1];
87  }
88 
89  $url = 'https://chart.apis.google.com/chart?' .
90  // Dimension of graph. Default is 800x350
91  'chs=' . $this->x . 'x' . $this->y .
92 
93  // Dateset values
94  '&chd=' . $this->encodedata($datasets) .
95 
96  // Fill area...
97  '&chco=ff5c00,cca600' .
98  '&chls=1,1,0|1,6,3' .
99 
100  // chart type is linechart
101  '&cht=lc' .
102  $labeld .
103  '&chxl=0:|' . $this->encodeaxis($axis) . # . $'|1:||top' .
104  '&chxp=0,' . join(',', $axispos) .
105  '&chg=' . (2400/(count($datasets[0])-1)) . ',-1,3,3'; // lines
106 
107  return $url;
108  }
109 
110  public function showPie($axis, $datasets)
111  {
112  $url = 'https://chart.apis.google.com/chart?' .
113 
114  // Dimension of graph. Default is 800x350
115  'chs=' . $this->x . 'x' . $this->y .
116 
117  // Dateset values.
118  '&chd=' . $this->encodedata(array($datasets)) .
119 
120  // chart type is linechart
121  '&cht=p' .
122 
123  '&chl=' . $this->encodeaxis($axis);
124 
125  return $url;
126  }
127 
144  public static function roof($max)
145  {
146  $mul = 1;
147 
148  if ($max < 1) {
149  return 1;
150  }
151 
152  $t = intval(ceil($max));
153  while ($t > 100) {
154  $t /= 10;
155  $mul *= 10;
156  }
157 
158  $maxGridLines = 10;
159  $candidates = array(1, 2, 5, 10, 20, 25, 50, 100);
160 
161  foreach ($candidates as $c) {
162  if ($t / $c < $maxGridLines) {
163  $tick_y = $c * $mul;
164  $target_top = intval(ceil($max / $tick_y) * $tick_y);
165  return $target_top;
166  }
167  }
168  }
169 }
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...
__construct($x=800, $y=350)
Constructor.
$datasets
Definition: showstats.php:85
$values
$url
$axis
Definition: showstats.php:88
$maxes
Definition: showstats.php:90