From 7a5be6c64065f1c10c3ea8a8e6213bcca4ddc764 Mon Sep 17 00:00:00 2001 From: Daniel Cothran <daniel@andile.co> Date: Sun, 11 Feb 2018 15:18:28 -0500 Subject: [PATCH] Issue #2943952 by andileco: Enable stacking --- charts.module | 4 +- .../src/Plugin/Block/ChartsBlock.php | 2 +- .../templates/charts-block.html.twig | 2 +- .../src/Charts/HighchartsChartsRender.php | 18 +++-- .../src/Plugin/chart/Highchart.php | 21 ++++-- .../Highcharts/PlotOptionsStacking.php | 65 +++++++++++++++++++ .../views/style/ChartsPluginStyleChart.php | 2 +- templates/views-view-charts.html.twig | 4 +- 8 files changed, 101 insertions(+), 17 deletions(-) create mode 100644 modules/charts_highcharts/src/Settings/Highcharts/PlotOptionsStacking.php diff --git a/charts.module b/charts.module index a6d11c1..0a01040 100644 --- a/charts.module +++ b/charts.module @@ -97,13 +97,15 @@ function template_preprocess_views_view_charts(&$variables) { if (!isset($plugin_definitions)) { // To be removed in drupal 9. - $moduleSelector = new ModuleSelector($library, $categories, $seriesData, $options, $attachmentDisplayOptions, $chartId); + $moduleSelector = new ModuleSelector($library, $categories, $seriesData, $attachmentDisplayOptions, $options, $chartId); if ($moduleSelector->moduleExists()) { $moduleSelector->buildVariables($variables); } } else { $plugin = $plugin_manager->createInstance($library); + $variables['height'] = $options['height']; + $variables['width'] = $options['width']; $plugin->buildVariables($options, $categories, $seriesData, $attachmentDisplayOptions, $variables, $chartId); } } diff --git a/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php b/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php index b722269..63dd860 100644 --- a/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php +++ b/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php @@ -134,7 +134,7 @@ class ChartsBlock extends BlockBase { unset($form['display']['background']['#parents']); $form['display']['background']['#default_value'] = $this->configuration['background']; - + unset($form['display']['three_dimensional']['#parents']); $form['display']['three_dimensional']['#default_value'] = $this->configuration['three_dimensional']; diff --git a/modules/charts_blocks/templates/charts-block.html.twig b/modules/charts_blocks/templates/charts-block.html.twig index b2a8950..734319d 100644 --- a/modules/charts_blocks/templates/charts-block.html.twig +++ b/modules/charts_blocks/templates/charts-block.html.twig @@ -1,3 +1,3 @@ {% set library, height, width = 'charts_' ~ library ~ '/' ~ library, options.height, options.width %} {{ attach_library("#{ library }") }} -<div {{ attributes }} {{ content_attributes }} style="width:{{ width }};height:{{ height }};"></div> +<div {{ attributes }} {{ content_attributes }} style="{% if width is not empty %}width:{{ width }}px;{% endif %}{% if height is not empty %}height:{{ height }}px;{% endif %}"></div> diff --git a/modules/charts_highcharts/src/Charts/HighchartsChartsRender.php b/modules/charts_highcharts/src/Charts/HighchartsChartsRender.php index 93db286..1f85601 100644 --- a/modules/charts_highcharts/src/Charts/HighchartsChartsRender.php +++ b/modules/charts_highcharts/src/Charts/HighchartsChartsRender.php @@ -6,6 +6,7 @@ use Drupal\charts\Charts\ChartsRenderInterface; use Drupal\charts\Util\Util; use Drupal\charts_highcharts\Settings\Highcharts\Chart; use Drupal\charts_highcharts\Settings\Highcharts\ChartTitle; +use Drupal\charts_highcharts\Settings\Highcharts\PlotOptionsStacking; use Drupal\charts_highcharts\Settings\Highcharts\Xaxis; use Drupal\charts_highcharts\Settings\Highcharts\XaxisTitle; use Drupal\charts_highcharts\Settings\Highcharts\ChartLabel; @@ -143,13 +144,22 @@ class HighchartsChartsRender implements ChartsRenderInterface { array_push($yAxes, $chartYaxisSecondary); } - // Set Plotoptions. + // Set plot options. $plotOptions = new PlotOptions(); + $plotOptionsStacking = new PlotOptionsStacking(); $plotOptionsSeries = new PlotOptionsSeries(); $plotOptionsSeriesDataLabels = new PlotOptionsSeriesDataLabels(); - $plotOptions->setPlotSeries($plotOptionsSeries); - $plotOptionsSeries->setDataLabels($plotOptionsSeriesDataLabels); - $plotOptionsSeriesDataLabels->setEnabled($options['data_labels']); + // Set plot options if stacked chart. + if(!empty($options['grouping'])) { + $plotOptions->setPlotSeries($plotOptionsStacking); + $plotOptionsStacking->setDataLabels($plotOptionsSeriesDataLabels); + $plotOptionsSeriesDataLabels->setEnabled($options['data_labels']); + } + else { + $plotOptions->setPlotSeries($plotOptionsSeries); + $plotOptionsSeries->setDataLabels($plotOptionsSeriesDataLabels); + $plotOptionsSeriesDataLabels->setEnabled($options['data_labels']); + } // Set Tooltip. $chartTooltip = new Tooltip(); diff --git a/modules/charts_highcharts/src/Plugin/chart/Highchart.php b/modules/charts_highcharts/src/Plugin/chart/Highchart.php index e05dee7..e2000ea 100644 --- a/modules/charts_highcharts/src/Plugin/chart/Highchart.php +++ b/modules/charts_highcharts/src/Plugin/chart/Highchart.php @@ -3,10 +3,9 @@ namespace Drupal\charts_highcharts\Plugin\chart; use Drupal\charts\Plugin\chart\AbstractChart; -/* 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\ChartTitle; +use Drupal\charts_highcharts\Settings\Highcharts\PlotOptionsStacking; use Drupal\charts_highcharts\Settings\Highcharts\Xaxis; use Drupal\charts_highcharts\Settings\Highcharts\XaxisTitle; use Drupal\charts_highcharts\Settings\Highcharts\ChartLabel; @@ -150,13 +149,22 @@ class Highchart extends AbstractChart { array_push($yAxes, $chartYaxisSecondary); } - // Set Plotoptions. + // Set plot options. $plotOptions = new PlotOptions(); + $plotOptionsStacking = new PlotOptionsStacking(); $plotOptionsSeries = new PlotOptionsSeries(); $plotOptionsSeriesDataLabels = new PlotOptionsSeriesDataLabels(); - $plotOptions->setPlotSeries($plotOptionsSeries); - $plotOptionsSeries->setDataLabels($plotOptionsSeriesDataLabels); - $plotOptionsSeriesDataLabels->setEnabled($options['data_labels']); + // Set plot options if stacked chart. + if(!empty($options['grouping'])) { + $plotOptions->setPlotSeries($plotOptionsStacking); + $plotOptionsStacking->setDataLabels($plotOptionsSeriesDataLabels); + $plotOptionsSeriesDataLabels->setEnabled($options['data_labels']); + } + else { + $plotOptions->setPlotSeries($plotOptionsSeries); + $plotOptionsSeries->setDataLabels($plotOptionsSeriesDataLabels); + $plotOptionsSeriesDataLabels->setEnabled($options['data_labels']); + } // Set Tooltip. $chartTooltip = new Tooltip(); @@ -186,7 +194,6 @@ class Highchart extends AbstractChart { $highchart->setChart($chart); $highchart->setTitle($chartTitle); $highchart->setAxisX($chartXaxis); - /* $highchart->yAxis = $yAxes; */ $highchart->setTooltip($chartTooltip); $highchart->setPlotOptions($plotOptions); $highchart->setCredits($chartCredits); diff --git a/modules/charts_highcharts/src/Settings/Highcharts/PlotOptionsStacking.php b/modules/charts_highcharts/src/Settings/Highcharts/PlotOptionsStacking.php new file mode 100644 index 0000000..58252e9 --- /dev/null +++ b/modules/charts_highcharts/src/Settings/Highcharts/PlotOptionsStacking.php @@ -0,0 +1,65 @@ +<?php + +namespace Drupal\charts_highcharts\Settings\Highcharts; + +/** + * Plot Options Series. + */ +class PlotOptionsStacking implements \JsonSerializable { + + private $dataLabels; + private $stacking = 'normal'; + + /** + * 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; + } + + /** + * Get Stacking. + * + * @return mixed + * Stacking. + */ + public function getStacking() { + return $this->stacking; + } + + /** + * Set Stacking. + * + * @param mixed $stacking + * Stacking. + */ + public function setStacking($stacking) { + $this->stacking = $stacking; + } + + /** + * Json Serialize. + * + * @return array + * Json Serialize. + */ + public function jsonSerialize() { + $vars = get_object_vars($this); + + return $vars; + } + +} diff --git a/src/Plugin/views/style/ChartsPluginStyleChart.php b/src/Plugin/views/style/ChartsPluginStyleChart.php index e7e2b9f..b10c546 100644 --- a/src/Plugin/views/style/ChartsPluginStyleChart.php +++ b/src/Plugin/views/style/ChartsPluginStyleChart.php @@ -24,7 +24,7 @@ use Drupal\views\Plugin\views\style\StylePluginBase; */ class ChartsPluginStyleChart extends StylePluginBase { - protected $usesGrouping = FALSE; + // protected $usesGrouping = FALSE; protected $usesFields = TRUE; protected $usesRowPlugin = TRUE; diff --git a/templates/views-view-charts.html.twig b/templates/views-view-charts.html.twig index f46df05..4d1a113 100644 --- a/templates/views-view-charts.html.twig +++ b/templates/views-view-charts.html.twig @@ -1,3 +1,3 @@ -{% set library, height, width = 'charts_' ~ chart_type ~ '/' ~ chart_type, options.height, options.width %} +{% set library, height, width = 'charts_' ~ chart_type ~ '/' ~ chart_type, height, width %} {{ attach_library("#{ library }") }} -<div {{ attributes }} {{ content_attributes }} style="width:{{ width }};height:{{ height }};"></div> +<div {{ attributes }} {{ content_attributes }} style="{% if width is not empty %}width:{{ width }}px;{% endif %}{% if height is not empty %}height:{{ height }}px;{% endif %}"></div> -- GitLab