-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogChart.class.php
270 lines (228 loc) · 6.35 KB
/
GoogChart.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
#
# Chart
#
# by Ludwig Pettersson
# <http://luddep.se>
#
# With help from Fredrik Holmstr�m
# <http://loveandtheft.org/>
#
# Copyright (c) 2008 Ludwig Pettersson
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
class GoogChart
{
// Constants
const BASE = 'http://chart.apis.google.com/chart?';
// Variables
protected $types = array(
'pie' => 'p',
'line' => 'lc',
'linespecify' => 'lxy',
'sparkline' => 'ls',
'bar-horizontal' => 'bhg',
'bar-vertical' => 'bvg',
);
protected $type;
protected $title;
protected $data = array();
protected $size = array();
protected $color = array();
protected $width;
protected $marker;
protected $fill = array();
protected $labelsXY = false;
//protected $axisLabels;
protected $legend;
protected $useLegend = false;
protected $background = 'a,s,ffffff';
protected $query = array();
// debug
public $debug = array();
// Return string
public function __toString()
{
return $this->display();
}
/** Create chart
*/
protected function display()
{
// Create query
$this->query = array(
'cht' => $this->types[strtolower($this->type)], // Type
//'chtt' => $this->title, // Title
'chd' => 't:'.$this->data['values'],
//hide the axis labels// Data
//'chl' => $this->data['names'], // Data labels
'chdl' => ( ($this->useLegend) && (is_array($this->legend)) ) ? implode('|',$this->legend) : null, // Data legend
'chs' => $this->size[0].'x'.$this->size[1], // Size
'chco' => preg_replace( '/[#]+/', '', implode(',',$this->color)), // Color ( Remove # from string )
'chm' => preg_replace( '/[#]+/', '', implode('|',$this->fill)), // Fill ( Remove # from string )
//'chxt' => ( $this->labelsXY == true) ? 'x,y' : null, // X & Y axis labels
// 'chxr' => $this->labelValues,
// 'chxp' => $this->axisLabels,
'chf' => preg_replace( '/[#]+/', '', $this->background),
'chls' => $this->width, //line width
'chm' => $this->marker, //point markers
// Background color ( Remove # from string )
);
// Return chart
return $this->img(
GoogChart::BASE.http_build_query($this->query),
$this->title
);
}
/** Set attributes
*/
public function setChartAttrs( $attrs )
{
// debug
$this->debug[] = $attrs;
foreach( $attrs as $key => $value )
{
$this->{"set$key"}($value);
}
}
/** Set type
*/
protected function setType( $type )
{
$this->type = $type;
}
/** Set title
*/
protected function setTitle( $title )
{
$this->title = $title;
}
/** Set data
*/
protected function setData( $data )
{
// Clear any previous data
unset( $this->data );
// Check if multiple data
if( is_array(reset($data)) )
{
/** Multiple sets of data
*/
foreach( $data as $key => $value )
{
// Add data values
$this->data['values'][] = implode( ',', $value );
// Add data names
$this->data['names'] = implode( '|', array_keys( $value ) );
}
/** Implode data correctly
*/
$this->data['values'] = implode('|', $this->data['values']);
/** Create legend
*/
//$this->legend = array_keys( $data );
}
else
{
/** Single set of data
*/
// Add data values
$this->data['values'] = implode( ',', $data );
// Add data names
$this->data['names'] = implode( '|', array_keys( $data ) );
}
}
/** Set legend
*/
protected function setLegend( $legend )
{
$this->useLegend = $legend;
}
/** Set size
*/
protected function setSize( $width, $height = null )
{
// check if width contains multiple params
if(is_array( $width ) )
{
$this->size = $width;
}
else
{
// set each individually
$this->size[] = $width;
$this->size[] = $height;
}
}
/** Set color
*/
protected function setColor( $color )
{
$this->color = $color;
}
// protected function setAxisLabels( $axisLabels )
// {
// $this->axisLabels = $axisLabels;
// }
/** Set line width */
protected function setWidth( $width )
{
$this->width = $width;
}
/** Set line markers */
protected function setMarker( $marker )
{
$this->marker = $marker;
}
/** Set labels
*/
// protected function setLabelsXY( $labels )
// {
// $this->labelsXY = $labels;
//
// }
// protected function setLabelValues( $labelValues )
// {
// $this->labelValues = $labelValues;
// }
/** Set fill
*/
protected function setFill( $fill )
{
// Fill must have atleast 4 parameters
if( count( $fill ) < 4 )
{
// Add remaining params
$count = count( $fill );
for( $i = 0; $i < $count; ++$i )
$fill[$i] = 'b,'.$fill[$i].','.$i.','.($i+1).',0';
}
$this->fill = $fill;
}
/** Set background
*/
protected function setBackground( $background )
{
$this->background = 'bg,s,'.$background;
}
/** Create img html tag
*/
protected function img( $url, $alt = null )
{
return sprintf('<img src="%s" alt="%s" style="width:%spx;height:%spx;" />', $url, $alt, $this->size[0], $this->size[1]);
}
}
?>