My favorites | Sign in
Project Logo
                
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
require_once('util.php');
class gBackground{
public $colors = array("ffffff");
public $fillType = 0;
protected $fillTypes = array ("s", "lg", "ls");
public $isChart = false;
public function toArray(){
$retArray = array();
if($this->isChart)
array_push($retArray, "c");
else
array_push($retArray, "bg");
array_push($retArray,$this->fillTypes[$this->fillType]);
array_push($retArray,$this->colors[0]);
return $retArray;
}
}
class gChart{
private $baseUrl = "http://chart.apis.google.com/chart?";
protected $scalar = 1;

public $types = array ("lc","lxy","bhs","bvs","bhg","bvg","p","p3","v","s");
public $type = 1;
public $dataEncodingType = "t";
public $values = Array();
protected $scaledValues = Array();
public $valueLabels;
public $xAxisLabels;
public $dataColors;
public $width = 200; //default
public $height = 200; //default
private $title;

public $backgrounds;


public function setTitle($newTitle){
$this->title = str_replace("\r\n", "|", $newTitle);
$this->title = str_replace(" ", "+", $this->title);
}

public function addBackground($gBackground){
if(!isset($this->backgrounds)){
$this->backgrounds = array($gBackground);
return;
}
array_push($this->backgrounds, $gBackground);
}

protected function encodeData($data, $encoding, $separator){
switch ($this->dataEncodingType){
case "s":
return $this->simpleEncodeData();
case "e":
return $this->extendedEncodeData();
default:{
$retStr = $this->textEncodeData($data, $separator, "|");
$retStr = trim($retStr, "|");
return $retStr;
}
}
}

private function textEncodeData($data, $separator, $datasetSeparator){
$retStr = "";
if(!is_array($data))
return $data;
foreach($data as $currValue){
if(is_array($currValue))
$retStr .= $this->textEncodeData($currValue, $separator, $datasetSeparator);
else
$retStr .= $currValue.$separator;
}

$retStr = trim($retStr, $separator);
$retStr .= $datasetSeparator;
return $retStr;
}

public function addDataSet($dataArray){
array_push($this->values, $dataArray);
}
public function clearDataSets(){
$this->values = Array();
}

private function simpleEncodeData(){
return "";
}

private function extendedEncodeData(){
return "";
}

protected function prepForUrl(){
$this->scaleValues();
}
protected function getDataSetString(){
return "&chd=".$this->dataEncodingType.":".$this->encodeData($this->scaledValues,"" ,",");
}
protected function getAxesString(){
$retStr = "&chxt=x,y";
$retStr .= "&chxr=0,1,4|1,1,10";
return $retStr;
}

protected function getBackgroundString(){
if(!isset($this->backgrounds))
return "";
$retStr = "&chf=";
foreach($this->backgrounds as $currBg){
$retStr .= $this->textEncodeData($currBg->toArray(), ",", "|");
}
$retStr = trim($retStr, "|");
return $retStr;
}
protected function getAxisLabels(){
$retStr = "";
if(isset($this->xAxisLabels))
$retStr = "&chxl=0:|".$this->encodeData($this->xAxisLabels,"", "|");
return $retStr;
}
protected function concatUrl(){
$fullUrl .= $this->baseUrl;
$fullUrl .= "cht=".$this->types[$this->type];
$fullUrl .= "&chs=".$this->width."x".$this->height;

$fullUrl .= $this->getDataSetString();
if(isset($this->valueLabels))
$fullUrl .= "&chdl=".$this->encodeData($this->getApplicableLabels($this->valueLabels),"", "|");
$fullUrl .= $this->getAxisLabels();
$fullUrl .= "&chco=".$this->encodeData($this->dataColors,"", ",");
if(isset($this->title))
$fullUrl .= "&chtt=".$this->title;
$fullUrl .= $this->getAxesString();
// $fullUrl .= $this->getBackgroundString();

return $fullUrl;
}
protected function getApplicableLabels($labels){
$trimmedValueLabels = $labels;
return array_splice($trimmedValueLabels, 0, count($this->values));
}
public function getUrl(){
$this->prepForUrl();
return $this->concatUrl();
}

protected function scaleValues(){
$this->setScalar();
$this->scaledValues = utility::getScaledArray($this->values, $this->scalar);
}


function setScalar(){
$maxValue = 100;
$maxValue = max($maxValue, utility::getMaxOfArray($this->values));
if($maxValue <100)
$this->scalar = 1;
else
$this->scalar = 100/$maxValue;
}
}

