| 1 |
<?php |
| 2 |
|
| 3 |
class FusionCharts { |
| 4 |
|
| 5 |
private $constructorOptions = array(); |
| 6 |
|
| 7 |
private $constructorTemplate = ' |
| 8 |
<script type="text/javascript"> |
| 9 |
FusionCharts.ready(function () { |
| 10 |
new FusionCharts(__constructorOptions__); |
| 11 |
}); |
| 12 |
</script>'; |
| 13 |
|
| 14 |
private $renderTemplate = ' |
| 15 |
<script type="text/javascript"> |
| 16 |
FusionCharts.ready(function () { |
| 17 |
FusionCharts("__chartId__").render(); |
| 18 |
}); |
| 19 |
</script> |
| 20 |
'; |
| 21 |
|
| 22 |
// constructor |
| 23 |
function __construct($type, $id, $width = 400, $height = 300, $renderAt = null, $dataFormat = null, $dataSource = null) { |
| 24 |
isset($type) ? $this->constructorOptions['type'] = $type : ''; |
| 25 |
isset($id) ? $this->constructorOptions['id'] = $id : 'php-fc-'.time(); |
| 26 |
isset($width) ? $this->constructorOptions['width'] = $width : ''; |
| 27 |
isset($height) ? $this->constructorOptions['height'] = $height : ''; |
| 28 |
isset($renderAt) ? $this->constructorOptions['renderAt'] = $renderAt : ''; |
| 29 |
isset($dataFormat) ? $this->constructorOptions['dataFormat'] = $dataFormat : ''; |
| 30 |
isset($dataSource) ? $this->constructorOptions['dataSource'] = $dataSource : ''; |
| 31 |
|
| 32 |
$tempArray = array(); |
| 33 |
foreach($this->constructorOptions as $key => $value) { |
| 34 |
if ($key === 'dataSource') { |
| 35 |
$tempArray['dataSource'] = '__dataSource__'; |
| 36 |
} else { |
| 37 |
$tempArray[$key] = $value; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
$jsonEncodedOptions = json_encode($tempArray); |
| 42 |
|
| 43 |
if ($dataFormat === 'json') { |
| 44 |
$jsonEncodedOptions = preg_replace('/\"__dataSource__\"/', $this->constructorOptions['dataSource'], $jsonEncodedOptions); |
| 45 |
} elseif ($dataFormat === 'xml') { |
| 46 |
$jsonEncodedOptions = preg_replace('/\"__dataSource__\"/', '\'__dataSource__\'', $jsonEncodedOptions); |
| 47 |
$jsonEncodedOptions = preg_replace('/__dataSource__/', $this->constructorOptions['dataSource'], $jsonEncodedOptions); |
| 48 |
} elseif ($dataFormat === 'xmlurl') { |
| 49 |
$jsonEncodedOptions = preg_replace('/__dataSource__/', $this->constructorOptions['dataSource'], $jsonEncodedOptions); |
| 50 |
} elseif ($dataFormat === 'jsonurl') { |
| 51 |
$jsonEncodedOptions = preg_replace('/__dataSource__/', $this->constructorOptions['dataSource'], $jsonEncodedOptions); |
| 52 |
} |
| 53 |
$newChartHTML = preg_replace('/__constructorOptions__/', $jsonEncodedOptions, $this->constructorTemplate); |
| 54 |
|
| 55 |
echo $newChartHTML; |
| 56 |
} |
| 57 |
|
| 58 |
// render the chart created |
| 59 |
// It prints a script and calls the FusionCharts javascript render method of created chart |
| 60 |
function render() { |
| 61 |
$renderHTML = preg_replace('/__chartId__/', $this->constructorOptions['id'], $this->renderTemplate); |
| 62 |
echo $renderHTML; |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
?> |
| 67 |
|