-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfusioncharts.php
225 lines (189 loc) · 9.21 KB
/
fusioncharts.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
<?php
class FusionCharts {
private $eventOptions = array();
private $constructorOptions = array();
private $baseTemplate = '
<script type="text/javascript">
FusionCharts.ready(function () {
__FT__
__FC__
});
</script>';
private $constructorTemplate = 'new FusionCharts(__constructorOptions__);';
private $renderTemplate = 'FusionCharts("__chartId__").render();';
private $eventTemplate = 'FusionCharts("__chartId__").addEventListener("_fceventname_",_fceventbody_);';
// constructor
function __construct($type, $id, $width = 400, $height = 300, $renderAt, $dataFormat= null, $dataSource) {
isset($type) ? $this->constructorOptions['type'] = $type : '';
isset($id) ? $this->constructorOptions['id'] = $id : 'php-fc-'.time();
isset($width) ? $this->constructorOptions['width'] = $width : '';
isset($height) ? $this->constructorOptions['height'] = $height : '';
isset($renderAt) ? $this->constructorOptions['renderAt'] = $renderAt : '';
isset($dataFormat) ? $this->constructorOptions['dataFormat'] = $dataFormat : '';
isset($dataSource) ? $this->constructorOptions['dataSource'] = $dataSource : '';
}
//Add event
function addEvent($eventName, $funcName){
isset($eventName) ? $this->eventOptions[$eventName] = $funcName : '';
}
//Add message
function addMessage($messageName, $messageValue){
isset($messageName) ? $this->constructorOptions[$messageName] = $messageValue : '';
}
// render the chart created
// It prints a script and calls the FusionCharts javascript render method of created chart
function render() {
$timeSeries = null;
$tempArray = array();
foreach($this->constructorOptions as $key => $value) {
if ($key === 'dataSource') {
$tempArray['dataSource'] = '__dataSource__';
} else {
$tempArray[$key] = $value;
}
}
$jsonEncodedOptions = json_encode($tempArray);
if (is_object($this->constructorOptions['dataSource']))
{
if (get_class($this->constructorOptions['dataSource']) === 'TimeSeries'){
$timeSeries = $this->constructorOptions['dataSource'];
}
} else {
$dataFormat = $this->constructorOptions['dataFormat'];
if ($dataFormat === 'json') {
$jsonEncodedOptions = preg_replace('/\"__dataSource__\"/', $this->constructorOptions['dataSource'], $jsonEncodedOptions);
} elseif ($dataFormat === 'xml') {
$jsonEncodedOptions = preg_replace('/\"__dataSource__\"/', '\'__dataSource__\'', $jsonEncodedOptions);
$jsonEncodedOptions = preg_replace('/__dataSource__/', $this->constructorOptions['dataSource'], $jsonEncodedOptions);
} elseif ($dataFormat === 'xmlurl') {
$jsonEncodedOptions = preg_replace('/__dataSource__/', $this->constructorOptions['dataSource'], $jsonEncodedOptions);
} elseif ($dataFormat === 'jsonurl') {
$jsonEncodedOptions = preg_replace('/__dataSource__/', $this->constructorOptions['dataSource'], $jsonEncodedOptions);
}
}
$tempData = preg_replace('/__constructorOptions__/', $jsonEncodedOptions, $this->constructorTemplate);
foreach($this->eventOptions as $key => $value) {
$tempEvtTmp = preg_replace('/__chartId__/', $this->constructorOptions['id'], $this->eventTemplate);
$tempEvtTmp = preg_replace('/_fceventname_/', $key, $tempEvtTmp);
$tempEvtTmp = preg_replace('/_fceventbody_/', $value, $tempEvtTmp);
$tempData .= $tempEvtTmp;
}
$tempData .= preg_replace('/__chartId__/', $this->constructorOptions['id'], $this->renderTemplate);
$renderHTML = preg_replace('/__FC__/', $tempData, $this->baseTemplate);
if ($timeSeries){
$renderHTML = preg_replace('/__FT__/', $timeSeries->GetDataStore(), $renderHTML);
$renderHTML = preg_replace('/\"__dataSource__\"/', $timeSeries->GetDataSource(), $renderHTML);
}else{
$renderHTML = preg_replace('/__FT__/', '', $renderHTML);
}
echo $renderHTML;
}
}
class TimeSeries {
private $fusionTableObject = null;
private $attributesList = array();
function __construct($fusionTable) {
$this->fusionTableObject = $fusionTable;
}
function AddAttribute($key, $value){
$attribute = array();
$attribute[$key] = $value;
array_push($this->attributesList, $attribute);
}
function GetDataSource(){
$stringData = '';
$format = '%s:%s,';
foreach ($this->attributesList as $attribute) {
$attribKey = key($attribute);
$stringData .= sprintf($format, $attribKey, $attribute[$attribKey]) . "\r\n";
}
$stringData .= sprintf('%s:%s', 'data', 'fusionTable');
return "{" . "\r\n" . $stringData . "\r\n" . "}";
}
function GetDataStore(){
return $this->fusionTableObject->GetDataTable();
}
}
abstract class OrderBy {
const ASC = 0;
const DESC = 1;
}
abstract class FilterType {
const Equals = 0;
const Greater = 1;
const GreaterEquals = 2;
const Less = 3;
const LessEquals = 4;
const Between = 5;
}
class FusionTable {
private $stringData = '';
function __construct($schema, $data) {
$this->stringData .="let schema = " . $schema . ";\r\n";
$this->stringData .="let data = " . $data . ";\r\n";
$this->stringData .="let fusionDataStore = new FusionCharts.DataStore();\r\n";
$this->stringData .="let fusionTable = fusionDataStore.createDataTable(data, schema);\r\n";
}
function Select(...$columnName) {
if (count($columnName) > 0 ) {
$selectData = sprintf("'%s'", implode("','", $columnName));
$this->stringData .= "fusionTable = fusionTable.query(FusionCharts.DataStore.Operators.select([" . $selectData . "]));". "\r\n";
}
}
function Sort($columnName, $columnOrderBy) {
$sortData = sprintf("sort([{column: '%s', order: '%s'}])", $columnName, (OrderBy::ASC === $columnOrderBy) ? "asc" : "desc");
$this->stringData .= "fusionTable = fusionTable.query(" . $sortData . ");". "\r\n";
}
function CreateFilter($filterType, $columnName, ...$values) {
$filterData = '';
if (count($values) > 0 ) {
$refl = new ReflectionClass('FilterType');
$constants = $refl->getConstants();
$constName = null;
foreach ( $constants as $name => $value )
{
if ( $value == $filterType )
{
$constName = lcfirst($name);
break;
}
}
if ($constName) {
switch ($filterType) {
case FilterType::Equals:
$filterData = sprintf("FusionCharts.DataStore.Operators.%s('%s','%s')", $constName, $columnName, $values[0]);
break;
case FilterType::Between:
if (count($values) > 1 ) {
$filterData = sprintf("FusionCharts.DataStore.Operators.%s('%s',%s,%s)", $constName, $columnName, $values[0], $values[1]);
}
break;
default:
$filterData = sprintf("FusionCharts.DataStore.Operators.%s('%s',%s)", $constName, $columnName, $values[0]);
}
}
}
return $filterData;
}
function ApplyFilter($filter) {
if (strlen($filter)>0){
$this->stringData .= "fusionTable = fusionTable.query(" . $filter . ");". "\r\n";
}
}
function ApplyFilterByCondition($filter) {
if (strlen($filter)>0){
$this->stringData .= "fusionTable = fusionTable.query(" . $filter . ");". "\r\n";
}
}
function Pipe(...$filters) {
$filterData='';
if (count($filters) > 0 ) {
$filterData = sprintf("%s", implode(",", $filters));
$this->stringData .= "fusionTable = fusionTable.query(FusionCharts.DataStore.Operators.pipe(" . $filterData . "));". "\r\n";
}
}
function GetDataTable() {
return $this->stringData;
}
}
?>