class gPieChart extends gChart{
function __construct(){
$this->type = 6;
$this->width = $this->height * 1.5;
}
function setScalar(){
return 1;
}

protected function getAxesString(){
return "";
}

public function getUrl(){
$retStr = parent::getUrl();
$retStr .= "&chl=".$this->encodeData($this->valueLabels,"", "|");
return $retStr;
}
private function getScaledArray($unscaledArray, $scalar){
return $unscaledArray;
}
public function set3D($is3d){
if($is3d){
$this->type = 7;
$this->width = $this->height * 2;
}
else{
$this->type = 6;
$this->width = $this->height * 1.5;
}
}
}

class gLineChart extends gChart{
function __construct(){
$this->type = 0;
}
}

class gBarChart extends gChart{
public $barWidth;
private $realBarWidth;
public $groupSpacerWidth = 1;
protected $totalBars = 1;
protected $isHoriz = false;
public function getUrl(){
$this->scaleValues();
$this->setBarWidth();
$retStr = parent::concatUrl();
$retStr .= "&chbh=$this->realBarWidth,$this->groupSpacerWidth";
return $retStr;
}

function setBarCount(){
$this->totalBars = utility::count_r($this->values);
}

protected function getAxisLabels(){
$retStr = "";
$xAxis = 0;
if($this->isHoriz)
$xAxis = 1;
$yAxis = 1 - $xAxis;
if(isset($this->xAxisLabels)){
$retStr = "&chxl=$xAxis:|".$this->encodeData($this->xAxisLabels,"", "|");
// $retStr = "&$yAxis:|".$this->encodeData($this->yAxisLabels,"", "|");
}
return $retStr;
}
private function setBarWidth(){
if(isset($this->barWidth)){
$this->realBarWidth = $this->barWidth;
return;
}
$this->setBarCount();
$totalGroups = utility::getMaxCountOfArray($this->values);
if($this->isHoriz)
$chartSize = $this->height - 50;
else
$chartSize = $this->width - 50;

$chartSize -= $totalGroups * $this->groupSpacerWidth;
$this->realBarWidth = round($chartSize/$this->totalBars);
}

}
class gGroupedBarChart extends gBarChart{
function __construct(){
$this->type = 5;
}

public function setHorizontal($isHorizontal){
if($isHorizontal){
$this->type = 4;
}
else{
$this->type = 5;
}
$this->isHoriz = $isHorizontal;
}

}
class gStackedBarChart extends gBarChart{
function __construct(){
$this->type = 3;
}

function setBarCount(){
$this->totalBars = utility::getMaxCountOfArray($this->values);
}

public function setHorizontal($isHorizontal){
if($isHorizontal){
$this->type = 2;
}
else{
$this->type = 3;
}
$this->isHoriz = $isHorizontal;
}

protected function scaleValues(){
$this->setScalar();
$this->scaledValues = utility::getScaledArray($this->values, $this->scalar);
}

function setScalar(){
$maxValue = 100;
$maxValue = max($maxValue, utility::getMaxOfArray(utility::addArrays($this->values)));
if($maxValue <100)
$this->scalar = 1;
else
$this->scalar = 100/$maxValue;
}

}

class gVennDiagram extends gChart{
private $intersections = array(0,0,0,0);
public function addIntersections($mixed){
$this->intersections = $mixed;
}
function __construct(){
$this->type = 8;
}
protected function getAxesString(){
return "";
}

public function getUrl(){
$retStr = parent::getUrl();
// $retStr .= "&chl=".$this->encodeData($this->valueLabels,"", "|");
return $retStr;
}
protected function getDataSetString(){
$fullDataSet = array_splice($this->scaledValues[0], 0, 3);
while(count($fullDataSet)<3){
array_push($fullDataSet, 0);
}

$scaledIntersections = utility::getScaledArray($this->intersections, $this->scalar);
foreach($scaledIntersections as $temp){
array_push($fullDataSet, $temp);
}
$fullDataSet = array_splice($fullDataSet, 0, 7);
while(count($fullDataSet)<7){
array_push($fullDataSet, 0);
}

return "&chd=".$this->dataEncodingType.":".$this->encodeData($fullDataSet,"" ,",");
}
}


?>
Show details Hide details

Change log

r12 by natatwo on Jan 08, 2008   Diff
fixed bar width bug, added xAxis label
support
Go to: 
Project members, sign in to write a code review

Older revisions

r3 by natatwo on Dec 10, 2007   Diff
importing code
All revisions of this file

File info

Size: 8457 bytes, 340 lines
Hosted by Google Code