EmptyGraph.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\UI\Widget\Graphs;
  3. use ModulesGarden\ProxmoxAddon\Core\UI\Builder\BaseContainer;
  4. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AjaxElementInterface;
  5. use \ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception;
  6. use \ModulesGarden\ProxmoxAddon\Core\HandlerError\ErrorCodes\ErrorCodesLib;
  7. use ModulesGarden\ProxmoxAddon\Core\Models\ModuleSettings\Model;
  8. use ModulesGarden\ProxmoxAddon\Core\UI\ResponseTemplates;
  9. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Graphs\Models\Data;
  10. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Graphs\Models\DataSet;
  11. use \ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Fields\Hidden;
  12. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  13. /**
  14. * Description of EmptyGraph
  15. *
  16. * @author inbs
  17. */
  18. class EmptyGraph extends BaseContainer implements AjaxElementInterface
  19. {
  20. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\TableRowCol;
  21. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\Icon;
  22. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\TitleButtons;
  23. const GRAPH_FILTER_TYPE_INT = 'int';
  24. const GRAPH_FILTER_TYPE_STRING = 'string';
  25. const GRAPH_FILTER_TYPE_DATE = 'date';
  26. const GRAPH_OPTIONS_MODE_POINT = 'point';
  27. const GRAPH_OPTIONS_MODE_NEAREST = 'nearest';
  28. const GRAPH_OPTIONS_MODE_INDEX = 'index';
  29. const GRAPH_OPTIONS_MODE_DATASET = 'dataset';
  30. const GRAPH_OPTIONS_MODE_X = 'x';
  31. const GRAPH_OPTIONS_MODE_Y = 'y';
  32. protected $id = 'emptyGraph';
  33. protected $name = 'emptyGraph';
  34. protected $title = 'title';
  35. protected $configCharts = [
  36. 'type' => '',
  37. 'data' => [],
  38. 'dataParse' => [],
  39. 'options' => [],
  40. 'filter' => []
  41. ];
  42. protected $tooltips = [
  43. 'mode' => self::GRAPH_OPTIONS_MODE_INDEX
  44. ];
  45. protected $responsive = true;
  46. protected $responsiveAnimationDuration = null;
  47. protected $maintainAspectRatio = null;
  48. protected $onResize = null;
  49. protected $filterInfo = [
  50. 'displayEditColor' => true,
  51. 'type' => self::GRAPH_FILTER_TYPE_INT,
  52. 'default' => [
  53. 'start' => 0,
  54. 'end' => 100
  55. ]
  56. ];
  57. protected $animation = [];
  58. protected $layout = [];
  59. protected $scales = [
  60. 'xAxes' => [
  61. [
  62. 'ticks' => [
  63. 'beginAtZero' => true
  64. ]
  65. ]
  66. ]
  67. ];
  68. protected $legend = [
  69. 'display' => true,
  70. 'position' => 'top'
  71. ];
  72. protected $titleGraph = [
  73. 'display' => false,
  74. 'text' => '',
  75. 'position' => 'top'
  76. ];
  77. protected $graphWidth = null;
  78. protected $graphHeight = 400;
  79. protected $vueComponent = true;
  80. protected $defaultVueComponentName = 'mg-graph';
  81. protected $graphData = null;
  82. protected $graphSettingsKey = null;
  83. protected $graphSettingsEnabled = false;
  84. protected $configurationFields = [];
  85. public function returnAjaxData()
  86. {
  87. $this->loadData();
  88. $this->setTitleText(sl('lang')->T($this->name));
  89. $this->reloadConfigCharts();
  90. $this->addColorForLabels();
  91. $this->useFilter();
  92. return (new ResponseTemplates\RawDataJsonResponse($this->configCharts))->setCallBackFunction($this->callBackFunction)->setRefreshTargetIds($this->refreshActionIds);
  93. }
  94. protected function disableEditColor()
  95. {
  96. $this->filterInfo['displayEditColor'] = false;
  97. return $this;
  98. }
  99. protected function enableEditColor()
  100. {
  101. $this->filterInfo['displayEditColor'] = true;
  102. return $this;
  103. }
  104. protected function setGraphFilterInfo($type = self::GRAPH_FILTER_TYPE_INT, $defaultStart = 0, $defaultStop = 100)
  105. {
  106. if ($type !== null && in_array($type, [self::GRAPH_FILTER_TYPE_INT, self::GRAPH_FILTER_TYPE_DATE, self::GRAPH_FILTER_TYPE_STRING], true))
  107. {
  108. $this->filterInfo['type'] = $type;
  109. }
  110. if ($defaultStart !== null)
  111. {
  112. $this->filterInfo['default']['start'] = $defaultStart;
  113. }
  114. if ($defaultStop !== null)
  115. {
  116. $this->filterInfo['default']['end'] = $defaultStop;
  117. }
  118. return $this;
  119. }
  120. protected function getActionId()
  121. {
  122. return $this->getRequestValue('index', false);
  123. }
  124. protected function reloadConfigCharts()
  125. {
  126. if (is_array($this->animation) && count($this->animation) > 0)
  127. {
  128. $this->configCharts['options']['animation'] = $this->animation;
  129. }
  130. elseif (is_string($this->animation) && strlen($this->animation) > 0)
  131. {
  132. $this->configCharts['options']['animation'] = json_decode($this->animation, true);
  133. }
  134. if (is_array($this->layout) && count($this->layout) > 0)
  135. {
  136. $this->configCharts['options']['layout'] = $this->layout;
  137. }
  138. if (is_array($this->legend) && count($this->legend) > 0)
  139. {
  140. $this->configCharts['options']['legend'] = $this->legend;
  141. }
  142. if (is_array($this->titleGraph) && count($this->titleGraph) > 0)
  143. {
  144. $this->configCharts['options']['title'] = $this->titleGraph;
  145. }
  146. if (is_array($this->scales) && count($this->scales) > 0)
  147. {
  148. $this->configCharts['options']['scales'] = $this->scales;
  149. }
  150. if (is_array($this->filterInfo) && count($this->filterInfo) > 0)
  151. {
  152. $this->configCharts['filter'] = $this->filterInfo;
  153. }
  154. if ($this->responsive !== null)
  155. {
  156. $this->configCharts['options']['responsive'] = $this->responsive;
  157. }
  158. if ($this->responsiveAnimationDuration !== null)
  159. {
  160. $this->configCharts['options']['responsiveAnimationDuration'] = $this->responsiveAnimationDuration;
  161. }
  162. if ($this->maintainAspectRatio !== null)
  163. {
  164. $this->configCharts['options']['maintainAspectRatio'] = $this->maintainAspectRatio;
  165. }
  166. if ($this->onResize !== null)
  167. {
  168. $this->configCharts['options']['onResize'] = $this->onResize;
  169. }
  170. return $this;
  171. }
  172. protected function setChartScales(array $scales = [])
  173. {
  174. $this->scales = $scales;
  175. return $this;
  176. }
  177. protected function addChartScale($key = null, array $scales = [])
  178. {
  179. if (trim($key) !== '' && is_string($key) && !isset($this->scales[$key]))
  180. {
  181. $this->scales[$key] = $scales;
  182. }
  183. return $this;
  184. }
  185. protected function updateChartScale($key = null, array $scales = [])
  186. {
  187. if (trim($key) !== '' && is_string($key))
  188. {
  189. $this->scales[$key] = $scales;
  190. }
  191. return $this;
  192. }
  193. protected function setTooltipsMode($tooltipMode = 'index')
  194. {
  195. 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]))
  196. {
  197. throw new Exception(ErrorCodesLib::CORE_GRA_000001, ['mode' => $tooltipMode]);
  198. }
  199. $this->tooltips['mode'] = $tooltipMode;
  200. return $this;
  201. }
  202. protected function setTitleDisplay($display = true)
  203. {
  204. $this->titleGraph['display'] = (bool) $display;
  205. return $this;
  206. }
  207. protected function setTitlePosition($position = 'top')
  208. {
  209. $this->titleGraph['position'] = (string) $position;
  210. return $this;
  211. }
  212. protected function setTitleFontSize($fontSize = 12)
  213. {
  214. $this->titleGraph['fontSize'] = (int) $fontSize;
  215. return $this;
  216. }
  217. protected function setTitleFontFamily($fontFamily = "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif")
  218. {
  219. $this->titleGraph['fontFamily'] = (string) $fontFamily;
  220. return $this;
  221. }
  222. protected function setTitleFontColor($fontColor = '#666')
  223. {
  224. $this->titleGraph['fontColor'] = (string) $fontColor;
  225. return $this;
  226. }
  227. protected function setTitleFontStyle($fontStyle = '')
  228. {
  229. $this->titleGraph['fontStyle'] = (string) $fontStyle;
  230. return $this;
  231. }
  232. protected function setTitlePadding($padding = '')
  233. {
  234. $this->titleGraph['padding'] = (string) $padding;
  235. return $this;
  236. }
  237. protected function setTitleLineHeight($lineHeight = 1.2)
  238. {
  239. $this->titleGraph['lineHeight'] = (float) $lineHeight;
  240. return $this;
  241. }
  242. protected function setTitleText($text = '')
  243. {
  244. $this->titleGraph['text'] = (string) $text;
  245. return $this;
  246. }
  247. protected function unsetLegend()
  248. {
  249. $this->legend = [];
  250. return $this;
  251. }
  252. protected function setLegendLabelsBoxWidth($boxWidth = 40)
  253. {
  254. $this->legend['labels']['boxWidth'] = (int) $boxWidth;
  255. return $this;
  256. }
  257. protected function setLegendLabelsFontSize($fontSize = 12)
  258. {
  259. $this->legend['labels']['fontSize'] = (int) $fontSize;
  260. return $this;
  261. }
  262. protected function setLegendLabelsFontStyle($fontStyle = 'normal')
  263. {
  264. $this->legend['labels']['fontStyle'] = (string) $fontStyle;
  265. return $this;
  266. }
  267. protected function setLegendLabelsFontColor($fontColor = '#666') // Demon Color 3:-]
  268. {
  269. $this->legend['labels']['fontColor'] = (string) $fontColor;
  270. return $this;
  271. }
  272. protected function setLegendLabelsFontFamily($fontFamily = "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif") // Demon Color 3:-]
  273. {
  274. $this->legend['labels']['fontFamily'] = (string) $fontFamily;
  275. return $this;
  276. }
  277. protected function setLegendLabelsPadding($padding = 10)
  278. {
  279. $this->legend['labels']['padding'] = (int) $padding;
  280. return $this;
  281. }
  282. protected function setLegendLabelsGenerateLabels($generateLabels = "function () {}")
  283. {
  284. $this->legend['labels']['generateLabels'] = (string) $generateLabels;
  285. return $this;
  286. }
  287. protected function setLegendLabelsFilter($filter = "function () {}")
  288. {
  289. $this->legend['labels']['filter'] = (string) $filter;
  290. return $this;
  291. }
  292. protected function setLegendLabelsUsePointStyle($usePointStyle = false)
  293. {
  294. $this->legend['labels']['usePointStyle'] = (bool) $usePointStyle;
  295. return $this;
  296. }
  297. protected function setLegendReverse($isReverse = false)
  298. {
  299. $this->legend['reverse'] = (bool) $isReverse;
  300. return $this;
  301. }
  302. protected function setLegendOnHover($eventFunction = "function (event, legendItem) {}")
  303. {
  304. $this->legend['onHover'] = (string) $eventFunction;
  305. return $this;
  306. }
  307. protected function setLegendOnClick($eventFunction = "function (event, legendItem) {}")
  308. {
  309. $this->legend['onClick'] = (string) $eventFunction;
  310. return $this;
  311. }
  312. protected function setLegendFullWidth($isFullWidth = true)
  313. {
  314. $this->legend['fullWidth'] = (bool) $isFullWidth;
  315. return $this;
  316. }
  317. protected function setLegendDisplay($isDisplay = true)
  318. {
  319. $this->legend['display'] = (bool) $isDisplay;
  320. return $this;
  321. }
  322. protected function setLegendPosition($position = 'top')
  323. {
  324. $this->legend['position'] = (string) $position;
  325. return $this;
  326. }
  327. protected function unsetLayout()
  328. {
  329. $this->layout = [];
  330. return $this;
  331. }
  332. protected function setLayoutPadding($left = 0, $right = 0, $top = 0, $bottom = 0)
  333. {
  334. $this->layout['padding'] = [
  335. 'left' => $left,
  336. 'right' => $right,
  337. 'top' => $top,
  338. 'bottom' => $bottom
  339. ];
  340. return $this;
  341. }
  342. protected function setAnimation($animation)
  343. {
  344. $this->animation = $animation;
  345. return $this;
  346. }
  347. protected function addAnimation($event, $animation)
  348. {
  349. if (is_array($this->animation))
  350. {
  351. $this->animation[$event] = $animation;
  352. }
  353. elseif (is_string($this->animation))
  354. {
  355. $animations = json_decode($this->animation, true);
  356. $animations[$event] = $animation;
  357. $this->animation = json_encode($animations);
  358. }
  359. return $this;
  360. }
  361. public function setGraphWidth($graphWidth = 0)
  362. {
  363. if (!is_numeric($graphWidth))
  364. {
  365. throw new Exception(ErrorCodesLib::CORE_GRA_000002, ['width' => $graphWidth], ['width' => $graphWidth]);
  366. }
  367. $this->graphWidth = $graphWidth;
  368. return $this;
  369. }
  370. public function setGraphHeight($graphHeight = 0)
  371. {
  372. if (!is_numeric($graphHeight))
  373. {
  374. throw new Exception(ErrorCodesLib::CORE_GRA_000003, ['height' => $graphHeight], ['height' => $graphHeight]);
  375. }
  376. $this->graphHeight = $graphHeight;
  377. return $this;
  378. }
  379. public function getGraphWidth()
  380. {
  381. return $this->graphWidth;
  382. }
  383. public function getGraphHeight()
  384. {
  385. return $this->graphHeight;
  386. }
  387. public function setChartOptions(array $chartOptions = [])
  388. {
  389. $this->configCharts['options'] = $chartOptions;
  390. return $this;
  391. }
  392. public function updateChartOption($key = null, $value = null)
  393. {
  394. if(is_string($key) && trim($key) !== '')
  395. {
  396. $this->configCharts['options'][$key] = $value;
  397. }
  398. return $this;
  399. }
  400. public function getChartOptions()
  401. {
  402. return $this->configChartsOptions;
  403. }
  404. public function setChartType($type = '')
  405. {
  406. $this->configCharts['type'] = $type;
  407. return $this;
  408. }
  409. public function getChartType()
  410. {
  411. return $this->configCharts['type'];
  412. }
  413. public function setChartTypeToLine()
  414. {
  415. return $this->setChartType('line');
  416. }
  417. public function setChartTypeToBar()
  418. {
  419. return $this->setChartType('bar');
  420. }
  421. public function setChartTypeToRader()
  422. {
  423. return $this->setChartType('radar');
  424. }
  425. public function setChartTypeToPolarArea()
  426. {
  427. return $this->setChartType('polarArea');
  428. }
  429. public function setChartTypeToPie()
  430. {
  431. return $this->setChartType('pie');
  432. }
  433. public function setChartTypeToDoughnut()
  434. {
  435. return $this->setChartType('doughnut');
  436. }
  437. public function setChartTypeToBubble()
  438. {
  439. return $this->setChartType('bubble');
  440. }
  441. public function setChartData($data = [])
  442. {
  443. if (is_object($data) && $data instanceof Data)
  444. {
  445. $this->configCharts['data'] = $data->toArray();
  446. }
  447. else
  448. {
  449. $this->configCharts['data'] = $data;
  450. }
  451. return $this;
  452. }
  453. public function getChartData()
  454. {
  455. return $this->configCharts['data'];
  456. }
  457. protected function loadSettings()
  458. {
  459. $this->configChartsSettings = json_decode(Model::where('setting', $this->graphSettingsKey)->first()->value);
  460. if ($this->configChartsSettings)
  461. {
  462. $this->setGraphFilterInfo(null, $this->configChartsSettings->start, $this->configChartsSettings->end);
  463. }
  464. return $this;
  465. }
  466. protected function useFilter()
  467. {
  468. if ($this->configCharts['data'] && $this->configCharts['data']['labels'] && $this->configCharts['filter']['default']['start'] && $this->configCharts['filter']['default']['end'])
  469. {
  470. $this->configCharts['dataParse'] = $this->configCharts['data'];
  471. $removeIndex = [];
  472. $isRemove = true;
  473. foreach ($this->configCharts['data']['labels'] as $index => $value)
  474. {
  475. if ($value == $this->configCharts['filter']['default']['start'])
  476. {
  477. $isRemove = false;
  478. continue;
  479. }
  480. if ($value == $this->configCharts['filter']['default']['end'])
  481. {
  482. $isRemove = true;
  483. continue;
  484. }
  485. if ($isRemove)
  486. {
  487. $removeIndex[] = $index;
  488. }
  489. }
  490. foreach ($removeIndex as $index)
  491. {
  492. if (isset($this->configCharts['data']['labels'][$index]))
  493. unset($this->configCharts['data']['labels'][$index]);
  494. if (isset($this->configCharts['data']['datasets']))
  495. {
  496. foreach ($this->configCharts['data']['datasets'] as &$dataset)
  497. {
  498. foreach ($dataset as $key => $value)
  499. {
  500. if (is_array($value) && isset($dataset[$key][$index]))
  501. unset($dataset[$key][$index]);
  502. }
  503. }
  504. }
  505. }
  506. }
  507. return $this;
  508. }
  509. protected function addColorForLabels()
  510. {
  511. $indexColor = [];
  512. if ($this->configCharts['data'] && $this->configCharts['data']['labels'] && $this->filterInfo['displayEditColor'])
  513. {
  514. foreach ($this->configCharts['data']['labels'] as $labelName)
  515. {
  516. if (isset($this->configChartsSettings->{$labelName}))
  517. {
  518. $indexColor[] = "#" . $this->configChartsSettings->{$labelName};
  519. }
  520. }
  521. if (!$indexColor)
  522. {
  523. return $this;
  524. }
  525. foreach ($this->configCharts['data']['datasets'] as &$dataset)
  526. {
  527. $dataset['backgroundColor'] = $indexColor;
  528. $dataset['borderColor'] = $indexColor;
  529. $dataset['hoverBackgroundColor'] = $indexColor;
  530. $dataset['hoverBorderColor'] = $indexColor;
  531. $dataset['pointBackgroundColor'] = $indexColor;
  532. $dataset['pointBorderColor'] = $indexColor;
  533. $dataset['pointHoverBackgroundColor'] = $indexColor;
  534. $dataset['pointHoverBorderColor'] = $indexColor;
  535. }
  536. }
  537. return $this;
  538. }
  539. /**
  540. *
  541. * @return Data
  542. */
  543. public function generateDataObject()
  544. {
  545. return (new Data());
  546. }
  547. /**
  548. * @return DataSet
  549. */
  550. public function generateDataSetObject()
  551. {
  552. return (new DataSet());
  553. }
  554. public function prepareAjaxData()
  555. {
  556. //to be overwritten
  557. //load the data for graph drawing in this function
  558. }
  559. public function setLabels($labels = [])
  560. {
  561. $this->initData();
  562. if ($labels)
  563. {
  564. $this->graphData->setLabels($labels);
  565. }
  566. return $this;
  567. }
  568. public function initData()
  569. {
  570. if (!$this->graphData)
  571. {
  572. $this->graphData = new Data();
  573. }
  574. return $this;
  575. }
  576. public function addDataSet($dataSet)
  577. {
  578. $this->graphData->addDataSet($dataSet);
  579. return $this;
  580. }
  581. protected function loadData()
  582. {
  583. $this->addGraphSettings();
  584. $this->prepareAjaxData();
  585. $this->setChartData($this->graphData);
  586. }
  587. protected function addGraphSettings()
  588. {
  589. if (!$this->getRequestValue('mgformtype', false) && $this->getRequestValue('ajax', false))
  590. {
  591. return $this;
  592. }
  593. if (!$this->graphSettingsEnabled)
  594. {
  595. return $this;
  596. }
  597. if (!$this->graphSettingsKey)
  598. {
  599. $this->graphSettingsKey = 'graph_' . $this->id;
  600. }
  601. if ($this->configurationFields)
  602. {
  603. $field = new Hidden('setting');
  604. $field->setDefaultValue($this->graphSettingsKey);
  605. $this->addSettingField($field);
  606. }
  607. $btn = (new Settings\SettingButton())
  608. ->setIndex($this->graphSettingsKey)
  609. ->addNamespaceScope($this->namespace)
  610. ->setConfigFields($this->configurationFields);
  611. $this->addTitleButton($btn);
  612. }
  613. public function enableGraphSettings($settingsKey = null)
  614. {
  615. $this->graphSettingsEnabled = true;
  616. if ((string) $settingsKey !== '')
  617. {
  618. $this->graphSettingsKey = (string) $settingsKey;
  619. }
  620. $this->addGraphSettings();
  621. return $this;
  622. }
  623. public function addSettingField($field)
  624. {
  625. if (is_object($field))
  626. {
  627. $this->configurationFields[$field->getId()] = $field;
  628. }
  629. return $this;
  630. }
  631. public function getGraphSettingsKey()
  632. {
  633. return $this->graphSettingsKey;
  634. }
  635. }