| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805 |
- <?php
- namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Graphs;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Builder\BaseContainer;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\AjaxElementInterface;
- use \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Exceptions\Exception;
- use \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\ErrorCodes\ErrorCodesLib;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\Models\ModuleSettings\Model;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Graphs\Models\Data;
- use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Graphs\Models\DataSet;
- use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Hidden;
- use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
- /**
- * Description of EmptyGraph
- *
- * @author inbs
- */
- class EmptyGraph extends BaseContainer implements AjaxElementInterface
- {
- use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits\TableRowCol;
- use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits\Icon;
- use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits\TitleButtons;
-
- const GRAPH_FILTER_TYPE_INT = 'int';
- const GRAPH_FILTER_TYPE_STRING = 'string';
- const GRAPH_FILTER_TYPE_DATE = 'date';
-
- const GRAPH_OPTIONS_MODE_POINT = 'point';
- const GRAPH_OPTIONS_MODE_NEAREST = 'nearest';
- const GRAPH_OPTIONS_MODE_INDEX = 'index';
- const GRAPH_OPTIONS_MODE_DATASET = 'dataset';
- const GRAPH_OPTIONS_MODE_X = 'x';
- const GRAPH_OPTIONS_MODE_Y = 'y';
-
- protected $id = 'emptyGraph';
- protected $name = 'emptyGraph';
- protected $title = 'title';
- protected $configCharts = [
- 'type' => '',
- 'data' => [],
- 'dataParse' => [],
- 'options' => [],
- 'filter' => []
- ];
-
- protected $tooltips = [
- 'mode' => self::GRAPH_OPTIONS_MODE_INDEX
- ];
-
- protected $responsive = true;
- protected $responsiveAnimationDuration = null;
- protected $maintainAspectRatio = null;
- protected $onResize = null;
-
- protected $filterInfo = [
- 'displayEditColor' => true,
- 'type' => self::GRAPH_FILTER_TYPE_INT,
- 'default' => [
- 'start' => 0,
- 'end' => 100
- ]
- ];
-
- protected $animation = [];
- protected $layout = [];
- protected $scales = [
- 'xAxes' => [
- [
- 'ticks' => [
- 'beginAtZero' => true
- ]
- ]
- ]
- ];
- protected $legend = [
- 'display' => true,
- 'position' => 'top'
- ];
- protected $titleGraph = [
- 'display' => false,
- 'text' => '',
- 'position' => 'top'
- ];
- protected $graphWidth = null;
- protected $graphHeight = 400;
- protected $vueComponent = true;
- protected $defaultVueComponentName = 'mg-graph';
- protected $graphData = null;
- protected $graphSettingsKey = null;
- protected $graphSettingsEnabled = false;
- protected $configurationFields = [];
- public function returnAjaxData()
- {
- if ($this->getRequestValue('loadData', false) === 'settingButton')
- {
- $settingButton = $this->titleButtons['settingButton'];
- return $settingButton->returnAjaxData();
- }
- $this->loadData();
-
- $this->setTitleText(sl('lang')->T($this->name));
- $this->reloadConfigCharts();
- $this->addColorForLabels();
- $this->useFilter();
- return (new ResponseTemplates\RawDataJsonResponse($this->configCharts))->setCallBackFunction($this->callBackFunction)->setRefreshTargetIds($this->refreshActionIds);
- }
-
- protected function disableEditColor()
- {
- $this->filterInfo['displayEditColor'] = false;
-
- return $this;
- }
-
- protected function enableEditColor()
- {
- $this->filterInfo['displayEditColor'] = true;
-
- return $this;
- }
-
- protected function setGraphFilterInfo($type = self::GRAPH_FILTER_TYPE_INT, $defaultStart = 0, $defaultStop = 100)
- {
- if ($type !== null && in_array($type, [self::GRAPH_FILTER_TYPE_INT, self::GRAPH_FILTER_TYPE_DATE, self::GRAPH_FILTER_TYPE_STRING], true))
- {
- $this->filterInfo['type'] = $type;
- }
-
- if ($defaultStart !== null)
- {
- $this->filterInfo['default']['start'] = $defaultStart;
- }
-
- if ($defaultStop !== null)
- {
- $this->filterInfo['default']['end'] = $defaultStop;
- }
-
- return $this;
- }
-
- protected function getActionId()
- {
- return $this->getRequestValue('index', false);
- }
-
- protected function reloadConfigCharts()
- {
- if (is_array($this->animation) && count($this->animation) > 0)
- {
- $this->configCharts['options']['animation'] = $this->animation;
- }
- elseif (is_string($this->animation) && strlen($this->animation) > 0)
- {
- $this->configCharts['options']['animation'] = json_decode($this->animation, true);
- }
-
- if (is_array($this->layout) && count($this->layout) > 0)
- {
- $this->configCharts['options']['layout'] = $this->layout;
- }
-
- if (is_array($this->legend) && count($this->legend) > 0)
- {
- $this->configCharts['options']['legend'] = $this->legend;
- }
-
- if (is_array($this->titleGraph) && count($this->titleGraph) > 0)
- {
- $this->configCharts['options']['title'] = $this->titleGraph;
- }
-
- if (is_array($this->scales) && count($this->scales) > 0)
- {
- $this->configCharts['options']['scales'] = $this->scales;
- }
-
- if (is_array($this->filterInfo) && count($this->filterInfo) > 0)
- {
- $this->configCharts['filter'] = $this->filterInfo;
- }
-
- if ($this->responsive !== null)
- {
- $this->configCharts['options']['responsive'] = $this->responsive;
- }
-
- if ($this->responsiveAnimationDuration!== null)
- {
- $this->configCharts['options']['responsiveAnimationDuration'] = $this->responsiveAnimationDuration;
- }
-
- if ($this->maintainAspectRatio!== null)
- {
- $this->configCharts['options']['maintainAspectRatio'] = $this->maintainAspectRatio;
- }
-
- if ($this->onResize !== null)
- {
- $this->configCharts['options']['onResize'] = $this->onResize;
- }
-
-
- return $this;
- }
-
- protected function setChartScales(array $scales = [])
- {
- $this->scales = $scales;
- return $this;
- }
- protected function addChartScale($key = null, array $scales = [])
- {
- if (trim($key) !== '' && is_string($key) && !isset($this->scales[$key]))
- {
- $this->scales[$key] = $scales;
- }
- return $this;
- }
- protected function updateChartScale($key = null, array $scales = [])
- {
- if (trim($key) !== '' && is_string($key))
- {
- $this->scales[$key] = $scales;
- }
- return $this;
- }
-
- protected function setTooltipsMode($tooltipMode = 'index')
- {
- if (!in_array($tooltipMode, [self::GRAPH_OPTIONS_MODE_DATASET, self::GRAPH_OPTIONS_MODE_INDEX, self::GRAPH_OPTIONS_MODE_NEAREST, self::GRAPH_OPTIONS_MODE_POINT, self::GRAPH_OPTIONS_MODE_X, self::GRAPH_OPTIONS_MODE_Y]))
- {
- throw new Exception(ErrorCodesLib::CORE_GRA_000001, ['mode' => $tooltipMode]);
- }
- $this->tooltips['mode'] = $tooltipMode;
-
- return $this;
- }
-
- protected function setTitleDisplay($display = true)
- {
- $this->titleGraph['display'] = (bool)$display;
-
- return $this;
- }
-
- protected function setTitlePosition($position = 'top')
- {
- $this->titleGraph['position'] = (string)$position;
-
- return $this;
- }
-
- protected function setTitleFontSize($fontSize = 12)
- {
- $this->titleGraph['fontSize'] = (int)$fontSize;
-
- return $this;
- }
-
- protected function setTitleFontFamily($fontFamily = "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif")
- {
- $this->titleGraph['fontFamily'] = (string)$fontFamily;
-
- return $this;
- }
-
- protected function setTitleFontColor($fontColor = '#666')
- {
- $this->titleGraph['fontColor'] = (string)$fontColor;
-
- return $this;
- }
-
- protected function setTitleFontStyle($fontStyle = '')
- {
- $this->titleGraph['fontStyle'] = (string)$fontStyle;
-
- return $this;
- }
-
- protected function setTitlePadding($padding = '')
- {
- $this->titleGraph['padding'] = (string)$padding;
-
- return $this;
- }
-
- protected function setTitleLineHeight($lineHeight = 1.2)
- {
- $this->titleGraph['lineHeight'] = (float)$lineHeight;
-
- return $this;
- }
-
- protected function setTitleText($text = '')
- {
- $this->titleGraph['text'] = (string)$text;
-
- return $this;
- }
- protected function unsetLegend()
- {
- $this->legend = [];
-
- return $this;
- }
-
- protected function setLegendLabelsBoxWidth($boxWidth = 40)
- {
- $this->legend['labels']['boxWidth'] = (int)$boxWidth;
-
- return $this;
- }
-
- protected function setLegendLabelsFontSize($fontSize = 12)
- {
- $this->legend['labels']['fontSize'] = (int)$fontSize;
-
- return $this;
- }
-
- protected function setLegendLabelsFontStyle($fontStyle = 'normal')
- {
- $this->legend['labels']['fontStyle'] = (string)$fontStyle;
-
- return $this;
- }
-
- protected function setLegendLabelsFontColor($fontColor = '#666') // Demon Color 3:-]
- {
- $this->legend['labels']['fontColor'] = (string)$fontColor;
-
- return $this;
- }
-
- protected function setLegendLabelsFontFamily($fontFamily = "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif") // Demon Color 3:-]
- {
- $this->legend['labels']['fontFamily'] = (string)$fontFamily;
-
- return $this;
- }
-
- protected function setLegendLabelsPadding($padding = 10)
- {
- $this->legend['labels']['padding'] = (int)$padding;
-
- return $this;
- }
-
- protected function setLegendLabelsGenerateLabels($generateLabels = "function () {}")
- {
- $this->legend['labels']['generateLabels'] = (string)$generateLabels;
-
- return $this;
- }
-
- protected function setLegendLabelsFilter($filter = "function () {}")
- {
- $this->legend['labels']['filter'] = (string)$filter;
-
- return $this;
- }
-
- protected function setLegendLabelsUsePointStyle($usePointStyle = false)
- {
- $this->legend['labels']['usePointStyle'] = (bool)$usePointStyle;
-
- return $this;
- }
-
- protected function setLegendReverse($isReverse = false)
- {
- $this->legend['reverse'] = (bool)$isReverse;
-
- return $this;
- }
-
- protected function setLegendOnHover($eventFunction = "function (event, legendItem) {}")
- {
- $this->legend['onHover'] = (string)$eventFunction;
-
- return $this;
- }
-
- protected function setLegendOnClick($eventFunction = "function (event, legendItem) {}")
- {
- $this->legend['onClick'] = (string)$eventFunction;
-
- return $this;
- }
-
- protected function setLegendFullWidth($isFullWidth = true)
- {
- $this->legend['fullWidth'] = (bool)$isFullWidth;
-
- return $this;
- }
-
- protected function setLegendDisplay($isDisplay = true)
- {
- $this->legend['display'] = (bool)$isDisplay;
-
- return $this;
- }
-
- protected function setLegendPosition($position = 'top')
- {
- $this->legend['position'] = (string)$position;
-
- return $this;
- }
- protected function unsetLayout()
- {
- $this->layout = [];
-
- return $this;
- }
-
- protected function setLayoutPadding($left = 0, $right = 0, $top = 0, $bottom = 0)
- {
- $this->layout['padding'] = [
- 'left' => $left,
- 'right' => $right,
- 'top' => $top,
- 'bottom'=> $bottom
- ];
-
- return $this;
- }
-
- protected function setAnimation($animation)
- {
- $this->animation = $animation;
-
- return $this;
- }
-
- protected function addAnimation($event, $animation)
- {
- if (is_array($this->animation))
- {
- $this->animation[$event] = $animation;
- }
- elseif (is_string($this->animation))
- {
- $animations = json_decode($this->animation, true);
- $animations[$event] = $animation;
- $this->animation = json_encode($animations);
- }
-
- return $this;
- }
-
- public function setGraphWidth($graphWidth = 0)
- {
- if (!is_numeric($graphWidth))
- {
- throw new Exception(ErrorCodesLib::CORE_GRA_000002, ['width' => $graphWidth], ['width' => $graphWidth]);
- }
-
- $this->graphWidth = $graphWidth;
-
- return $this;
- }
-
- public function setGraphHeight($graphHeight = 0)
- {
- if (!is_numeric($graphHeight))
- {
- throw new Exception(ErrorCodesLib::CORE_GRA_000003, ['height' => $graphHeight], ['height' => $graphHeight]);
- }
-
- $this->graphHeight = $graphHeight;
-
- return $this;
- }
-
- public function getGraphWidth()
- {
- return $this->graphWidth;
- }
-
- public function getGraphHeight()
- {
- return $this->graphHeight;
- }
-
- public function setChartOptions (array $chartOptions = [])
- {
- $this->configCharts['options'] = $chartOptions;
- return $this;
- }
- public function updateChartOption($key = null, $value = null)
- {
- if(is_string($key) && trim($key) !== '')
- {
- $this->configCharts['options'][$key] = $value;
- }
- return $this;
- }
- public function getChartOptions()
- {
- return $this->configChartsOptions;
- }
-
- public function setChartType($type = '')
- {
- $this->configCharts['type'] = $type;
-
- return $this;
- }
-
- public function getChartType()
- {
- return $this->configCharts['type'];
- }
-
- public function setChartTypeToLine()
- {
- return $this->setChartType('line');
- }
-
- public function setChartTypeToBar()
- {
- return $this->setChartType('bar');
- }
-
- public function setChartTypeToRader()
- {
- return $this->setChartType('radar');
- }
-
- public function setChartTypeToPolarArea()
- {
- return $this->setChartType('polarArea');
- }
-
- public function setChartTypeToPie()
- {
- return $this->setChartType('pie');
- }
-
- public function setChartTypeToDoughnut()
- {
- return $this->setChartType('doughnut');
- }
-
- public function setChartTypeToBubble()
- {
- return $this->setChartType('bubble');
- }
-
- public function setChartData($data = [])
- {
- if(is_object($data) && $data instanceof Data)
- {
- $this->configCharts['data'] = $data->toArray();
- }
- else
- {
- $this->configCharts['data'] = $data;
- }
-
- return $this;
- }
-
- public function getChartData()
- {
- return $this->configCharts['data'];
- }
-
- protected function loadSettings()
- {
- $this->configChartsSettings = json_decode(Model::where('setting', $this->graphSettingsKey)->first()->value);
-
- if ($this->configChartsSettings)
- {
- $this->setGraphFilterInfo(null, $this->configChartsSettings->start, $this->configChartsSettings->end);
- }
-
- return $this;
- }
-
- protected function useFilter()
- {
- if ($this->configCharts['data'] && $this->configCharts['data']['labels'] && $this->configCharts['filter']['default']['start'] && $this->configCharts['filter']['default']['end'])
- {
- $this->configCharts['dataParse'] = $this->configCharts['data'];
- $removeIndex = [];
- $isRemove = true;
- foreach ($this->configCharts['data']['labels'] as $index => $value)
- {
- if ($value == $this->configCharts['filter']['default']['start'])
- {
- $isRemove = false;
- continue;
- }
-
- if ($value == $this->configCharts['filter']['default']['end'])
- {
- $isRemove = true;
- continue;
- }
-
- if ($isRemove)
- {
- $removeIndex[] = $index;
- }
- }
-
- foreach ($removeIndex as $index)
- {
- if (isset($this->configCharts['data']['labels'][$index]))
- unset($this->configCharts['data']['labels'][$index]);
- if (isset($this->configCharts['data']['datasets']))
- {
- foreach ($this->configCharts['data']['datasets'] as &$dataset)
- {
- foreach($dataset as $key => $value)
- {
- if (is_array($value) && isset($dataset[$key][$index]))
- unset($dataset[$key][$index]);
- }
- }
- }
- }
- }
-
- return $this;
- }
-
- protected function addColorForLabels()
- {
- $indexColor = [];
-
- if ($this->configCharts['data'] && $this->configCharts['data']['labels'] && $this->filterInfo['displayEditColor'])
- {
- foreach ($this->configCharts['data']['labels'] as $labelName)
- {
- if (isset($this->configChartsSettings->{$labelName}))
- {
- $indexColor[] = "#" . $this->configChartsSettings->{$labelName};
- }
- }
-
- if (!$indexColor)
- {
- return $this;
- }
-
- foreach ($this->configCharts['data']['datasets'] as &$dataset)
- {
- $dataset['backgroundColor'] = $indexColor;
- $dataset['borderColor'] = $indexColor;
- $dataset['hoverBackgroundColor'] = $indexColor;
- $dataset['hoverBorderColor'] = $indexColor;
- $dataset['pointBackgroundColor'] = $indexColor;
- $dataset['pointBorderColor'] = $indexColor;
- $dataset['pointHoverBackgroundColor'] = $indexColor;
- $dataset['pointHoverBorderColor'] = $indexColor;
- }
- }
-
- return $this;
- }
-
- /**
- *
- * @return Data
- */
- public function generateDataObject()
- {
- return (new Data());
- }
- /**
- * @return DataSet
- */
- public function generateDataSetObject()
- {
- return (new DataSet());
- }
-
- public function prepareAjaxData()
- {
- //to be overwritten
- //load the data for graph drawing in this function
- }
-
- public function setLabels($labels = [])
- {
- $this->initData();
- if ($labels)
- {
- $this->graphData->setLabels($labels);
- }
-
- return $this;
- }
-
- public function initData()
- {
- if (!$this->graphData)
- {
- $this->graphData = new Data();
- }
-
- return $this;
- }
-
- public function addDataSet($dataSet)
- {
- $this->graphData->addDataSet($dataSet);
-
- return $this;
- }
-
- protected function loadData()
- {
- $this->addGraphSettings();
- $this->prepareAjaxData();
-
- $this->setChartData($this->graphData);
- }
-
- protected function addGraphSettings()
- {
- if (!$this->getRequestValue('mgformtype', false) && $this->getRequestValue('ajax', false))
- {
- return $this;
- }
- if (!$this->graphSettingsEnabled)
- {
- return $this;
- }
-
- if (!$this->graphSettingsKey)
- {
- $this->graphSettingsKey = 'graph_' . $this->id;
- }
- if ($this->configurationFields)
- {
- $field = new Hidden('setting');
- $field->setDefaultValue($this->graphSettingsKey);
- $this->addSettingField($field);
- }
-
- $btn = (new Settings\SettingButton())
- ->setIndex($this->graphSettingsKey)
- ->addNamespaceScope($this->namespace)
- ->setConfigFields($this->configurationFields);
- $this->addTitleButton($btn);
- }
-
- public function enableGraphSettings($settingsKey = null)
- {
- $this->graphSettingsEnabled = true;
- if ((string)$settingsKey !== '')
- {
- $this->graphSettingsKey = (string)$settingsKey;
- }
-
- $this->addGraphSettings();
-
- return $this;
- }
-
- public function addSettingField($field)
- {
- if (is_object($field))
- {
- $this->configurationFields[$field->getId()] = $field;
- }
-
- return $this;
- }
- public function getGraphSettingsKey()
- {
- return $this->graphSettingsKey;
- }
- }
|