From 0330f6f5dcef42cb73a6f8f96b81b3aa653bc33b Mon Sep 17 00:00:00 2001
From: Daniel Cothran <daniel@andile.co>
Date: Wed, 28 Feb 2018 19:33:41 -0500
Subject: [PATCH] Issue #2947190 by zinalacina, andileco: C3 Charts donut Chart
 Doesn't Render Properly

---
 modules/charts_c3/src/Plugin/chart/C3.php     | 14 ++--
 .../charts_google/src/Plugin/chart/Google.php | 69 +++++++++++++++----
 2 files changed, 61 insertions(+), 22 deletions(-)

diff --git a/modules/charts_c3/src/Plugin/chart/C3.php b/modules/charts_c3/src/Plugin/chart/C3.php
index de99739..8007823 100644
--- a/modules/charts_c3/src/Plugin/chart/C3.php
+++ b/modules/charts_c3/src/Plugin/chart/C3.php
@@ -51,12 +51,13 @@ class C3 extends AbstractChart {
     }
 
     /**
-     * Depending on the number of data fields, the charts will either use data
-     * fields or label fields for the categories. If only one data field is
-     * selected, then the label field will serve as the categories. If multiple
-     * data fields are selected, they will become the categories.
+     * For pie and donut chart types, depending on the number of data fields,
+     * the charts will either use data fields or label fields for the
+     * categories. If only one data field is selected, then the label field
+     * will serve as the categories. If multiple data fields are selected,
+     * they will become the categories.
      * */
-    if ($options['type'] == 'pie' || $options['type'] == 'donut' ) {
+    if ($options['type'] == 'pie' || $options['type'] == 'donut') {
       if (count($seriesData) > 1) {
         $c3Data = [];
         for ($i = 0; $i < count($seriesData); $i++) {
@@ -82,8 +83,7 @@ class C3 extends AbstractChart {
         array_push($c3Data, $c3DataTemp);
       }
     }
-
-
+    
     $c3Chart = new ChartType();
     $c3Chart->setType($options['type']);
     $c3ChartTitle = new ChartTitle();
diff --git a/modules/charts_google/src/Plugin/chart/Google.php b/modules/charts_google/src/Plugin/chart/Google.php
index ed66022..3c606d9 100644
--- a/modules/charts_google/src/Plugin/chart/Google.php
+++ b/modules/charts_google/src/Plugin/chart/Google.php
@@ -46,23 +46,63 @@ class Google extends AbstractChart {
       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);
+    /**
+     * For pie and donut chart types, depending on the number of data fields,
+     * the charts will either use data fields or label fields for the
+     * categories. If only one data field is selected, then the label field
+     * will serve as the categories. If multiple data fields are selected,
+     * they will become the categories.
+     * */
+    if ($options['type'] == 'pie' || $options['type'] == 'donut') {
+      if ($seriesCount > 1) {
+        $dataTable = [];
+        for ($j = 0; $j < $seriesCount; $j++) {
+          $rowDataTable = [];
+          $rowDataTabletemp = array_sum($seriesData[$j]['data']);
+          array_push($rowDataTable, $rowDataTabletemp);
+          array_unshift($rowDataTable, $seriesData[$j]['name']);
+          array_push($dataTable, $rowDataTable);
+        }
+        $dataTableHeader = ['label','value'];
+        array_unshift($dataTable, $dataTableHeader);
+      }
+      else {
+        $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);
       }
-      array_unshift($rowDataTable, $categories[$j]);
-      array_push($dataTable, $rowDataTable);
     }
-
-    $dataTableHeader = [];
-    for ($r = 0; $r < $seriesCount; $r++) {
-      array_push($dataTableHeader, $seriesData[$r]['name']);
+    else {
+      $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);
     }
-    array_unshift($dataTableHeader, 'label');
-    array_unshift($dataTable, $dataTableHeader);
 
     $googleOptions = $this->createChartsOptions($options, $seriesData, $attachmentDisplayOptions);
     $googleChartType = $this->createChartType($options);
@@ -72,7 +112,6 @@ class Google extends AbstractChart {
     $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);
-
   }
 
   /**
-- 
GitLab