Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Kvberlin\FusionCharts;
- use Zend\Config\Factory;
- use Zend\Debug\Debug;
- /**
- * ZendFramework3 Fusion
- * Autor: Peter Wendel (ITA),- 897
- * Datum: 01.12.2017
- *
- * Kurzbeschreibung:
- * Neue Version der Klasse FusionCharts
- *
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * Created by PhpStorm.
- */
- class Fusion
- {
- const FUSION_VERSION = 'v1.0.1 albernerAal';
- /**
- * @var string
- */
- private $constructorTemplate = '
- <script type="text/javascript">
- FusionCharts.ready(function () {
- new FusionCharts(__constructorOptions__);
- });
- </script>';
- /**
- * @var string
- */
- private $renderTemplate = '
- <script type="text/javascript">
- FusionCharts.ready(function () {
- FusionCharts("__chartId__").render();
- });
- </script>
- ';
- /**
- * Datenformat festlegen
- * @var string
- */
- private $dataFormat = 'json';
- /**
- * Gibt das fertige Html zurück.
- * @var string
- */
- private $chartHtml;
- /**
- * Typ des ausgewählten Charts
- * @var string
- */
- private $chartType;
- /**
- * Legt eine eineindeuitge Id des DIVs an, in dem gereandert werden soll.
- * @var string
- */
- private $chartId;
- /**
- * Canvas-Breite
- * @var int
- */
- private $width= 400;
- /**
- * Canvas Höhe
- * @var int
- */
- private $height = 300;
- /**
- *
- * Eindeutige DIV-Id zur Erzeugung.
- * @var string
- */
- private $divId;
- /**
- * Eintrag der Datenquelle/Optionen
- * @var string
- */
- private $dataSource = '';
- /**
- * Enthält den gesamten Inhalt der Config-Datei
- * @var array|\Zend\Config\Config
- */
- private $fusionChart=[];
- /**
- * Globale Chart-Optionen
- * @var array|mixed
- */
- private $globalOptions=[];
- /**
- * Benutzerdefinierte Chart-Optionen
- * @var array
- */
- private $customOptions=[];
- /**
- * Besondere Optionen des speziellen ChartTyps
- * @var array
- */
- private $chartOptions = [];
- /**
- * Gesamt-Array-Options
- * @var array
- */
- private $finalOptions=[];
- /**
- * Die Daten, die angezeigt werden sollen
- * @var array
- */
- private $chartData;
- /**
- * Daten für die Kategorien, sofern für das Chart benötigt
- * @var array
- */
- private $catData;
- /**
- * @var array
- */
- private $trendlineData;
- /**
- * @var array
- */
- private $customConfig;
- /**
- * @var array
- */
- private $colors = ['#FF9900','#FFC500','#1B0FB5','#0857AA','#FFB545','#FFD445','#5C52DC','#468DD7','#FF9D0C','#FFC70C','#3326D5','#1972CF'];
- /*
- * Chart-Typen
- */
- const FC_COLUMN_2D = 'column2d';
- const FC_PIE_2D = 'pie2d';
- const FC_MULTISERIES_COLUMN_2D = 'mscolumn2d';
- const FC_STACKED_COLUMN_2D = 'stackedcolumn2d';
- const FC_DOUGHNUT_2D = 'doughnut2d';
- const FC_DOUGHNUT_3D = 'doughnut3d';
- const FC_MARIMEKKO = 'marimekko';
- const FC_BUBBLE_CHART_2D = 'bubble';
- const FC_BAR_2D = 'bar2d';
- /**
- * Fusion constructor.
- */
- public function __construct(){
- $this->setChartId();
- $this->setDivId();
- $this->setChartType(self::FC_COLUMN_2D);
- $this->fusionChart = Factory::fromFile(__DIR__.'/fusion_charts_config.php');
- $this->globalOptions = $this->fusionChart['global'];
- }
- /**
- * Benutzerdefinierte Chart-Options setzen
- * @param string $option
- * @param string $value
- */
- public function setChartOption(string $option, string $value){
- @$this->customOptions[$option] = $value;
- }
- /**
- * Nach einer bestimmten Option fragen.
- * @param string $option
- * @return mixed|null
- */
- public function getChartOption(string $option){
- $array = $this->getFinalOptions();
- $ret = array_key_exists($option) ? $array[$option] : null;
- return $ret;
- }
- /**
- * @param string|null $chartId
- */
- public function setChartId(string $chartId = null){
- $this->chartId = is_null($chartId) ? 'fusion_chart_' . mt_rand(0, 100000) : $chartId;
- }
- /**
- * @return string
- */
- public function getChartId(){
- return $this->chartId;
- }
- /**
- * @param string|null $divId
- */
- public function setDivId(string $divId=null){
- $this->divId = is_null($divId) ? 'div_id_' . mt_rand(0, 100000) : $divId;
- }
- /**
- * @return string
- */
- public function getDivId(){
- return $this->divId;
- }
- /**
- * @param int $width
- * @param int $height
- */
- public function setCanvasSize(int $width = 400, int $height =300){
- $this->width = $width;
- $this->height = $height;
- }
- /**
- * @return int
- */
- public function getCanvasWidth(){
- return $this->width;
- }
- /**
- * @return int
- */
- public function getCanvasHeight(){
- return $this->height;
- }
- /**
- * @param $dataSource
- */
- public function setDataSource($dataSource){
- $this->dataSource = $dataSource;
- }
- /**
- * @return string
- */
- public function getDataSource(){
- return $this->dataSource;
- }
- /**
- * @param string $format
- */
- public function setDataFormat($format = 'json'){
- $this->dataFormat = $format;
- }
- /**
- * @return string
- */
- public function getDataFormat(){
- return $this->dataFormat;
- }
- /**
- * @param string $chartType
- */
- public function setChartType(string $chartType){
- $this->chartType = $chartType;
- }
- /**
- * @return string
- */
- public function getChartType(){
- return $this->chartType;
- }
- /**
- * Schreibt die Werte der Constructor-Options in den Quelltext:
- * type,id,width,height,divId,dataFormat, dataSource
- */
- private function writeHtmlText(){
- $tempArray = [
- 'type' => $this->getChartType(),
- 'id' => $this->getChartId(),
- 'width' => $this->getCanvasWidth(),
- 'height' => $this->getCanvasHeight(),
- 'renderAt' => $this->getDivId(),
- 'dataFormat'=> $this->getDataFormat(),
- 'dataSource'=> '__dataSource__'
- ];
- $jsonEncodedOptions = json_encode($tempArray);
- $this->dataSource= $this->getFinalOptions();
- if ($this->dataFormat === 'json') {
- $jsonEncodedOptions = preg_replace('/\"__dataSource__\"/', json_encode($this->dataSource), $jsonEncodedOptions);
- } elseif ($this->dataFormat === 'xml') {
- $jsonEncodedOptions = preg_replace('/\"__dataSource__\"/', '\'__dataSource__\'', $jsonEncodedOptions);
- $jsonEncodedOptions = preg_replace('/__dataSource__/', json_encode($this->dataSource), $jsonEncodedOptions);
- } elseif ($this->dataFormat === 'xmlurl') {
- $jsonEncodedOptions = preg_replace('/__dataSource__/', json_encode($this->dataSource), $jsonEncodedOptions);
- } elseif ($this->dataFormat === 'jsonurl') {
- $jsonEncodedOptions = preg_replace('/__dataSource__/', json_encode($this->dataSource), $jsonEncodedOptions);
- }
- $this->chartHtml = preg_replace('/__constructorOptions__/', $jsonEncodedOptions, $this->constructorTemplate);
- }
- /**
- * render the chart created
- * It prints a script and calls the FusionCharts javascript render method of created chart
- *
- * @return string
- */
- function render() {
- $this->writeHtmlText();
- $renderHTML = preg_replace('/__chartId__/', $this->getChartId(), $this->renderTemplate);
- return $this->chartHtml."\n".$renderHTML;
- }
- /**
- * Generiert die finalOptions, die zum Rendern benutzt werden.
- * Setzt diese aus allen konfigurierten Daten zusammen:
- * - Options
- * - Kategorien
- * - Trendlines
- */
- private function setFinalOptions(){
- // Optionen aus dem Bereich "Chart"
- $this->finalOptions['chart'] = array_merge(
- $this->globalOptions,
- $this->chartOptions,
- $this->customOptions
- );
- // Angabe von Kategorien, sofern benötigt
- if (isset($this->catData)) {
- $this->finalOptions['categories'] = $this->getCategories();
- }
- if (isset($this->trendlineData)){
- $this->finalOptions['trendlines'] = $this->getTrendlines();
- }
- if (array_key_exists('data',$this->getChartData())):
- $this->finalOptions['dataset']= $this->getChartData();
- //Debug::dump($this->getChartData(),'<h4>ChartData - Fusion</h4>');
- else:
- $this->finalOptions['data']=$this->getChartData();
- endif;
- }
- /**
- * Gesamt-Array aller Optionen des Charts
- * @return array
- */
- public function getFinalOptions(){
- $this->setFinalOptions();
- return $this->finalOptions;
- }
- /**
- * Spezielle Chart-Typ-Options (von der Chart-Klasse abgelegt)
- * @param array $options
- */
- public function setChartTypeOptions(array $options){
- $this->chartOptions = $options;
- }
- /**
- * Spezielle Chart-Typ-Options
- * @return array
- */
- public function getChartTypeOptions(){
- return $this->chartOptions;
- }
- public function setCategories(array $cats){
- /**
- * Ausgangsarray
- * [ Cat1, Cat2, Cat3, Cat4....]
- * oder
- * [ Cat1 => [x-value, showverticalline],Cat2 => [..,..],... ]
- */
- foreach($cats as $key => $value):
- $array = is_array($value) ? [
- 'label' => $key,
- 'x' => $value[0],
- 'showverticalline' => $value[1]
- ] : ['label' => $value];
- @$catArray['category'][]=$array;
- endforeach;
- $this->catData = $catArray;
- }
- public function getCategories(){
- return $this->catData;
- }
- /**
- * Hier eingeworfene Arrays werden passend zum Chart-Typ
- * in Data-Sets für die Anzeige umgewandelt.
- * @param string $chartType
- * @param array $data
- */
- public function setChartData(string $chartType, array $data){
- switch ($chartType):
- case self::FC_STACKED_COLUMN_2D:
- $this->setSeriesDataArray($data);
- break;
- case self::FC_MARIMEKKO:
- $this->setSeriesDataArray($data);
- break;
- case self::FC_BUBBLE_CHART_2D:
- $this->setBubbleDataArray($data);
- break;
- case self::FC_MULTISERIES_COLUMN_2D:
- $this->setSeriesDataArray($data);
- break;
- default:
- $this->setDefaultDataArray($data);
- break;
- endswitch;
- }
- /**
- * Umwandlung in das Default-Data-Set für einfache Charts
- * @param array $dataArray
- */
- private function setDefaultDataArray(array $dataArray){
- /**
- * Ausgangsarray:
- * [ Labeltext => Wert ]
- */
- $data = [];
- foreach($dataArray as $label => $value):
- $array = [
- 'label' => $label,
- 'value' => $value
- ];
- @$data[]=$array;
- endforeach;
- $this->setDataArray($data);
- }
- private function setSeriesDataArray(array $dataArray){
- /**
- * Ausgangsarray:
- * [ Seriesname => array (wert1,wert2,wert3,wert4...) ]
- */
- $dataSet=[];
- foreach ($dataArray as $series => $valueArray):
- $array = [
- 'seriesname' => $series,
- 'data' => $valueArray
- ];
- @$dataSet[]=$array;
- endforeach;
- $this->setDataArray($dataSet);
- }
- private function setBubbleDataArray(array $dataArray){
- /**
- * Ausgangsarray
- * [ Name => [x, y, z] ]
- */
- @$BubbleSet['color'] = '#00aee4';
- $z = 0;
- foreach($dataArray as $key => $valueArray):
- if ($z > 9) $z=0;
- $array = [
- 'x' => $valueArray[0],
- 'y' => $valueArray[1],
- 'z' => $valueArray[2],
- 'name' => $key,
- 'color' => $this->colors[$z]
- ];
- @$BubbleSet['data'][]=$array;
- $z +=1;
- endforeach;
- $this->setDataArray($BubbleSet);
- }
- /**
- * Array für Data-Set setzen
- * @param array $data
- */
- private function setDataArray(Array $data){
- $this->chartData = $data;
- }
- /**
- * Array für das Data-Set holen
- * @return mixed
- */
- public function getChartData(){
- return $this->chartData;
- }
- public function setTrendlines(array $data,bool $vTrendline=false){
- /**
- * Ausgangsarray:
- * [ 'startvalue => 12205,'color' => '#0075c2',... ]
- */
- if($vTrendline):
- if (!isset($this->trendlineData['vTrendlines'])) @$this->trendlineData['vTrendlines']['line'];
- $this->trendlineData['vTrendlines']['line'][]=$data;
- else:
- if (!isset($this->trendlineData['trendlines'])) @$this->trendlineData['trendlines']['line'];
- $this->trendlineData['trendlines']['line'][]=$data;
- endif;
- }
- public function getTrendlines(){
- return $this->trendlineData;
- }
- public function getConfig(string $abschnitt){
- return array_key_exists($abschnitt, $this->fusionChart) ? $this->fusionChart[$abschnitt] : null;
- }
- public function setCustomOption(array $options){
- $this->customOptions = array_merge($this->customOptions,$options);
- }
- public function setCustomConfigFile(string $path){
- $this->customConfig = Factory::fromFile($path);
- }
- public function getFromCustomConfigFile(string $abschnitt){
- return array_key_exists($abschnitt, $this->customConfig) ? $this->customConfig[$abschnitt] : null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement