From 350cdc2d4255e24919e13e192dfb3f610271a7aa Mon Sep 17 00:00:00 2001 From: Daniel Cothran <daniel@andile.co> Date: Mon, 11 Dec 2017 15:17:32 -0500 Subject: [PATCH] Issue #2879533 by lbaran: Improve Charts code documentation --- charts.module | 16 +- .../src/Controller/ChartsApiExample.php | 2 +- .../charts_c3/src/Charts/C3ChartsRender.php | 6 +- .../src/Plugin/chart/CThreeCharts.php | 175 ++--- .../charts_c3/src/Settings/CThree/CThree.php | 37 + .../src/Settings/CThree/ChartAxis.php | 13 + .../src/Settings/CThree/ChartColor.php | 13 + .../src/Settings/CThree/ChartData.php | 31 + .../src/Settings/CThree/ChartDimensions.php | 13 + .../src/Settings/CThree/ChartLabel.php | 13 + .../src/Settings/CThree/ChartTitle.php | 13 + .../src/Settings/CThree/ChartType.php | 12 + .../src/Plugin/chart/GoogleCharts.php | 707 +++++++++--------- .../src/Charts/HighchartsChartsRender.php | 6 +- .../src/Plugin/chart/Highchart.php | 256 +++---- .../src/Settings/Highcharts/ChartCredits.php | 13 + .../src/Settings/Highcharts/ChartLabel.php | 13 + .../src/Settings/Highcharts/ChartLegend.php | 61 ++ .../src/Settings/Highcharts/ChartTitle.php | 13 + .../Settings/Highcharts/DataLabelStatus.php | 13 + .../src/Settings/Highcharts/DataLabels.php | 13 + .../src/Settings/Highcharts/Highcharts.php | 86 ++- .../src/Settings/Highcharts/Label.php | 13 + .../src/Settings/Highcharts/PlotOptions.php | 13 + .../src/Settings/Highcharts/Tooltip.php | 13 + .../src/Settings/Highcharts/Xaxis.php | 25 + .../src/Settings/Highcharts/XaxisTitle.php | 12 + .../src/Settings/Highcharts/Yaxis.php | 19 + .../src/Settings/Highcharts/YaxisLabel.php | 15 +- .../src/Settings/Highcharts/YaxisTitle.php | 12 + src/Charts/ChartsRenderInterface.php | 2 +- src/Charts/ModuleSelector.php | 14 +- src/Form/ChartsConfigForm.php | 11 +- src/Plugin/chart/AbstractChart.php | 55 +- .../display/ChartsPluginDisplayChart.php | 6 +- .../views/style/ChartsPluginStyleChart.php | 48 +- src/Services/ChartsSettingsService.php | 6 +- src/Theme/ChartsInterface.php | 4 +- src/Util/Util.php | 12 +- 39 files changed, 1145 insertions(+), 660 deletions(-) mode change 100755 => 100644 modules/charts_c3/src/Plugin/chart/CThreeCharts.php mode change 100755 => 100644 modules/charts_google/src/Plugin/chart/GoogleCharts.php mode change 100755 => 100644 modules/charts_highcharts/src/Plugin/chart/Highchart.php mode change 100755 => 100644 src/Plugin/chart/AbstractChart.php diff --git a/charts.module b/charts.module index 86374a3..a6d11c1 100644 --- a/charts.module +++ b/charts.module @@ -96,14 +96,14 @@ function template_preprocess_views_view_charts(&$variables) { $plugin_definitions = $plugin_manager->getDefinitions(); if (!isset($plugin_definitions)) { - //to be removed in drupal 9 - $moduleSelector = new ModuleSelector($library, $categories, $seriesData, $options, $attachmentDisplayOptions, $chartId); - if ($moduleSelector->moduleExists()) { - $moduleSelector->buildVariables($variables); - } + // To be removed in drupal 9. + $moduleSelector = new ModuleSelector($library, $categories, $seriesData, $options, $attachmentDisplayOptions, $chartId); + if ($moduleSelector->moduleExists()) { + $moduleSelector->buildVariables($variables); + } } - else{ - $plugin = $plugin_manager->createInstance($library); - $plugin->buildVariables($options, $categories, $seriesData, $attachmentDisplayOptions, $variables, $chartId); + else { + $plugin = $plugin_manager->createInstance($library); + $plugin->buildVariables($options, $categories, $seriesData, $attachmentDisplayOptions, $variables, $chartId); } } diff --git a/modules/charts_api_example/src/Controller/ChartsApiExample.php b/modules/charts_api_example/src/Controller/ChartsApiExample.php index 835d1a0..18e3d6b 100644 --- a/modules/charts_api_example/src/Controller/ChartsApiExample.php +++ b/modules/charts_api_example/src/Controller/ChartsApiExample.php @@ -74,7 +74,7 @@ class ChartsApiExample extends ControllerBase implements ContainerInjectionInter '#seriesData' => $seriesData, '#options' => $options, ]; - + return $build; } diff --git a/modules/charts_c3/src/Charts/C3ChartsRender.php b/modules/charts_c3/src/Charts/C3ChartsRender.php index 5c1ee8e..7bf275c 100644 --- a/modules/charts_c3/src/Charts/C3ChartsRender.php +++ b/modules/charts_c3/src/Charts/C3ChartsRender.php @@ -26,7 +26,7 @@ class C3ChartsRender implements ChartsRenderInterface { /** * {@inheritdoc} */ - public function chartsRenderCharts(array $options = [], array $categories = [], array $seriesData = [], array $attachmentDisplayOptions = [],array &$variables = [], $chartId = '') { + public function chartsRenderCharts(array $options = [], array $categories = [], array $seriesData = [], array $attachmentDisplayOptions = [], array &$variables = [], $chartId = '') { $noAttachmentDisplays = count($attachmentDisplayOptions) === 0; $types = []; @@ -84,14 +84,14 @@ class C3ChartsRender implements ChartsRenderInterface { array_push($c3Data, $categories); $chartData->setColumns($c3Data); } - else if ($options['type'] == 'column') { + elseif ($options['type'] == 'column') { $chartData->setType('bar'); $chartAxis->setRotated(FALSE); array_unshift($categories, 'x'); array_push($c3Data, $categories); $chartData->setColumns($c3Data); } - else if ($options['type'] == 'pie' || $options['type'] == 'donut') { + elseif ($options['type'] == 'pie' || $options['type'] == 'donut') { $chartData->setColumns($c3Data); } else { diff --git a/modules/charts_c3/src/Plugin/chart/CThreeCharts.php b/modules/charts_c3/src/Plugin/chart/CThreeCharts.php old mode 100755 new mode 100644 index 09e3ee0..b4bf849 --- a/modules/charts_c3/src/Plugin/chart/CThreeCharts.php +++ b/modules/charts_c3/src/Plugin/chart/CThreeCharts.php @@ -18,99 +18,100 @@ use Drupal\charts_c3\Settings\CThree\ChartAxis; * name = @Translation("C3 Charts") * ) */ -class CThreeCharts extends AbstractChart{ - public function buildVariables($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, - $chartId) { - $noAttachmentDisplays = count($attachmentDisplayOptions) === 0; - $types = []; +class CThreeCharts extends AbstractChart { - // @todo - make this work for more that one attachment. - for ($i = 1; $i <= count($attachmentDisplayOptions); $i++) { - if ($attachmentDisplayOptions[$i - 1]['style']['options']['type'] == 'column') { - $types[$seriesData[$i]['name']] = 'bar'; - } - else { - $types[$seriesData[$i]['name']] = $attachmentDisplayOptions[$i - 1]['style']['options']['type']; - } - } - $c3Data = []; - for ($i = 0; $i < count($seriesData); $i++) { - $c3DataTemp = $seriesData[$i]['data']; - array_unshift($c3DataTemp, $seriesData[$i]['name']); - array_push($c3Data, $c3DataTemp); - } + public function buildVariables($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId) { + $noAttachmentDisplays = count($attachmentDisplayOptions) === 0; + $types = []; - $c3Chart = new ChartType(); - $c3Chart->setType($options['type']); - $c3ChartTitle = new ChartTitle(); - $c3ChartTitle->setText($options['title']); - $chartAxis = new ChartAxis(); - $c3 = new CThree(); - $bindTo = '#' . $chartId; - $c3->setBindTo($bindTo); - $c3->setTitle($c3ChartTitle); - $chartData = new ChartData(); - if ($noAttachmentDisplays > 0) { - $chartData->setLabels(FALSE); - } + // @todo - make this work for more that one attachment. + for ($i = 1; $i <= count($attachmentDisplayOptions); $i++) { + if ($attachmentDisplayOptions[$i - 1]['style']['options']['type'] == 'column') { + $types[$seriesData[$i]['name']] = 'bar'; + } + else { + $types[$seriesData[$i]['name']] = $attachmentDisplayOptions[$i - 1]['style']['options']['type']; + } + } + $c3Data = []; + for ($i = 0; $i < count($seriesData); $i++) { + $c3DataTemp = $seriesData[$i]['data']; + array_unshift($c3DataTemp, $seriesData[$i]['name']); + array_push($c3Data, $c3DataTemp); + } - // Sets the primary y axis. - $yAxis = []; - $yAxis[$seriesData[0]['name']] = 'y'; - $showAxis['show'] = TRUE; - $showAxis['label'] = $options['yaxis_title']; - $chartAxis->y = $showAxis; + $c3Chart = new ChartType(); + $c3Chart->setType($options['type']); + $c3ChartTitle = new ChartTitle(); + $c3ChartTitle->setText($options['title']); + $chartAxis = new ChartAxis(); + $c3 = new CThree(); + $bindTo = '#' . $chartId; + $c3->setBindTo($bindTo); + $c3->setTitle($c3ChartTitle); + $chartData = new ChartData(); + if ($noAttachmentDisplays > 0) { + $chartData->setLabels(FALSE); + } - // Sets secondary axis from the first attachment only. - if (!$noAttachmentDisplays && $attachmentDisplayOptions[0]['inherit_yaxis'] == 0) { - $yAxis[$seriesData[1]['name']] = 'y2'; - $showSecAxis['show'] = TRUE; - $showSecAxis['label'] = $attachmentDisplayOptions[0]['style']['options']['yaxis_title']; - $chartAxis->y2 = $showSecAxis; - } + // Sets the primary y axis. + $yAxis = []; + $yAxis[$seriesData[0]['name']] = 'y'; + $showAxis['show'] = TRUE; + $showAxis['label'] = $options['yaxis_title']; + $chartAxis->y = $showAxis; - // Sets the chart type. - $chartData->setType($options['type']); - $c3->setData($chartData); - if ($options['type'] == 'bar') { - $chartAxis->setRotated(TRUE); - array_unshift($categories, 'x'); - array_push($c3Data, $categories); - $chartData->setColumns($c3Data); - } - else if ($options['type'] == 'column') { - $chartData->setType('bar'); - $chartAxis->setRotated(FALSE); - array_unshift($categories, 'x'); - array_push($c3Data, $categories); - $chartData->setColumns($c3Data); - } - else if ($options['type'] == 'pie' || $options['type'] == 'donut') { - $chartData->setColumns($c3Data); - } - else { - array_unshift($categories, 'x'); - array_push($c3Data, $categories); - $chartData->setColumns($c3Data); - } - $chartData->types = $types; + // Sets secondary axis from the first attachment only. + if (!$noAttachmentDisplays && $attachmentDisplayOptions[0]['inherit_yaxis'] == 0) { + $yAxis[$seriesData[1]['name']] = 'y2'; + $showSecAxis['show'] = TRUE; + $showSecAxis['label'] = $attachmentDisplayOptions[0]['style']['options']['yaxis_title']; + $chartAxis->y2 = $showSecAxis; + } - if ($options['type'] != 'pie' && $options['type'] != 'donut') { - $c3->setAxis($chartAxis); - } + // Sets the chart type. + $chartData->setType($options['type']); + $c3->setData($chartData); + if ($options['type'] == 'bar') { + $chartAxis->setRotated(TRUE); + array_unshift($categories, 'x'); + array_push($c3Data, $categories); + $chartData->setColumns($c3Data); + } + else if ($options['type'] == 'column') { + $chartData->setType('bar'); + $chartAxis->setRotated(FALSE); + array_unshift($categories, 'x'); + array_push($c3Data, $categories); + $chartData->setColumns($c3Data); + } + else if ($options['type'] == 'pie' || $options['type'] == 'donut') { + $chartData->setColumns($c3Data); + } + else { + array_unshift($categories, 'x'); + array_push($c3Data, $categories); + $chartData->setColumns($c3Data); + } + $chartData->types = $types; - $chartColor = new ChartColor(); - $seriesColors = []; - for ($i = 0; $i < count($seriesData); $i++) { - $seriesColor = $seriesData[$i]['color']; - array_push($seriesColors, $seriesColor); - } - $chartColor->setPattern($seriesColors); - $c3->setColor($chartColor); + if ($options['type'] != 'pie' && $options['type'] != 'donut') { + $c3->setAxis($chartAxis); + } - $variables['chart_type'] = 'c3'; - $variables['content_attributes']['data-chart'][] = json_encode($c3); - $variables['attributes']['id'][0] = $chartId; - $variables['attributes']['class'][] = 'charts-c3'; + $chartColor = new ChartColor(); + $seriesColors = []; + for ($i = 0; $i < count($seriesData); $i++) { + $seriesColor = $seriesData[$i]['color']; + array_push($seriesColors, $seriesColor); } -} \ No newline at end of file + $chartColor->setPattern($seriesColors); + $c3->setColor($chartColor); + + $variables['chart_type'] = 'c3'; + $variables['content_attributes']['data-chart'][] = json_encode($c3); + $variables['attributes']['id'][0] = $chartId; + $variables['attributes']['class'][] = 'charts-c3'; + } + +} diff --git a/modules/charts_c3/src/Settings/CThree/CThree.php b/modules/charts_c3/src/Settings/CThree/CThree.php index c98a878..80c8205 100644 --- a/modules/charts_c3/src/Settings/CThree/CThree.php +++ b/modules/charts_c3/src/Settings/CThree/CThree.php @@ -2,7 +2,11 @@ namespace Drupal\charts_c3\Settings\CThree; +/** + * CThree. + */ class CThree implements \JsonSerializable { + private $color; private $bindto; private $data; @@ -10,77 +14,110 @@ class CThree implements \JsonSerializable { private $title; /** + * Get Title. + * * @return mixed + * Title. */ public function getTitle() { return $this->title; } /** + * Set Title. + * * @param mixed $title + * Title. */ public function setTitle($title) { $this->title = $title; } /** + * Get Axis. + * * @return mixed + * Axis. */ public function getAxis() { return $this->axis; } /** + * Set Axis. + * * @param mixed $axis + * Axis. */ public function setAxis($axis) { $this->axis = $axis; } /** + * Get Data. + * * @return mixed + * Data. */ public function getData() { return $this->data; } /** + * Set Data. + * * @param mixed $data + * Data. */ public function setData($data) { $this->data = $data; } /** + * Get Bind to. + * * @return string + * Bind to. */ public function getBindTo() { return $this->bindto; } /** + * Set Bind to. + * * @param mixed $bindto + * Bind to. */ public function setBindTo($bindto) { $this->bindto = $bindto; } /** + * Get Color. + * * @return mixed + * Color. */ public function getColor() { return $this->color; } /** + * Set Color. + * * @param mixed $color + * Color. */ public function setColor($color) { $this->color = $color; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_c3/src/Settings/CThree/ChartAxis.php b/modules/charts_c3/src/Settings/CThree/ChartAxis.php index 617254e..c3bd294 100644 --- a/modules/charts_c3/src/Settings/CThree/ChartAxis.php +++ b/modules/charts_c3/src/Settings/CThree/ChartAxis.php @@ -2,26 +2,39 @@ namespace Drupal\charts_c3\Settings\CThree; +/** + * Chart Axis. + */ class ChartAxis implements \JsonSerializable { + private $rotated = FALSE; private $x = ['type' => 'category']; /** + * Get Rotated. + * * @return mixed + * Rotated. */ public function getRotated() { return $this->rotated; } /** + * Set Rotated. + * * @param mixed $rotated + * Rotated. */ public function setRotated($rotated) { $this->rotated = $rotated; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_c3/src/Settings/CThree/ChartColor.php b/modules/charts_c3/src/Settings/CThree/ChartColor.php index aeb81f6..3f435ab 100644 --- a/modules/charts_c3/src/Settings/CThree/ChartColor.php +++ b/modules/charts_c3/src/Settings/CThree/ChartColor.php @@ -2,25 +2,38 @@ namespace Drupal\charts_c3\Settings\CThree; +/** + * Chart Color. + */ class ChartColor implements \JsonSerializable { + private $pattern = []; /** + * Get Pattern. + * * @return mixed + * Pattern. */ public function getPattern() { return $this->pattern; } /** + * Set Pattern. + * * @param mixed $pattern + * Pattern. */ public function setPattern($pattern) { $this->pattern = $pattern; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_c3/src/Settings/CThree/ChartData.php b/modules/charts_c3/src/Settings/CThree/ChartData.php index 31cd15a..8c0d4b9 100644 --- a/modules/charts_c3/src/Settings/CThree/ChartData.php +++ b/modules/charts_c3/src/Settings/CThree/ChartData.php @@ -2,70 +2,101 @@ namespace Drupal\charts_c3\Settings\CThree; +/** + * Chart Data. + */ class ChartData implements \JsonSerializable { + private $columns = []; private $type; private $labels = TRUE; private $x = 'x'; /** + * Get X. + * * @return array + * X. */ public function getX() { return $this->x; } /** + * Set X. + * * @param array $x + * X. */ public function setX($x) { $this->x = $x; } /** + * Get Columns. + * * @return array + * Columns. */ public function getColumns() { return $this->columns; } /** + * Set Columns. + * * @param array $columns + * Columns. */ public function setColumns($columns) { $this->columns = $columns; } /** + * Get Type. + * * @return mixed + * Type. */ public function getType() { return $this->type; } /** + * Set Type. + * * @param mixed $type + * Type. */ public function setType($type) { $this->type = $type; } /** + * Get Labels. + * * @return mixed + * Labels. */ public function getLabels() { return $this->labels; } /** + * Set Labels. + * * @param mixed $labels + * Labels. */ public function setLabels($labels) { $this->labels = $labels; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_c3/src/Settings/CThree/ChartDimensions.php b/modules/charts_c3/src/Settings/CThree/ChartDimensions.php index 1365904..e861b23 100644 --- a/modules/charts_c3/src/Settings/CThree/ChartDimensions.php +++ b/modules/charts_c3/src/Settings/CThree/ChartDimensions.php @@ -2,25 +2,38 @@ namespace Drupal\charts_c3\Settings\CThree; +/** + * Chart Dimensions. + */ class ChartDimensions implements \JsonSerializable { + private $ratio; /** + * Get Ratio. + * * @return mixed + * Ratio. */ public function getRatio() { return $this->ratio; } /** + * Set Ratio. + * * @param mixed $ratio + * Ratio. */ public function setRatio($ratio) { $this->ratio = $ratio; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_c3/src/Settings/CThree/ChartLabel.php b/modules/charts_c3/src/Settings/CThree/ChartLabel.php index ba521d5..b494bb1 100644 --- a/modules/charts_c3/src/Settings/CThree/ChartLabel.php +++ b/modules/charts_c3/src/Settings/CThree/ChartLabel.php @@ -2,25 +2,38 @@ namespace Drupal\charts_c3\Settings\CThree; +/** + * Chart Label. + */ class ChartLabel implements \JsonSerializable { + private $rotation; /** + * Get Rotation. + * * @return mixed + * Rotation. */ public function getRotation() { return $this->rotation; } /** + * Set Rotation. + * * @param mixed $rotation + * Rotation. */ public function setRotation($rotation) { $this->rotation = $rotation; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_c3/src/Settings/CThree/ChartTitle.php b/modules/charts_c3/src/Settings/CThree/ChartTitle.php index 65f38dd..38707ea 100644 --- a/modules/charts_c3/src/Settings/CThree/ChartTitle.php +++ b/modules/charts_c3/src/Settings/CThree/ChartTitle.php @@ -2,25 +2,38 @@ namespace Drupal\charts_c3\Settings\CThree; +/** + * Chart Title. + */ class ChartTitle implements \JsonSerializable { + private $text; /** + * Get Text. + * * @return mixed + * Text. */ public function getText() { return $this->text; } /** + * Set Text. + * * @param mixed $text + * Text. */ public function setText($text) { $this->text = $text; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_c3/src/Settings/CThree/ChartType.php b/modules/charts_c3/src/Settings/CThree/ChartType.php index 747d23b..7532151 100644 --- a/modules/charts_c3/src/Settings/CThree/ChartType.php +++ b/modules/charts_c3/src/Settings/CThree/ChartType.php @@ -2,24 +2,36 @@ namespace Drupal\charts_c3\Settings\CThree; +/** + * Chart Type. + */ class ChartType implements \JsonSerializable { + private $type; /** + * Get Type. + * * @return mixed + * Type. */ public function getType() { return $this->type; } /** + * Set Type. + * * @param mixed $type + * Type. */ public function setType($type) { $this->type = $type; } /** + * Json Serialize. + * * @return array */ public function jsonSerialize() { diff --git a/modules/charts_google/src/Plugin/chart/GoogleCharts.php b/modules/charts_google/src/Plugin/chart/GoogleCharts.php old mode 100755 new mode 100644 index a55d547..d6f0d86 --- a/modules/charts_google/src/Plugin/chart/GoogleCharts.php +++ b/modules/charts_google/src/Plugin/chart/GoogleCharts.php @@ -1,4 +1,5 @@ <?php + namespace Drupal\charts_google\Plugin\chart; use Drupal\charts\Plugin\chart\AbstractChart; @@ -17,353 +18,367 @@ use Drupal\charts_google\Settings\Google\VerticalAxis; * ) */ class GoogleCharts extends AbstractChart { - /** - * Creates a JSON Object formatted for Google charts to use - * @param array $categories - * @param array $seriesData - * - * @return json|string - */ - public function buildVariables($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId) { - $categoriesCount = count($categories); - $seriesCount = count($seriesData); - - // Creates an array of the length of the series data. - $dataCount = []; - for ($x = 0; $x < $seriesCount; $x++) { - $dataCountTemp = count($seriesData[$x]['data']); - array_push($dataCount, $dataCountTemp); - } - - $dataTable = []; - for ($j = 0; $j < $categoriesCount; $j++) { - $rowDataTable = []; - for ($i = 0; $i < $seriesCount; $i++) { - $rowDataTabletemp = $seriesData[$i]['data'][$j]; - array_push($rowDataTable, $rowDataTabletemp); - } - array_unshift($rowDataTable, $categories[$j]); - array_push($dataTable, $rowDataTable); - } - - $dataTableHeader = []; - for ($r = 0; $r < $seriesCount; $r++) { - array_push($dataTableHeader, $seriesData[$r]['name']); - } - array_unshift($dataTableHeader, 'label'); - array_unshift($dataTable, $dataTableHeader); - - $googleOptions = $this->createChartsOptions($options, $seriesData, $attachmentDisplayOptions); - $googleChartType = $this->createChartType($options); - $variables['chart_type'] = 'google'; - $variables['attributes']['class'][0] = 'charts-google'; - $variables['attributes']['id'][0] = $chartId; - $variables['content_attributes']['data-chart'][] = json_encode($dataTable); - $variables['attributes']['google-options'][1] = json_encode($googleOptions); - $variables['attributes']['google-chart-type'][2] = json_encode($googleChartType); - } - - /** - * Create charts options. - * - * @param array $options - * Options. - * @param array $seriesData - * Series data. - * @param array $attachmentDisplayOptions - * Attachment Display Options. - * - * @return \Drupal\charts_google\Settings\Google\GoogleOptions - * GoogleOptions object with chart options or settings to be used by google visualization framework. - */ - private function createChartsOptions(array $options = [], array $seriesData = [], array $attachmentDisplayOptions = []) { - $noAttachmentDisplays = count($attachmentDisplayOptions) === 0; - - $chartSelected = []; - $seriesTypes = []; - - $firstVaxis = new VerticalAxis(); - - if (isset($options['yaxis_min'])) { - $firstVaxis->setMinValue($options['yaxis_min']); - } - - if (isset($options['yaxis_view_min'])) { - $firstVaxis->setViewWindowValue('min', $options['yaxis_view_min']); - } - - if (isset($options['yaxis_view_max'])) { - $firstVaxis->setViewWindowValue('max', $options['yaxis_view_max']); - } - - if (isset($options['yaxis_max'])) { - $firstVaxis->setMaxValue($options['yaxis_max']); - } - - // A format string for numeric or date axis labels. - if (isset($options['yaxis_title'])) { - $firstVaxis->setTitle($options['yaxis_title']); - } - - if (isset($options['yaxis_title_color'])) { - $firstVaxis->setTitleTextStyleValue('color', $options['yaxis_title_color']); - } - - if (isset($options['yaxis_title_font'])) { - $firstVaxis->setTitleTextStyleValue('fontName', $options['yaxis_title_font']); - } - - if (isset($options['yaxis_title_size'])) { - $firstVaxis->setTitleTextStyleValue('fontSize', $options['yaxis_title_size']); - } - - if (isset($options['yaxis_title_bold'])) { - $firstVaxis->setTitleTextStyleValue('bold', $options['yaxis_title_bold']); - } - - if (isset($options['yaxis_title_italic'])) { - $firstVaxis->setTitleTextStyleValue('italic', $options['yaxis_title_italic']); - } - - // Axis title position. - if (isset($options['yaxis_title_position'])) { - $firstVaxis->setTextPosition($options['yaxis_title_position']); - } - - if (isset($options['yaxis_baseline'])) { - $firstVaxis->setBaseline($options['yaxis_baseline']); - } - - if (isset($options['yaxis_baseline_color'])) { - $firstVaxis->setBaselineColor($options['yaxis_baseline_color']); - } - - if (isset($options['yaxis_direction'])) { - $firstVaxis->setDirection($options['yaxis_direction']); - } - - // A format string for numeric or date axis labels. - if (isset($options['yaxis_format'])) { - $firstVaxis->setFormat($options['yaxis_format']); - } - - if (isset($options['yaxis_view_window_mode'])) { - $firstVaxis->setViewWindowMode($options['yaxis_view_window_mode']); - } - - $firstHaxis = new HorizontalAxis(); - - if (isset($options['xaxis_min'])) { - $firstHaxis->setMinValue($options['xaxis_min']); - } - - if (isset($options['xaxis_view_min'])) { - $firstHaxis->setViewWindowValue('min', $options['xaxis_view_min']); - } - - if (isset($options['xaxis_view_max'])) { - $firstHaxis->setViewWindowValue('max', $options['xaxis_view_max']); - } - - if (isset($options['xaxis_max'])) { - $firstHaxis->setMaxValue($options['xaxis_max']); - } - - // A format string for numeric or date axis labels. - if (isset($options['xaxis_title'])) { - $firstHaxis->setTitle($options['xaxis_title']); - } - - if (isset($options['xaxis_title_color'])) { - $firstHaxis->setTitleTextStyleValue('color', $options['xaxis_title_color']); - } - if (isset($options['xaxis_title_font'])) { - $firstHaxis->setTitleTextStyleValue('fontName', $options['xaxis_title_font']); - } + /** + * Creates a JSON Object formatted for Google charts to use. + * + * @param mixed $options + * Options. + * @param mixed $categories + * Categories. + * @param mixed $seriesData + * Series data. + * @param mixed $attachmentDisplayOptions + * Attachment display options. + * @param mixed &$variables + * Variables. + * @param mixed $chartId + * Chart Id. + */ + public function buildVariables($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId) { + + $categoriesCount = count($categories); + $seriesCount = count($seriesData); + + // Creates an array of the length of the series data. + $dataCount = []; + for ($x = 0; $x < $seriesCount; $x++) { + $dataCountTemp = count($seriesData[$x]['data']); + array_push($dataCount, $dataCountTemp); + } + + $dataTable = []; + for ($j = 0; $j < $categoriesCount; $j++) { + $rowDataTable = []; + for ($i = 0; $i < $seriesCount; $i++) { + $rowDataTabletemp = $seriesData[$i]['data'][$j]; + array_push($rowDataTable, $rowDataTabletemp); + } + array_unshift($rowDataTable, $categories[$j]); + array_push($dataTable, $rowDataTable); + } + + $dataTableHeader = []; + for ($r = 0; $r < $seriesCount; $r++) { + array_push($dataTableHeader, $seriesData[$r]['name']); + } + array_unshift($dataTableHeader, 'label'); + array_unshift($dataTable, $dataTableHeader); + + $googleOptions = $this->createChartsOptions($options, $seriesData, $attachmentDisplayOptions); + $googleChartType = $this->createChartType($options); + $variables['chart_type'] = 'google'; + $variables['attributes']['class'][0] = 'charts-google'; + $variables['attributes']['id'][0] = $chartId; + $variables['content_attributes']['data-chart'][] = json_encode($dataTable); + $variables['attributes']['google-options'][1] = json_encode($googleOptions); + $variables['attributes']['google-chart-type'][2] = json_encode($googleChartType); + } + + /** + * Create charts options. + * + * @param array $options + * Options. + * @param array $seriesData + * Series data. + * @param array $attachmentDisplayOptions + * Attachment Display Options. + * + * @return \Drupal\charts_google\Settings\Google\GoogleOptions + * GoogleOptions object with chart options or settings to be used by google + * visualization framework. + */ + private function createChartsOptions(array $options = [], array $seriesData = [], array $attachmentDisplayOptions = []) { + $noAttachmentDisplays = count($attachmentDisplayOptions) === 0; + + $chartSelected = []; + $seriesTypes = []; + + $firstVaxis = new VerticalAxis(); + + if (isset($options['yaxis_min'])) { + $firstVaxis->setMinValue($options['yaxis_min']); + } + + if (isset($options['yaxis_view_min'])) { + $firstVaxis->setViewWindowValue('min', $options['yaxis_view_min']); + } + + if (isset($options['yaxis_view_max'])) { + $firstVaxis->setViewWindowValue('max', $options['yaxis_view_max']); + } + + if (isset($options['yaxis_max'])) { + $firstVaxis->setMaxValue($options['yaxis_max']); + } + + // A format string for numeric or date axis labels. + if (isset($options['yaxis_title'])) { + $firstVaxis->setTitle($options['yaxis_title']); + } + + if (isset($options['yaxis_title_color'])) { + $firstVaxis->setTitleTextStyleValue('color', $options['yaxis_title_color']); + } + + if (isset($options['yaxis_title_font'])) { + $firstVaxis->setTitleTextStyleValue('fontName', $options['yaxis_title_font']); + } + + if (isset($options['yaxis_title_size'])) { + $firstVaxis->setTitleTextStyleValue('fontSize', $options['yaxis_title_size']); + } + + if (isset($options['yaxis_title_bold'])) { + $firstVaxis->setTitleTextStyleValue('bold', $options['yaxis_title_bold']); + } + + if (isset($options['yaxis_title_italic'])) { + $firstVaxis->setTitleTextStyleValue('italic', $options['yaxis_title_italic']); + } + + // Axis title position. + if (isset($options['yaxis_title_position'])) { + $firstVaxis->setTextPosition($options['yaxis_title_position']); + } + + if (isset($options['yaxis_baseline'])) { + $firstVaxis->setBaseline($options['yaxis_baseline']); + } + + if (isset($options['yaxis_baseline_color'])) { + $firstVaxis->setBaselineColor($options['yaxis_baseline_color']); + } + + if (isset($options['yaxis_direction'])) { + $firstVaxis->setDirection($options['yaxis_direction']); + } + + // A format string for numeric or date axis labels. + if (isset($options['yaxis_format'])) { + $firstVaxis->setFormat($options['yaxis_format']); + } + + if (isset($options['yaxis_view_window_mode'])) { + $firstVaxis->setViewWindowMode($options['yaxis_view_window_mode']); + } + + $firstHaxis = new HorizontalAxis(); + + if (isset($options['xaxis_min'])) { + $firstHaxis->setMinValue($options['xaxis_min']); + } + + if (isset($options['xaxis_view_min'])) { + $firstHaxis->setViewWindowValue('min', $options['xaxis_view_min']); + } + + if (isset($options['xaxis_view_max'])) { + $firstHaxis->setViewWindowValue('max', $options['xaxis_view_max']); + } + + if (isset($options['xaxis_max'])) { + $firstHaxis->setMaxValue($options['xaxis_max']); + } + + // A format string for numeric or date axis labels. + if (isset($options['xaxis_title'])) { + $firstHaxis->setTitle($options['xaxis_title']); + } + + if (isset($options['xaxis_title_color'])) { + $firstHaxis->setTitleTextStyleValue('color', $options['xaxis_title_color']); + } + + if (isset($options['xaxis_title_font'])) { + $firstHaxis->setTitleTextStyleValue('fontName', $options['xaxis_title_font']); + } + + if (isset($options['xaxis_title_size'])) { + $firstHaxis->setTitleTextStyleValue('fontSize', $options['xaxis_title_size']); + } + + if (isset($options['xaxis_title_bold'])) { + $firstHaxis->setTitleTextStyleValue('bold', $options['xaxis_title_bold']); + } + + if (isset($options['xaxis_title_italic'])) { + $firstHaxis->setTitleTextStyleValue('italic', $options['xaxis_title_italic']); + } + + // Axis title position. + if (isset($options['xaxis_title_position'])) { + $firstHaxis->setTextPosition($options['xaxis_title_position']); + } + + if (isset($options['xaxis_baseline'])) { + $firstHaxis->setBaseline($options['xaxis_baseline']); + } + + if (isset($options['xaxis_baseline_color'])) { + $firstHaxis->setBaselineColor($options['xaxis_baseline_color']); + } + + if (isset($options['xaxis_direction'])) { + $firstHaxis->setDirection($options['xaxis_direction']); + } + + // A format string for numeric or date axis labels. + if (isset($options['xaxis_format'])) { + $firstHaxis->setFormat($options['xaxis_format']); + } + + if (isset($options['xaxis_view_window_mode'])) { + $firstHaxis->setViewWindowMode($options['xaxis_view_window_mode']); + } + + $vAxes = []; + $hAxes = []; + + array_push($vAxes, $firstVaxis); + array_push($hAxes, $firstHaxis); + + // Sets secondary axis from the first attachment only. + if (!$noAttachmentDisplays && $attachmentDisplayOptions[0]['inherit_yaxis'] == 0) { + $secondVaxis = new VerticalAxis(); + $secondVaxis->setTitle($attachmentDisplayOptions[0]['style']['options']['yaxis_title']); + array_push($vAxes, $secondVaxis); + } + + array_push($chartSelected, $options['type']); + + // @todo: make sure this works for more than one attachment. + for ($i = 0; $i < count($attachmentDisplayOptions); $i++) { + $attachmentChartType = $attachmentDisplayOptions[$i]['style']['options']['type']; + + if ($attachmentChartType == 'column') { + $attachmentChartType = 'bars'; + } + + if ($attachmentDisplayOptions[$i]['inherit_yaxis'] == 0 && $i == 0) { + $seriesTypes[$i + 1] = [ + 'type' => $attachmentChartType, + 'targetAxisIndex' => 1 + ]; + } + else { + $seriesTypes[$i + 1] = ['type' => $attachmentChartType]; + } + + array_push($chartSelected, $attachmentChartType); + } + + $chartSelected = array_unique($chartSelected); + $googleOptions = new GoogleOptions(); + + if (count($chartSelected) > 1) { + $parentChartType = $options['type']; + + if ($parentChartType == 'column') { + $parentChartType = 'bars'; + } + + $googleOptions->seriesType = $parentChartType; + $googleOptions->series = $seriesTypes; + } + + $googleOptions->setTitle($options['title']); + + if (isset($options['subtitle'])) { + $googleOptions->setSubTitle($options['subtitle']); + } + + $googleOptions->setVerticalAxes($vAxes); + $googleOptions->setHorizontalAxes($hAxes); + + if (in_array('donut', $chartSelected)) { + $googleOptions->pieHole = '0.5'; + } + + $chartArea = new ChartArea(); + + // Chart Area width. + if (isset($options['chart_area']['width'])) { + $chartArea->setWidth($options['chart_area']['width']); + } + + // Chart Area height. + if (isset($options['chart_area']['height'])) { + $chartArea->setHeight($options['chart_area']['height']); + } + + // Chart Area padding top. + if (isset($options['chart_area']['top'])) { + $chartArea->setPaddingTop($options['chart_area']['top']); + } - if (isset($options['xaxis_title_size'])) { - $firstHaxis->setTitleTextStyleValue('fontSize', $options['xaxis_title_size']); - } + // Chart Area padding left. + if (isset($options['chart_area']['left'])) { + $chartArea->setPaddingLeft($options['chart_area']['left']); + } + + $seriesColors = []; + for ($i = 0; $i < count($seriesData); $i++) { + $seriesColor = $seriesData[$i]['color']; + array_push($seriesColors, $seriesColor); + } + $googleOptions->setColors($seriesColors); + + // Width of the chart, in pixels. + if (isset($options['width'])) { + $googleOptions->setWidth($options['width']); + } + + // Height of the chart, in pixels. + if (isset($options['height'])) { + $googleOptions->setHeight($options['height']); + } - if (isset($options['xaxis_title_bold'])) { - $firstHaxis->setTitleTextStyleValue('bold', $options['xaxis_title_bold']); - } + // 'legend' can be a string (for position) or an array with legend + // properties: [position: 'top', textStyle: [color: 'blue', fontSize: 16]] + if (isset($options['legend'])) { + $googleOptions->setLegend($options['legend']); + } + + // Set legend position. + if (isset($options['legend_position'])) { + if (empty($options['legend_position'])) { + $options['legend_position'] = 'none'; + $googleOptions->setLegend($options['legend_position']); + } + else { + $googleOptions->setLegend($options['legend_position']); + } + } + + // Where to place the chart title, compared to the chart area. + if (isset($options['title_position'])) { + $googleOptions->setTitlePosition($options['title_position']); + } + + // Where to place the axis titles, compared to the chart area + if (isset($options['axis_titles_position'])) { + $googleOptions->setAxisTitlesPosition($options['axis_titles_position']); + } - if (isset($options['xaxis_title_italic'])) { - $firstHaxis->setTitleTextStyleValue('italic', $options['xaxis_title_italic']); - } - - // Axis title position. - if (isset($options['xaxis_title_position'])) { - $firstHaxis->setTextPosition($options['xaxis_title_position']); - } - - if (isset($options['xaxis_baseline'])) { - $firstHaxis->setBaseline($options['xaxis_baseline']); - } - - if (isset($options['xaxis_baseline_color'])) { - $firstHaxis->setBaselineColor($options['xaxis_baseline_color']); - } - - if (isset($options['xaxis_direction'])) { - $firstHaxis->setDirection($options['xaxis_direction']); - } - - // A format string for numeric or date axis labels. - if (isset($options['xaxis_format'])) { - $firstHaxis->setFormat($options['xaxis_format']); - } - - if (isset($options['xaxis_view_window_mode'])) { - $firstHaxis->setViewWindowMode($options['xaxis_view_window_mode']); - } - - $vAxes = []; - $hAxes = []; - - array_push($vAxes, $firstVaxis); - array_push($hAxes, $firstHaxis); - - // Sets secondary axis from the first attachment only. - if (!$noAttachmentDisplays && $attachmentDisplayOptions[0]['inherit_yaxis'] == 0) { - $secondVaxis = new VerticalAxis(); - $secondVaxis->setTitle($attachmentDisplayOptions[0]['style']['options']['yaxis_title']); - array_push($vAxes, $secondVaxis); - } - - array_push($chartSelected, $options['type']); - - // @todo: make sure this works for more than one attachment. - for ($i = 0; $i < count($attachmentDisplayOptions); $i++) { - $attachmentChartType = $attachmentDisplayOptions[$i]['style']['options']['type']; - - if ($attachmentChartType == 'column') { - $attachmentChartType = 'bars'; - } - - if ($attachmentDisplayOptions[$i]['inherit_yaxis'] == 0 && $i == 0) { - $seriesTypes[$i + 1] = [ - 'type' => $attachmentChartType, - 'targetAxisIndex' => 1 - ]; - } - else { - $seriesTypes[$i + 1] = ['type' => $attachmentChartType]; - } - - array_push($chartSelected, $attachmentChartType); - } - - $chartSelected = array_unique($chartSelected); - $googleOptions = new GoogleOptions(); - - if (count($chartSelected) > 1) { - $parentChartType = $options['type']; - - if ($parentChartType == 'column') { - $parentChartType = 'bars'; - } - - $googleOptions->seriesType = $parentChartType; - $googleOptions->series = $seriesTypes; - } - - $googleOptions->setTitle($options['title']); - - if (isset($options['subtitle'])) { - $googleOptions->setSubTitle($options['subtitle']); - } - - $googleOptions->setVerticalAxes($vAxes); - $googleOptions->setHorizontalAxes($hAxes); - - if (in_array('donut', $chartSelected)) { - $googleOptions->pieHole = '0.5'; - } - - $chartArea = new ChartArea(); - - // Chart Area width. - if (isset($options['chart_area']['width'])) { - $chartArea->setWidth($options['chart_area']['width']); - } - - // Chart Area height. - if (isset($options['chart_area']['height'])) { - $chartArea->setHeight($options['chart_area']['height']); - } - - // Chart Area padding top. - if (isset($options['chart_area']['top'])) { - $chartArea->setPaddingTop($options['chart_area']['top']); - } - - // Chart Area padding left. - if (isset($options['chart_area']['left'])) { - $chartArea->setPaddingLeft($options['chart_area']['left']); - } - - $seriesColors = []; - for ($i = 0; $i < count($seriesData); $i++) { - $seriesColor = $seriesData[$i]['color']; - array_push($seriesColors, $seriesColor); - } - $googleOptions->setColors($seriesColors); - - // Width of the chart, in pixels. - if (isset($options['width'])) { - $googleOptions->setWidth($options['width']); - } - - // Height of the chart, in pixels. - if (isset($options['height'])) { - $googleOptions->setHeight($options['height']); - } - - // 'legend' can be a string (for position) or an array with legend - // properties: [position: 'top', textStyle: [color: 'blue', fontSize: 16]] - if (isset($options['legend'])) { - $googleOptions->setLegend($options['legend']); - } - - // Set legend position. - if (isset($options['legend_position'])) { - if(empty($options['legend_position'])) { - $options['legend_position'] = 'none'; - $googleOptions->setLegend($options['legend_position']); - } else { - $googleOptions->setLegend($options['legend_position']); - } - } - - // Where to place the chart title, compared to the chart area. - if (isset($options['title_position'])) { - $googleOptions->setTitlePosition($options['title_position']); - } - - // Where to place the axis titles, compared to the chart area - if (isset($options['axis_titles_position'])) { - $googleOptions->setAxisTitlesPosition($options['axis_titles_position']); - } - - return $googleOptions; - } - - /** - * Create Chart Type. - * - * @param array $options - * Options. - * - * @return \Drupal\charts_google\Settings\Google\ChartType - * ChartType. - */ - private function createChartType(array $options = []) { - $googleChartType = new ChartType(); - $googleChartType->setChartType($options['type']); - - return $googleChartType; - } -} \ No newline at end of file + return $googleOptions; + } + + /** + * Create Chart Type. + * + * @param array $options + * Options. + * + * @return \Drupal\charts_google\Settings\Google\ChartType + * ChartType. + */ + private function createChartType(array $options = []) { + $googleChartType = new ChartType(); + $googleChartType->setChartType($options['type']); + + return $googleChartType; + } + +} diff --git a/modules/charts_highcharts/src/Charts/HighchartsChartsRender.php b/modules/charts_highcharts/src/Charts/HighchartsChartsRender.php index 7b522aa..d28285a 100644 --- a/modules/charts_highcharts/src/Charts/HighchartsChartsRender.php +++ b/modules/charts_highcharts/src/Charts/HighchartsChartsRender.php @@ -41,11 +41,11 @@ class HighchartsChartsRender implements ChartsRenderInterface { $chart = new Chart(); $typeOptions = $options['type']; // @todo: make this so that it happens if any display uses donut. - if ($typeOptions == 'donut'){ + if ($typeOptions == 'donut') { $typeOptions = 'pie'; // Remove donut from seriesData. foreach ($seriesData as $key => &$value) { - $value = str_replace('donut','pie',$value); + $value = str_replace('donut', 'pie', $value); } // Add innerSize to differentiate between donut and pie. foreach ($seriesData as $key => &$value) { @@ -137,7 +137,7 @@ class HighchartsChartsRender implements ChartsRenderInterface { $highchart->setChart($chart); $highchart->setTitle($chartTitle); $highchart->setXAxis($chartXaxis); -// $highchart->yAxis = $yAxes; + /* $highchart->yAxis = $yAxes; */ $highchart->setTooltip($chartTooltip); $highchart->setPlotOptions($plotOptions); $highchart->setCredits($chartCredits); diff --git a/modules/charts_highcharts/src/Plugin/chart/Highchart.php b/modules/charts_highcharts/src/Plugin/chart/Highchart.php old mode 100755 new mode 100644 index 0cc1340..9922807 --- a/modules/charts_highcharts/src/Plugin/chart/Highchart.php +++ b/modules/charts_highcharts/src/Plugin/chart/Highchart.php @@ -1,10 +1,11 @@ <?php + namespace Drupal\charts_highcharts\Plugin\chart; use Drupal\charts\Plugin\chart\AbstractChart; -//use Drupal\Charts\Annotation\Chart; +/* use Drupal\Charts\Annotation\Chart; */ use Drupal\charts_highcharts\Settings\Highcharts\Chart; -//use Drupal\charts_highcharts\Settings\Highcharts\ChartType; +/* use Drupal\charts_highcharts\Settings\Highcharts\ChartType; */ use Drupal\charts_highcharts\Settings\Highcharts\ChartTitle; use Drupal\charts_highcharts\Settings\Highcharts\Xaxis; use Drupal\charts_highcharts\Settings\Highcharts\XaxisTitle; @@ -19,6 +20,7 @@ use Drupal\charts_highcharts\Settings\Highcharts\Tooltip; use Drupal\charts_highcharts\Settings\Highcharts\ChartCredits; use Drupal\charts_highcharts\Settings\Highcharts\ChartLegend; use Drupal\charts_highcharts\Settings\Highcharts\Highcharts; + /** * Defines a concrete class for a Highcharts. * @@ -27,128 +29,130 @@ use Drupal\charts_highcharts\Settings\Highcharts\Highcharts; * name = @Translation("Highcharts") * ) */ -class Highchart extends AbstractChart{ - /** - * Creates a JSON Object formatted for Highcharts to use - * - * @param $options - * @param array $categories - * @param array $seriesData - * - * @param array $attachmentDisplayOptions - * - * @return Highcharts object to be used by highcharts javascripts visualization framework - */ - public function buildVariables($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId) { - $noAttachmentDisplays = count($attachmentDisplayOptions) === 0; - - $chart = new Chart(); - $typeOptions = $options['type']; - // @todo: make this so that it happens if any display uses donut. - if ($typeOptions == 'donut'){ - $typeOptions = 'pie'; - // Remove donut from seriesData. - foreach ($seriesData as $key => &$value) { - $value = str_replace('donut','pie',$value); - } - // Add innerSize to differentiate between donut and pie. - foreach ($seriesData as $key => &$value) { - $innerSize['showInLegend'] = 'true'; - $innerSize['innerSize'] = '40%'; - $chartPlacement = array_search($value, $seriesData); - $seriesData[$chartPlacement] = array_merge($innerSize, $seriesData[$chartPlacement]); - } - } - $chart->setType($typeOptions); - - // Set chart width. - if (isset($options['width'])) { - $chart->setWidth($options['width']); - } - - // Set chart height. - if (isset($options['height'])) { - $chart->setHeight($options['height']); - } - - // Set chart title. - $chartTitle = new ChartTitle(); - if (isset($options['title'])) { - $chartTitle->setText($options['title']); - } - - $chartXaxis = new Xaxis(); - $chartLabels = new ChartLabel(); - - // Set x-axis label rotation. - if (isset($options['xaxis_labels_rotation'])) { - $chartLabels->setRotation($options['xaxis_labels_rotation']); - } - - $chartXaxis->setCategories($categories); - - // Set x-axis title. - $xAxisTitle = new XaxisTitle(); - if (isset($options['xaxis_title'])) { - $xAxisTitle->setText($options['xaxis_title']); - } - $chartXaxis->setTitle($xAxisTitle); - $chartXaxis->setLabels($chartLabels); - $yaxisLabels = new YaxisLabel(); - $chartYaxis = new Yaxis(); - $yAxes = []; - $yAxisTitle = new YaxisTitle(); - $yAxisTitle->setText($options['yaxis_title']); - if (!empty($options['yaxis_min'])) { - $chartYaxis->min = $options['yaxis_min']; - } - if (!empty($options['yaxis_max'])) { - $chartYaxis->max = $options['yaxis_max']; - } - - $chartYaxis->setLabels($yaxisLabels); - $chartYaxis->setTitle($yAxisTitle); - array_push($yAxes, $chartYaxis); - - // Chart libraries tend to support only one secondary axis. - if (!$noAttachmentDisplays && $attachmentDisplayOptions[0]['inherit_yaxis'] == 0) { - $chartYaxisSecondary = new Yaxis(); - $yAxisTitleSecondary = new YaxisTitle(); - $yAxisTitleSecondary->setText($attachmentDisplayOptions[0]['style']['options']['yaxis_title']); - $chartYaxisSecondary->setTitle($yAxisTitleSecondary); - $chartYaxisSecondary->setLabels($yaxisLabels); - $chartYaxisSecondary->opposite = 'true'; - if (!empty($attachmentDisplayOptions[0]['style']['options']['yaxis_min'])) { - $chartYaxisSecondary->min = $attachmentDisplayOptions[0]['style']['options']['yaxis_min']; - } - if (!empty($attachmentDisplayOptions[0]['style']['options']['yaxis_max'])) { - $chartYaxisSecondary->max = $attachmentDisplayOptions[0]['style']['options']['yaxis_max']; - } - array_push($yAxes, $chartYaxisSecondary); - } - $dataLabelStatus = new DataLabelStatus(); - $dataLabels = new DataLabels(); - $dataLabels->setDataLabels($dataLabelStatus); - $plotOptions = new PlotOptions(); - $plotOptions->setPlotType($dataLabels); - $chartTooltip = new Tooltip(); - $chartCredits = new ChartCredits(); - $chartLegend = new ChartLegend(); - - $highchart = new Highcharts(); - $highchart->setChart($chart); - $highchart->setTitle($chartTitle); - $highchart->setXAxis($chartXaxis); -// $highchart->yAxis = $yAxes; - $highchart->setTooltip($chartTooltip); - $highchart->setPlotOptions($plotOptions); - $highchart->setCredits($chartCredits); - $highchart->setLegend($chartLegend); - $highchart->setSeries($seriesData); - - $variables['chart_type'] = 'highcharts'; - $variables['content_attributes']['data-chart'][] = json_encode($highchart); - $variables['attributes']['id'][0] = $chartId; - $variables['attributes']['class'][] = 'charts-highchart'; +class Highchart extends AbstractChart { + + /** + * Creates a JSON Object formatted for Highcharts to use + * + * @param $options + * @param array $categories + * @param array $seriesData + * + * @param array $attachmentDisplayOptions + * + * @return Highcharts object to be used by highcharts javascripts visualization framework + */ + public function buildVariables($options, $categories = [], $seriesData = [], $attachmentDisplayOptions = [], &$variables, $chartId) { + $noAttachmentDisplays = count($attachmentDisplayOptions) === 0; + + $chart = new Chart(); + $typeOptions = $options['type']; + // @todo: make this so that it happens if any display uses donut. + if ($typeOptions == 'donut') { + $typeOptions = 'pie'; + // Remove donut from seriesData. + foreach ($seriesData as $key => &$value) { + $value = str_replace('donut', 'pie', $value); + } + // Add innerSize to differentiate between donut and pie. + foreach ($seriesData as $key => &$value) { + $innerSize['showInLegend'] = 'true'; + $innerSize['innerSize'] = '40%'; + $chartPlacement = array_search($value, $seriesData); + $seriesData[$chartPlacement] = array_merge($innerSize, $seriesData[$chartPlacement]); + } + } + $chart->setType($typeOptions); + + // Set chart width. + if (isset($options['width'])) { + $chart->setWidth($options['width']); + } + + // Set chart height. + if (isset($options['height'])) { + $chart->setHeight($options['height']); + } + + // Set chart title. + $chartTitle = new ChartTitle(); + if (isset($options['title'])) { + $chartTitle->setText($options['title']); + } + + $chartXaxis = new Xaxis(); + $chartLabels = new ChartLabel(); + + // Set x-axis label rotation. + if (isset($options['xaxis_labels_rotation'])) { + $chartLabels->setRotation($options['xaxis_labels_rotation']); + } + + $chartXaxis->setCategories($categories); + + // Set x-axis title. + $xAxisTitle = new XaxisTitle(); + if (isset($options['xaxis_title'])) { + $xAxisTitle->setText($options['xaxis_title']); + } + $chartXaxis->setTitle($xAxisTitle); + $chartXaxis->setLabels($chartLabels); + $yaxisLabels = new YaxisLabel(); + $chartYaxis = new Yaxis(); + $yAxes = []; + $yAxisTitle = new YaxisTitle(); + $yAxisTitle->setText($options['yaxis_title']); + if (!empty($options['yaxis_min'])) { + $chartYaxis->min = $options['yaxis_min']; + } + if (!empty($options['yaxis_max'])) { + $chartYaxis->max = $options['yaxis_max']; } -} \ No newline at end of file + + $chartYaxis->setLabels($yaxisLabels); + $chartYaxis->setTitle($yAxisTitle); + array_push($yAxes, $chartYaxis); + + // Chart libraries tend to support only one secondary axis. + if (!$noAttachmentDisplays && $attachmentDisplayOptions[0]['inherit_yaxis'] == 0) { + $chartYaxisSecondary = new Yaxis(); + $yAxisTitleSecondary = new YaxisTitle(); + $yAxisTitleSecondary->setText($attachmentDisplayOptions[0]['style']['options']['yaxis_title']); + $chartYaxisSecondary->setTitle($yAxisTitleSecondary); + $chartYaxisSecondary->setLabels($yaxisLabels); + $chartYaxisSecondary->opposite = 'true'; + if (!empty($attachmentDisplayOptions[0]['style']['options']['yaxis_min'])) { + $chartYaxisSecondary->min = $attachmentDisplayOptions[0]['style']['options']['yaxis_min']; + } + if (!empty($attachmentDisplayOptions[0]['style']['options']['yaxis_max'])) { + $chartYaxisSecondary->max = $attachmentDisplayOptions[0]['style']['options']['yaxis_max']; + } + array_push($yAxes, $chartYaxisSecondary); + } + $dataLabelStatus = new DataLabelStatus(); + $dataLabels = new DataLabels(); + $dataLabels->setDataLabels($dataLabelStatus); + $plotOptions = new PlotOptions(); + $plotOptions->setPlotType($dataLabels); + $chartTooltip = new Tooltip(); + $chartCredits = new ChartCredits(); + $chartLegend = new ChartLegend(); + + $highchart = new Highcharts(); + $highchart->setChart($chart); + $highchart->setTitle($chartTitle); + $highchart->setXAxis($chartXaxis); + /* $highchart->yAxis = $yAxes; */ + $highchart->setTooltip($chartTooltip); + $highchart->setPlotOptions($plotOptions); + $highchart->setCredits($chartCredits); + $highchart->setLegend($chartLegend); + $highchart->setSeries($seriesData); + + $variables['chart_type'] = 'highcharts'; + $variables['content_attributes']['data-chart'][] = json_encode($highchart); + $variables['attributes']['id'][0] = $chartId; + $variables['attributes']['class'][] = 'charts-highchart'; + } + +} diff --git a/modules/charts_highcharts/src/Settings/Highcharts/ChartCredits.php b/modules/charts_highcharts/src/Settings/Highcharts/ChartCredits.php index a75386c..bf179d8 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/ChartCredits.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/ChartCredits.php @@ -2,25 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Chart Credits. + */ class ChartCredits implements \JsonSerializable { + private $enabled = FALSE; /** + * Is Enabled. + * * @return boolean + * Enabled. */ public function isEnabled() { return $this->enabled; } /** + * Set Enabled. + * * @param boolean $enabled + * Enabled. */ public function setEnabled($enabled) { $this->enabled = $enabled; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/ChartLabel.php b/modules/charts_highcharts/src/Settings/Highcharts/ChartLabel.php index 7b65ae9..3d4995a 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/ChartLabel.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/ChartLabel.php @@ -2,25 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Chart Label. + */ class ChartLabel implements \JsonSerializable { + private $rotation; /** + * Get Rotation. + * * @return mixed + * Rotation. */ public function getRotation() { return $this->rotation; } /** + * Set Rotation. + * * @param mixed $rotation + * Rotation. */ public function setRotation($rotation) { $this->rotation = (int)$rotation; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/ChartLegend.php b/modules/charts_highcharts/src/Settings/Highcharts/ChartLegend.php index 3c90933..2bf6f5b 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/ChartLegend.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/ChartLegend.php @@ -2,7 +2,11 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Chart Legend. + */ class ChartLegend implements \JsonSerializable { + private $layout = 'vertical'; private $align = 'right'; private $verticalAlign = 'top'; @@ -14,133 +18,190 @@ class ChartLegend implements \JsonSerializable { private $shadow = TRUE; /** + * Get Layout. + * * @return string + * Layout. */ public function getLayout() { return $this->layout; } /** + * Set Layout. + * * @param string $layout + * Layout. */ public function setLayout($layout) { $this->layout = $layout; } /** + * Get Align. + * * @return string + * Align. */ public function getAlign() { return $this->align; } /** + * Set Align. + * * @param string $align + * Align. */ public function setAlign($align) { $this->align = $align; } /** + * Get Vertical Align. + * * @return string + * Vertical Align. */ public function getVerticalAlign() { return $this->verticalAlign; } /** + * Set Vertical Align. + * * @param string $verticalAlign + * Vertical Align. */ public function setVerticalAlign($verticalAlign) { $this->verticalAlign = $verticalAlign; } /** + * Get X. + * * @return int + * X. */ public function getX() { return $this->x; } /** + * Set X. + * * @param int $x + * X. */ public function setX($x) { $this->x = $x; } /** + * Get Y. + * * @return int + * Y. */ public function getY() { return $this->y; } /** + * Set Y. + * * @param int $y + * Y. */ public function setY($y) { $this->y = $y; } /** + * Is Floating. + * * @return boolean + * Floating. */ public function isFloating() { return $this->floating; } /** + * Set Floating. + * * @param boolean $floating + * Floating. */ public function setFloating($floating) { $this->floating = $floating; } /** + * Get Border Width. + * * @return int + * Border Width. */ public function getBorderWidth() { return $this->borderWidth; } /** + * Set Border Width. + * * @param int $borderWidth + * Border Width. */ public function setBorderWidth($borderWidth) { $this->borderWidth = $borderWidth; } /** + * Get Background Color. + * * @return string + * Background Color. */ public function getBackgroundColor() { return $this->backgroundColor; } /** + * Set Background Color. + * * @param string $backgroundColor + * Background Color. */ public function setBackgroundColor($backgroundColor) { $this->backgroundColor = $backgroundColor; } /** + * Is Shadow. + * * @return boolean + * Shadow. */ public function isShadow() { return $this->shadow; } /** + * Set Shadow. + * * @param boolean $shadow + * Shadow. */ public function setShadow($shadow) { $this->shadow = $shadow; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/ChartTitle.php b/modules/charts_highcharts/src/Settings/Highcharts/ChartTitle.php index 726c25c..fe7ab6f 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/ChartTitle.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/ChartTitle.php @@ -2,25 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Chart Title. + */ class ChartTitle implements \JsonSerializable { + private $text; /** + * Get Text. + * * @return mixed + * Text. */ public function getText() { return $this->text; } /** + * Set Text. + * * @param mixed $text + * Text. */ public function setText($text) { $this->text = $text; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/DataLabelStatus.php b/modules/charts_highcharts/src/Settings/Highcharts/DataLabelStatus.php index 3426d01..305c97a 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/DataLabelStatus.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/DataLabelStatus.php @@ -2,25 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Data Label Status. + */ class DataLabelStatus implements \JsonSerializable { + private $enabled = TRUE; /** + * Is Enabled. + * * @return boolean + * Enabled. */ public function isEnabled() { return $this->enabled; } /** + * Set Enabled. + * * @param boolean $enabled + * Enabled. */ public function setEnabled($enabled) { $this->enabled = $enabled; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/DataLabels.php b/modules/charts_highcharts/src/Settings/Highcharts/DataLabels.php index 080ef48..76810b2 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/DataLabels.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/DataLabels.php @@ -2,25 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Data Labels. + */ class DataLabels implements \JsonSerializable { + private $dataLabels; /** + * Get Data Labels. + * * @return mixed + * Data Labels. */ public function getDataLabels() { return $this->dataLabels; } /** + * Set Data Labels. + * * @param mixed $dataLabels + * Data Labels. */ public function setDataLabels($dataLabels) { $this->dataLabels = $dataLabels; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/Highcharts.php b/modules/charts_highcharts/src/Settings/Highcharts/Highcharts.php index 0234044..7d990dc 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/Highcharts.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/Highcharts.php @@ -2,11 +2,15 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Highcharts. + */ class Highcharts implements \JsonSerializable { + private $chart; private $title; private $xAxis; -// private $yAxis; + /* private $yAxis; */ private $tooltip; private $plotOptions; private $legend; @@ -15,147 +19,201 @@ class Highcharts implements \JsonSerializable { private $series; /** + * Get Chart. + * * @return mixed + * Chart. */ public function getChart() { return $this->chart; } /** + * Set Chart. + * * @param mixed $chart + * Chart. */ public function setChart($chart) { $this->chart = $chart; } /** + * Get Title. + * * @return mixed + * Title. */ public function getTitle() { return $this->title; } /** + * Set Title. + * * @param mixed $title + * Title. */ public function setTitle($title) { $this->title = $title; } /** + * Get X Axis. + * * @return mixed + * X Axis. */ public function getXAxis() { return $this->xAxis; } /** + * Set X Axis. + * * @param mixed $xAxis + * X Axis. */ public function setXAxis($xAxis) { $this->xAxis = $xAxis; } -// /** -// * @return mixed -// */ -// public function getYAxis() { -// return $this->yAxis; -// } -// -// /** -// * @param mixed $yAxis -// */ -// public function setYAxis($yAxis) { -// $this->yAxis = $yAxis; -// } + /* + @return mixed + public function getYAxis() { + return $this->yAxis; + } + @param mixed $yAxis + public function setYAxis($yAxis) { + $this->yAxis = $yAxis; + } + */ /** + * Get Tooltip. + * * @return mixed + * Tooltip. */ public function getTooltip() { return $this->tooltip; } /** + * Set Tooltip. + * * @param mixed $tooltip + * Tooltip. */ public function setTooltip($tooltip) { $this->tooltip = $tooltip; } /** + * Get Plot Options. + * * @return mixed + * Plot Options. */ public function getPlotOptions() { return $this->plotOptions; } /** + * Set Plot Options. + * * @param mixed $plotOptions + * Plot Options. */ public function setPlotOptions($plotOptions) { $this->plotOptions = $plotOptions; } /** + * Get Legend. + * * @return mixed + * Legend. */ public function getLegend() { return $this->legend; } /** + * Set Legend. + * * @param mixed $legend + * Legend. */ public function setLegend($legend) { $this->legend = $legend; } /** + * Get Credits. + * * @return mixed + * Credits. */ public function getCredits() { return $this->credits; } /** + * Set Credits. + * * @param mixed $credits + * Credits. */ public function setCredits($credits) { $this->credits = $credits; } /** + * Get Inner Size. + * * @return mixed + * Inner Size. */ public function getInnerSize() { return $this->innerSize; } /** + * Set Inner Size. + * * @param mixed $innerSize + * Inner Size. */ public function setInnerSize($innerSize) { $this->innerSize = $innerSize; } /** + * Get Series. + * * @return mixed + * Series. */ public function getSeries() { return $this->series; } /** + * Set Series. + * * @param mixed $series + * Series. */ public function setSeries($series) { $this->series = $series; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/Label.php b/modules/charts_highcharts/src/Settings/Highcharts/Label.php index dc94072..d9f2849 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/Label.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/Label.php @@ -2,25 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Label. + */ class Label implements \JsonSerializable { + private $rotation; /** + * Get Rotation. + * * @return mixed + * Rotation. */ public function getRotation() { return $this->rotation; } /** + * Set Rotation. + * * @param mixed $rotation + * Rotation. */ public function setRotation($rotation) { $this->rotation = $rotation; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/PlotOptions.php b/modules/charts_highcharts/src/Settings/Highcharts/PlotOptions.php index 58d46df..344f9f2 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/PlotOptions.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/PlotOptions.php @@ -2,25 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Plot Options. + */ class PlotOptions implements \JsonSerializable { + private $plotType; /** + * Get Plot Type. + * * @return mixed + * Plot Type. */ public function getPlotType() { return $this->plotType; } /** + * Set Plot Type. + * * @param mixed $plotType + * Plot Type. */ public function setPlotType($plotType) { $this->plotType = $plotType; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/Tooltip.php b/modules/charts_highcharts/src/Settings/Highcharts/Tooltip.php index e96c678..4f92ef6 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/Tooltip.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/Tooltip.php @@ -2,25 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Tooltip. + */ class Tooltip implements \JsonSerializable { + private $valueSuffix = ''; /** + * Get Value Suffix. + * * @return string + * Value Suffix. */ public function getValueSuffix() { return $this->valueSuffix; } /** + * Set Value Suffix. + * * @param string $valueSuffix + * Value Suffix. */ public function setValueSuffix($valueSuffix) { $this->valueSuffix = $valueSuffix; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/Xaxis.php b/modules/charts_highcharts/src/Settings/Highcharts/Xaxis.php index 65ddc15..d30a012 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/Xaxis.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/Xaxis.php @@ -2,55 +2,80 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * X Axis. + */ class Xaxis implements \JsonSerializable { + private $categories = []; private $title; private $labels; /** + * Get Categories. + * * @return array + * Categories. */ public function getCategories() { return $this->categories; } /** + * Set Categories. + * * @param array $categories + * Categories. */ public function setCategories($categories) { $this->categories = $categories; } /** + * Get Title. + * * @return mixed + * Title. */ public function getTitle() { return $this->title; } /** + * Set Title. + * * @param mixed $title + * Title. */ public function setTitle($title) { $this->title = $title; } /** + * Get Labels. + * * @return mixed + * Labels. */ public function getLabels() { return $this->labels; } /** + * Set Labels. + * * @param mixed $labels + * Labels. */ public function setLabels($labels) { $this->labels = $labels; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/XaxisTitle.php b/modules/charts_highcharts/src/Settings/Highcharts/XaxisTitle.php index ae0ec66..264bf6b 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/XaxisTitle.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/XaxisTitle.php @@ -2,26 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * X Axis Title. + */ class XaxisTitle extends ChartTitle implements \JsonSerializable { private $text; /** + * Get Text. + * * @return string + * Text. */ public function getText() { return $this->text; } /** + * Set Text. + * * @param string $text + * Text. */ public function setText($text) { $this->text = $text; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/Yaxis.php b/modules/charts_highcharts/src/Settings/Highcharts/Yaxis.php index e282b5c..90e2fa6 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/Yaxis.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/Yaxis.php @@ -2,40 +2,59 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Y Axis. + */ class Yaxis implements \JsonSerializable { + private $title; private $labels = ''; /** + * Get Title. + * * @return mixed + * Title. */ public function getTitle() { return $this->title; } /** + * Set Title. + * * @param mixed $title + * Title. */ public function setTitle($title) { $this->title = $title; } /** + * Get Labels. + * * @return string + * Labels. */ public function getLabels() { return $this->labels; } /** + * Set Labels. + * * @param string $labels + * Labels. */ public function setLabels($labels) { $this->labels = $labels; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/YaxisLabel.php b/modules/charts_highcharts/src/Settings/Highcharts/YaxisLabel.php index b613b8e..0e6eac8 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/YaxisLabel.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/YaxisLabel.php @@ -2,25 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Y Axis Label. + */ class YaxisLabel implements \JsonSerializable { + private $overflow = 'justify'; /** - * @param $overflow + * Set Overflow. + * + * @param mixed $overflow + * Overflow. */ public function setOverflow($overflow) { $this->overflow = $overflow; } /** + * Get Overflow. + * * @return string + * Overflow. */ public function getOverflow() { return $this->overflow; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/YaxisTitle.php b/modules/charts_highcharts/src/Settings/Highcharts/YaxisTitle.php index 964b062..924382a 100644 --- a/modules/charts_highcharts/src/Settings/Highcharts/YaxisTitle.php +++ b/modules/charts_highcharts/src/Settings/Highcharts/YaxisTitle.php @@ -2,26 +2,38 @@ namespace Drupal\charts_highcharts\Settings\Highcharts; +/** + * Y Axis Title. + */ class YaxisTitle extends ChartTitle implements \JsonSerializable { private $text; /** + * Get Text. + * * @return string + * Text. */ public function getText() { return $this->text; } /** + * Set Text. + * * @param string $text + * Text. */ public function setText($text) { $this->text = $text; } /** + * Json Serialize. + * * @return array + * Json Serialize. */ public function jsonSerialize() { $vars = get_object_vars($this); diff --git a/src/Charts/ChartsRenderInterface.php b/src/Charts/ChartsRenderInterface.php index 8f4e82a..4c823e5 100644 --- a/src/Charts/ChartsRenderInterface.php +++ b/src/Charts/ChartsRenderInterface.php @@ -23,6 +23,6 @@ interface ChartsRenderInterface { * @param string $chartId * Chart Id. */ - public function chartsRenderCharts(array $options = [], array $categories = [], array $seriesData = [], array $attachmentDisplayOptions = [],array &$variables = [], $chartId = ''); + public function chartsRenderCharts(array $options = [], array $categories = [], array $seriesData = [], array $attachmentDisplayOptions = [], array &$variables = [], $chartId = ''); } diff --git a/src/Charts/ModuleSelector.php b/src/Charts/ModuleSelector.php index b25df8f..a677e7c 100644 --- a/src/Charts/ModuleSelector.php +++ b/src/Charts/ModuleSelector.php @@ -47,18 +47,18 @@ class ModuleSelector { * @return bool * Module exist. */ - public function moduleExists(){ - return \Drupal::moduleHandler()->moduleExists('charts_'.$this->library); + public function moduleExists() { + return \Drupal::moduleHandler()->moduleExists('charts_' . $this->library); } - + /** * Get class charts render. - * + * * @return string * Class charts render name. */ - private function getClassChartsRender(){ - return 'Drupal\charts_' . $this->library. '\Charts\\' . ucfirst($this->library) . 'ChartsRender'; + private function getClassChartsRender() { + return 'Drupal\charts_' . $this->library . '\Charts\\' . ucfirst($this->library) . 'ChartsRender'; } /** @@ -67,7 +67,7 @@ class ModuleSelector { * @param array $variables * Variables. */ - public function buildVariables(array &$variables = []){ + public function buildVariables(array &$variables = []) { $moduleChartsRenderer = $this->getClassChartsRender(); if (class_exists($moduleChartsRenderer)) { $chartingModule = new $moduleChartsRenderer(); diff --git a/src/Form/ChartsConfigForm.php b/src/Form/ChartsConfigForm.php index 7b1fcfc..5609507 100644 --- a/src/Form/ChartsConfigForm.php +++ b/src/Form/ChartsConfigForm.php @@ -166,7 +166,7 @@ class ChartsConfigForm extends ConfigFormBase { * @param array $field_options * Field options. * @param array $parents - * parents. + * Parents. * * @return array * Form. @@ -216,7 +216,7 @@ class ChartsConfigForm extends ConfigFormBase { 'class' => [ 'chart-type-radios', 'container-inline', - ] + ], ], '#parents' => array_merge($parents, ['type']), ]; @@ -233,7 +233,7 @@ class ChartsConfigForm extends ConfigFormBase { if ($field_options) { $first_field = key($field_options); - // $form['fields']['#theme'] = 'charts_settings_fields'; + /* $form['fields']['#theme'] = 'charts_settings_fields'; */ $form['fields']['label_field'] = [ '#type' => 'radios', '#title' => $this->t('Label field'), @@ -401,7 +401,8 @@ class ChartsConfigForm extends ConfigFormBase { 45 => $this->t('45°'), 60 => $this->t('60°'), 90 => $this->t('90°'), - ], // This is only shown on non-inverted charts. + ], + // This is only shown on non-inverted charts. '#attributes' => ['class' => ['axis-inverted-hide']], '#default_value' => $options['xaxis_labels_rotation'], '#parents' => array_merge($parents, ['xaxis_labels_rotation']), @@ -497,7 +498,7 @@ class ChartsConfigForm extends ConfigFormBase { 90 => $this->t('90°'), ], '#attributes' => [ - 'class' => ['axis-inverted-show'] + 'class' => ['axis-inverted-show'], ], '#default_value' => $options['yaxis_labels_rotation'], '#parents' => array_merge($parents, ['yaxis_labels_rotation']), diff --git a/src/Plugin/chart/AbstractChart.php b/src/Plugin/chart/AbstractChart.php old mode 100755 new mode 100644 index cc51fa0..a2422a9 --- a/src/Plugin/chart/AbstractChart.php +++ b/src/Plugin/chart/AbstractChart.php @@ -1,6 +1,7 @@ <?php namespace Drupal\charts\Plugin\chart; + use Drupal\Component\Plugin\PluginBase; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -9,26 +10,34 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base class Chart plugins. */ -abstract class AbstractChart extends PluginBase implements ChartInterface, ContainerFactoryPluginInterface{ - use StringTranslationTrait; - /** - * {@inheritdoc} - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition) - { - parent::__construct($configuration, $plugin_id, $plugin_definition); - } - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition){ - return new static( - $configuration, - $plugin_id, - $plugin_definition - ); - } - public function getChartName() { - return $this->pluginDefinition['name']; - } -} \ No newline at end of file +abstract class AbstractChart extends PluginBase implements ChartInterface, ContainerFactoryPluginInterface { + + use StringTranslationTrait; + + /** + * {@inheritdoc} + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, $plugin_id, $plugin_definition + ); + } + + /** + * Get Chart Name. + * + * @return string + * Chart Name. + */ + public function getChartName() { + return $this->pluginDefinition['name']; + } + +} diff --git a/src/Plugin/views/display/ChartsPluginDisplayChart.php b/src/Plugin/views/display/ChartsPluginDisplayChart.php index af2feea..f28461a 100644 --- a/src/Plugin/views/display/ChartsPluginDisplayChart.php +++ b/src/Plugin/views/display/ChartsPluginDisplayChart.php @@ -48,12 +48,13 @@ class ChartsPluginDisplayChart extends Attachment { $categories['attachment'] = [ 'title' => t('Chart settings'), 'column' => 'second', - 'build' => ['#weight' => -10,], + 'build' => ['#weight' => -10], ]; $displays = array_filter($this->getOption('displays')); if (count($displays) > 1) { $attach_to = $this->t('Multiple displays'); - } elseif (count($displays) == 1) { + } + elseif (count($displays) == 1) { $display = array_shift($displays); if ($display = $this->view->storage->getDisplay($display)) { $attach_to = $display['display_title']; @@ -118,6 +119,7 @@ class ChartsPluginDisplayChart extends Attachment { case 'displays': $form_state->setValue($section, array_filter($form_state->getValue($section))); break; + // @todo set isDefaulted to false by default. case 'inherit_arguments': case 'inherit_exposed_filters': diff --git a/src/Plugin/views/style/ChartsPluginStyleChart.php b/src/Plugin/views/style/ChartsPluginStyleChart.php index 96c2f28..98fc943 100644 --- a/src/Plugin/views/style/ChartsPluginStyleChart.php +++ b/src/Plugin/views/style/ChartsPluginStyleChart.php @@ -93,14 +93,13 @@ class ChartsPluginStyleChart extends StylePluginBase { $dataFieldsCounter = 0; foreach ($dataFields as $value) { - /** - * Skip title field no need to validate it and if data field is set add to dataFieldsValueState array state 1 - * otherwise add to same array state 0 - */ + // Skip title field no need to validate it and if data field is set add to + // dataFieldsValueState array state 1 otherwise add to same array state 0. if ($dataFieldsCounter > 0) { if (empty($value)) { array_push($dataFieldsValueState, 0); - } else { + } + else { array_push($dataFieldsValueState, 1); } } @@ -108,10 +107,9 @@ class ChartsPluginStyleChart extends StylePluginBase { $dataFieldsCounter++; } - /** - * If total sum of dataFieldsValueState is less than 1, then no dataFields were selected otherwise 1 or more selected - * total sum will be greater than 1 - */ + // If total sum of dataFieldsValueState is less than 1, then no dataFields + // were selected otherwise 1 or more selected total sum will be greater + // than 1. if (array_sum($dataFieldsValueState) < 1) { $errors[] = $this->t('At least one data field must be selected in the chart configuration before this chart may be shown'); } @@ -164,7 +162,7 @@ class ChartsPluginStyleChart extends StylePluginBase { '#width' => $this->options['width'], '#height' => $this->options['height'], '#view' => $this->view, - // Pass info about the actual view results to allow further processing + // Pass info about the actual view results to allow further processing. '#theme' => 'views_view_charts', ]; $chart_type_info = charts_get_type($this->options['type']); @@ -185,9 +183,10 @@ class ChartsPluginStyleChart extends StylePluginBase { // Convert empty strings to NULL. if ($value === '') { $value = NULL; - } // Strip thousands placeholders if present, then cast to float. + } else { - $value = (float)str_replace([',', ' '], '', $value); + // Strip thousands placeholders if present, then cast to float. + $value = (float) str_replace([',', ' '], '', $value); } $data_row[] = $value; $data[] = $data_row; @@ -203,7 +202,8 @@ class ChartsPluginStyleChart extends StylePluginBase { '#title' => $data_field['label'], ]; - } else { + } + else { $chart['xaxis'] = [ '#type' => 'chart_xaxis', '#title' => $this->options['xaxis_title'] ? $this->options['xaxis_title'] : FALSE, @@ -254,9 +254,10 @@ class ChartsPluginStyleChart extends StylePluginBase { // Convert empty strings to NULL. if ($value === '') { $value = NULL; - } // Strip thousands placeholders if present, then cast to float. + } else { - $value = (float)str_replace([',', ' '], '', $value); + // Strip thousands placeholders if present, then cast to float. + $value = (float) str_replace([',', ' '], '', $value); } $chart[$series_key]['#data'][] = $value; } @@ -268,7 +269,7 @@ class ChartsPluginStyleChart extends StylePluginBase { // Check if this display has any children charts that should be applied // on top of it. $children_displays = $this->getChildrenChartDisplays(); - //contains the different subviews of the attachments + // Contains the different subviews of the attachments. $attachments = []; $service = \Drupal::service('charts.charts_attachment'); @@ -301,18 +302,19 @@ class ChartsPluginStyleChart extends StylePluginBase { } $subchart = $subview->style_plugin->render(); - array_push($attachments, $subview); //add attachment views to attachments array + // Add attachment views to attachments array. + array_push($attachments, $subview); /*$subview->postExecute(); unset($subview);*/ - // Create a secondary axis if needed. + // Create a secondary axis if needed. if ($this->view->displayHandlers->get($child_display)->options['inherit_yaxis'] !== '1' && isset($subchart['yaxis'])) { $chart['secondary_yaxis'] = $subchart['yaxis']; $chart['secondary_yaxis']['#opposite'] = TRUE; } // Merge in the child chart data. - //foreach (\Drupal::state()->getMultiple($subchart) as $key) { + /* foreach (\Drupal::state()->getMultiple($subchart) as $key) { */ foreach (Element::children($subchart) as $key) { if ($subchart[$key]['#type'] === 'chart_data') { $chart[$key] = $subchart[$key]; @@ -339,7 +341,7 @@ class ChartsPluginStyleChart extends StylePluginBase { * @return bool * Parent Display. */ - function getParentChartDisplay() { + public function getParentChartDisplay() { $parent_display = FALSE; return $parent_display; @@ -351,13 +353,13 @@ class ChartsPluginStyleChart extends StylePluginBase { * @return array * Children Chart Display. */ - function getChildrenChartDisplays() { + public function getChildrenChartDisplays() { $children_displays = $this->displayHandler->getAttachedDisplays(); foreach ($children_displays as $key => $child) { $display_handler = $this->view->displayHandlers->get($child); - //unset disabled & non chart attachments. - if ((!$display_handler->isEnabled()) || (strstr($child,'chart_extension') == !TRUE)) { + // Unset disabled & non chart attachments. + if ((!$display_handler->isEnabled()) || (strstr($child, 'chart_extension') == !TRUE)) { unset($children_displays[$key]); } } diff --git a/src/Services/ChartsSettingsService.php b/src/Services/ChartsSettingsService.php index ed46eed..d5c093f 100644 --- a/src/Services/ChartsSettingsService.php +++ b/src/Services/ChartsSettingsService.php @@ -9,12 +9,12 @@ use Drupal\Core\Config\ConfigFactory; */ class ChartsSettingsService implements ChartsSettingsServiceInterface { - //private $editableConfigName = 'charts.settings'; + /* private $editableConfigName = 'charts.settings'; */ private $configFactory; /** * Construct. - * + * * @param \Drupal\Core\Config\ConfigFactory $config_factory * Config factory. */ @@ -30,4 +30,4 @@ class ChartsSettingsService implements ChartsSettingsServiceInterface { return $config->get('charts_default_settings'); } -} \ No newline at end of file +} diff --git a/src/Theme/ChartsInterface.php b/src/Theme/ChartsInterface.php index d3fb36b..6b43398 100644 --- a/src/Theme/ChartsInterface.php +++ b/src/Theme/ChartsInterface.php @@ -10,8 +10,8 @@ interface ChartsInterface { /** * Used to define a single axis. * - * Constant used in hook_charts_type_info() to declare chart types with a single - * axis. For example a pie chart only has a single dimension. + * Constant used in hook_charts_type_info() to declare chart types with a + * single axis. For example a pie chart only has a single dimension. */ const CHARTS_SINGLE_AXIS = 'y_only'; diff --git a/src/Util/Util.php b/src/Util/Util.php index e57817f..c2560c3 100644 --- a/src/Util/Util.php +++ b/src/Util/Util.php @@ -3,12 +3,15 @@ namespace Drupal\charts\Util; use Drupal\views\ViewExecutable; +use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Util. */ class Util { + use StringTranslationTrait; + /** * Views Data. * @@ -34,8 +37,8 @@ class Util { foreach ($labelValues as $fieldId => $rowDataValue) { $alter_text = $view->field[$labelField]->options['alter']['alter_text']; if ($alter_text) { - $text = $view->field[$labelField]->options['alter']['text']; - $tokenized_text = trim(str_replace("\n", '', strip_tags($view->field[$labelField]->tokenizeValue($text, $row_number)))); + $text = $view->field[$labelField]->options['alter']['text']; + $tokenized_text = trim(str_replace("\n", '', strip_tags($view->field[$labelField]->tokenizeValue($text, $row_number)))); } $rowData[$numberFields] = [ 'value' => $view->field[$fieldId]->getValue($row), @@ -74,7 +77,7 @@ class Util { /** * Creates chart data to be used later by visualization frameworks. * - * @param string $data + * @param array $data * Data. * * @return array @@ -114,8 +117,7 @@ class Util { public static function checkMissingLibrary($moduleName = '', $libraryPath = '') { $module_path = drupal_get_path('module', $moduleName); if (!file_exists($module_path . $libraryPath)) { - drupal_set_message(t('Charting libraries for ' . $moduleName . ' might - not be installed. Run \'composer install\' for ' . $moduleName . ' sub-module.'), 'error'); + drupal_set_message($this->t('Charting libraries for ' . $moduleName . ' might not be installed. Run \'composer install\' for ' . $moduleName . ' sub-module.'), 'error'); } } -- GitLab