-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFormat.php
270 lines (254 loc) · 8.54 KB
/
Format.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
/**
* Google_Format
* @package Google
* @subpackage Google_Format
* @author Thomas Schaefer
* @copyright Thomas Schaefer
* @since 2009-05-20
* @desc Factory to load a formatter object
*
* The Google Visualization API provides formatters that can be used to reformat
* data in a visualization. These formatters change the formatted value of the
* specified column in all rows. Note that it does not modify the underlying
* values; just the formatted values. So, for example, the displayed value would
* be "$1,000.00" but the underlying value would still be "1000". Formatters can
* only affect one column at a time; to reformat multiple columns, apply a
* formatter to each column that you want to change.
*
* Important: Formatters can only be used with a DataTable; they cannot be used
* with a DataView (DataView objects are read-only).
*
* Here are the general steps for using a formatter:
*
* 1. Get your populated DataTable object.
* 2. For each column that you want to reformat:
*
* 1. Create an object that specifies all the options for your formatter.
* This is a basic JavaScript object with a set of properties and
* values. Look at your formatter's documentation to see what
* properties are supported. (Optionally, you can pass in an object
* literal notation object specifying your options.)
* 2. Create your formatter, passing in your options object.
* 3. Call formatter.Format(table, colIndex), passing in the DataTable
* and the (zero-based) column number of the data to reformat.
* Important: Many formatters require HTML tags to display special
* formatting; if your visualization supports an allowHtml option,
* you should set it to true.
*
* Here is an example of changing the formatted date values of a date column to
* use a long date format ("January 1, 2009"):
*
* @example
* var data = new google.visualization.DataTable();
* data.addColumn('string', 'Employee Name');
* data.addColumn('date', 'Start Date');
* data.addRows(3);
* data.setCell(0, 0, 'Mike');
* data.setCell(0, 1, new Date(2008, 1, 28));
* data.setCell(1, 0, 'Bob');
* data.setCell(1, 1, new Date(2007, 5, 1));
* data.setCell(2, 0, 'Alice');
* data.setCell(2, 1, new Date(2006, 7, 16));
*
* // Create a formatter.
* // This example uses object literal notation to define the options.
* var formatter = new google.visualization.DateFormat({formatType: 'long'});
*
* // Reformat our data.
* formatter.format(data, 1);
*
* // Draw our data
* var table = new google.visualization.Table(document.getElementById('dateformat_div'));
* table.draw(data, {showRowNumber: true});
*
*/
/**
* Google_Format
* @package Google
* @subpackage Google_Format
* @author Thomas Schaefer
* @copyright Thomas Schaefer
* @since 2009-05-20
* @desc Factory to load a formatter object
*
* The Google Visualization API provides formatters that can be used to reformat
* data in a visualization. These formatters change the formatted value of the
* specified column in all rows. Note that it does not modify the underlying
* values; just the formatted values. So, for example, the displayed value would
* be "$1,000.00" but the underlying value would still be "1000". Formatters can
* only affect one column at a time; to reformat multiple columns, apply a
* formatter to each column that you want to change.
*
* Important: Formatters can only be used with a DataTable; they cannot be used
* with a DataView (DataView objects are read-only).
*
* Here are the general steps for using a formatter:
*
* 1. Get your populated DataTable object.
* 2. For each column that you want to reformat:
*
* 1. Create an object that specifies all the options for your formatter.
* This is a basic JavaScript object with a set of properties and
* values. Look at your formatter's documentation to see what
* properties are supported. (Optionally, you can pass in an object
* literal notation object specifying your options.)
* 2. Create your formatter, passing in your options object.
* 3. Call formatter.Format(table, colIndex), passing in the DataTable
* and the (zero-based) column number of the data to reformat.
* Important: Many formatters require HTML tags to display special
* formatting; if your visualization supports an allowHtml option,
* you should set it to true.
*
* Here is an example of changing the formatted date values of a date column to
* use a long date format ("January 1, 2009"):
*
* @example
* var data = new google.visualization.DataTable();
* data.addColumn('string', 'Employee Name');
* data.addColumn('date', 'Start Date');
* data.addRows(3);
* data.setCell(0, 0, 'Mike');
* data.setCell(0, 1, new Date(2008, 1, 28));
* data.setCell(1, 0, 'Bob');
* data.setCell(1, 1, new Date(2007, 5, 1));
* data.setCell(2, 0, 'Alice');
* data.setCell(2, 1, new Date(2006, 7, 16));
*
* // Create a formatter.
* // This example uses object literal notation to define the options.
* var formatter = new google.visualization.DateFormat({formatType: 'long'});
*
* // Reformat our data.
* formatter.format(data, 1);
*
* // Draw our data
* var table = new google.visualization.Table(document.getElementById('dateformat_div'));
* table.draw(data, {showRowNumber: true});
*
*/
class Google_Format {
/**
* @var stdClass $properties
*/
protected $properties;
/**
* constructor
*/
public function __construct() {}
/**
* factory
* @desc factory class to call table formatter
* @param string $name
* @return instanceof Google_Format
*/
public static function factory($name) {
switch($name) {
case "Arrow":
/** @var Google_Format_Arrow $arrow */
$arrow = new Google_Format_Arrow;
return $arrow;
case "Bar":
/** @var Google_Format_Pattern $bar */
$bar = new Google_Format_Bar;
return $bar;
case "Color":
/** @var Google_Format_Color $color */
$color = new Google_Format_Color;
return $color;
case "Date":
/** @var Google_Format_Date $date */
$date = new Google_Format_Date;
return $date;
case "Number":
/** @var Google_Format_Number $number */
$number = new Google_Format_Number;
return $number;
case "Pattern":
/** @var Google_Format_Pattern $pattern */
$pattern = new Google_Format_Pattern;
return $pattern;
default:
throw new Google_Exception_Format("A Formatter Object named $name does not exist.");
break;
}
}
/**
* __set
* @desc give direct access to object properties
* @example
* $obj->setDecimalSymbol(1) is equal to $obj->decimalSymbol = 1;
* @param string $name
* @param mixed $value
*/
public function __set($name, $value) {
$this->setProperty($name, $value);
}
/**
* hasProperty
* @desc test if a chart type has a specific property
* @param string $name
* @return bool
*/
public function hasProperty($name, $properties) {
return array_key_exists($name, $properties);
}
/**
* addProperty
* @param string $name
* @param array $parameters
* @return void
*/
protected function addProperty($name, array $parameters) {
if(!$this->properties instanceof stdClass) {
$this->properties = new stdClass;
$this->properties->$name = array();
}
$this->properties->{$name}[] = $parameters;
}
/**
* setProperty
* @param string $name
* @param mixed $val
* @return void
*/
protected function setProperty($name, $val) {
$r = new ReflectionClass(get_class($this));
$arr = $r->getStaticProperties();
if($this->hasProperty($name, $arr["isRegistered"])) {
if(!$this->properties instanceof stdClass) {
$this->properties = new stdClass;
}
$this->properties->$name = $val;
} else {
$e = new Google_Exception_Format("Formatter ".__CLASS__." does not support a property named $name.");
echo $e->show();
}
return $this;
}
/**
* __call
* @desc sets a property while reflecting a formatter's registry
* @param string $name
* @param array $parameters
* @return void
*/
public function __call($name, $parameters) {
// analyse property name
$methodObject = Google_Base::getMethodType($name);
$methodType = $methodObject["type"];
$name = $methodObject["name"];
switch($methodType) {
case "set":
$firstDown = Google_Base::ucFirstDown($name);
$this->setProperty($firstDown, $parameters[0]);
break;
}
}
/**
* @return string empty string
*/
public function __toString() {
return '';
}
}