From 706a136555fad22067c24facc62c66122aed3fdb Mon Sep 17 00:00:00 2001
From: andileco <andileco@2054544.no-reply.drupal.org>
Date: Sun, 4 Mar 2018 12:52:18 -0500
Subject: [PATCH] Issue #2943952 by andileco: Enable stacking

---
 includes/charts.pages.inc                     |  9 ++++++++
 .../src/Plugin/Block/ChartsBlock.php          |  1 +
 modules/charts_c3/src/Plugin/chart/C3.php     | 10 ++++++++
 .../src/Settings/CThree/ChartData.php         | 23 ++++++++++++++++++-
 src/Form/ChartsConfigForm.php                 | 10 ++++++++
 .../views/style/ChartsPluginStyleChart.php    |  2 +-
 6 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/includes/charts.pages.inc b/includes/charts.pages.inc
index f59e2e5..7907c1a 100644
--- a/includes/charts.pages.inc
+++ b/includes/charts.pages.inc
@@ -283,6 +283,15 @@ function charts_settings_form($form, $defaults = [], $field_options = [], $paren
     }
   }
 
+  // Enable stacking.
+  $form['grouping'] = [
+    '#type' => 'checkbox',
+    '#title' => t('Stacking'),
+    '#description' => t('Enable stacking'),
+    '#default_value' => $options['grouping'],
+    '#weight' => '-19',
+  ];
+
   if ($field_options) {
     $first_field = key($field_options);
 
diff --git a/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php b/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php
index b0df36f..a488fd5 100644
--- a/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php
+++ b/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php
@@ -110,6 +110,7 @@ class ChartsBlock extends BlockBase {
       '#weight' => '-16',
     ];
     // Enable stacking.
+    unset($form['grouping']['#parents']);
     $form['grouping'] = [
       '#type' => 'checkbox',
       '#title' => $this->t('Stacking'),
diff --git a/modules/charts_c3/src/Plugin/chart/C3.php b/modules/charts_c3/src/Plugin/chart/C3.php
index d130c21..f100d93 100644
--- a/modules/charts_c3/src/Plugin/chart/C3.php
+++ b/modules/charts_c3/src/Plugin/chart/C3.php
@@ -136,10 +136,20 @@ class C3 extends AbstractChart {
     }
     $chartData->types = $types;
 
+
     if ($options['type'] != 'pie' && $options['type'] != 'donut') {
       $c3->setAxis($chartAxis);
     }
 
+    // Determines if chart is stacked.
+    if (!empty($options['grouping'] && $options['grouping'] == TRUE)) {
+      $seriesNames = [];
+      for ($i = 0; $i < count($seriesData); $i++) {
+        array_push($seriesNames, $seriesData[$i]['name']);
+      }
+      $chartData->setGroups([$seriesNames]);
+    }
+
     $chartColor = new ChartColor();
     $seriesColors = [];
     for ($i = 0; $i < count($seriesData); $i++) {
diff --git a/modules/charts_c3/src/Settings/CThree/ChartData.php b/modules/charts_c3/src/Settings/CThree/ChartData.php
index 8768a57..f92bebb 100644
--- a/modules/charts_c3/src/Settings/CThree/ChartData.php
+++ b/modules/charts_c3/src/Settings/CThree/ChartData.php
@@ -11,11 +11,12 @@ class ChartData implements \JsonSerializable {
   private $type;
   private $labels = TRUE;
   private $x = 'x';
+  private $groups = '';
 
   /**
    * Get X.
    *
-   * @return array
+   * @return mixed
    *   X.
    */
   public function getX() {
@@ -92,6 +93,26 @@ class ChartData implements \JsonSerializable {
     $this->labels = $labels;
   }
 
+  /**
+   * Get Stacking.
+   *
+   * @return array
+   *   Stacking.
+   */
+  public function getGroups() {
+    return $this->groups;
+  }
+
+  /**
+   * Set Stacking.
+   *
+   * @param array $groups
+   *   Stacking.
+   */
+  public function setGroups($groups) {
+    $this->groups = $groups;
+  }
+
   /**
    * Json Serialize.
    *
diff --git a/src/Form/ChartsConfigForm.php b/src/Form/ChartsConfigForm.php
index 3cd8617..1d9d853 100644
--- a/src/Form/ChartsConfigForm.php
+++ b/src/Form/ChartsConfigForm.php
@@ -269,6 +269,16 @@ class ChartsConfigForm extends ConfigFormBase {
       }
     }
 
+    // Enable stacking.
+    $form['grouping'] = [
+      '#type'          => 'checkbox',
+      '#title'         => $this->t('Stacking'),
+      '#description'   => $this->t('Enable stacking'),
+      '#default_value' => $options['grouping'],
+      '#weight'        => '-15',
+      '#parents'       => array_merge($parents, ['grouping']),
+    ];
+
     $form['display'] = [
       '#title'       => $this->t('Display'),
       '#type'        => 'details',
diff --git a/src/Plugin/views/style/ChartsPluginStyleChart.php b/src/Plugin/views/style/ChartsPluginStyleChart.php
index 6aa0f5e..60324b6 100644
--- a/src/Plugin/views/style/ChartsPluginStyleChart.php
+++ b/src/Plugin/views/style/ChartsPluginStyleChart.php
@@ -66,7 +66,7 @@ class ChartsPluginStyleChart extends StylePluginBase {
     // Limit grouping options (we only support one grouping field).
     if (isset($form['grouping'][0])) {
       $form['grouping'][0]['field']['#title'] = t('Grouping field');
-      $form['grouping'][0]['field']['#description'] = t('If grouping by a particular field, that field will be used to determine stacking of the chart. Generally this will be the same field as what you select for the "Label field" below. If you do not have more than one "Provides data" field below, there will be nothing to stack. If you want to have another series displayed, use a "Chart attachment" display, and set it to attach to this display. Stacking is still a work in progress for C3 Charts.');
+      $form['grouping'][0]['field']['#description'] = t('If grouping by a particular field, that field will be used to determine stacking of the chart. Generally this will be the same field as what you select for the "Label field" below. If you do not have more than one "Provides data" field below, there will be nothing to stack. If you want to have another series displayed, use a "Chart attachment" display, and set it to attach to this display.');
       $form['grouping'][0]['field']['#attributes']['class'][] = 'charts-grouping-field';
       // Grouping by rendered version has no effect in charts. Hide the options.
       $form['grouping'][0]['rendered']['#access'] = FALSE;
-- 
GitLab