[WIP] [Discuss] Make use of all grouped data to draw pie chart

### What is this PR for?
Now, grouped pie charts only uses the data from the first group
With this fix-
* Add data from all groups to variable d3g, so all groups could be rendered
* Rewrite for loop with map and concat
* Refactor some variables to const and let

### What type of PR is it?
[Bug Fix]

### What is the Jira issue?
*  [ZEPPELIN-2237](https://issues.apache.org/jira/browse/ZEPPELIN-2237)

### How should this be tested?
* Create a built in pie chart visualization
* Select a column to group the data
* Should display the visualization based on all the available grouped data

### Questions:
* Does the licenses files need update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No

Author: ess_ess <sravans2...@gmail.com>

This patch had conflicts when merged, resolved by
Committer: Lee moon soo <m...@apache.org>

Closes #2128 from sravan-s/ZEPPELIN-2237-grouped-piechart and squashes the 
following commits:

652c943 [ess_ess] Make use of all grouped data to draw pie chart

(cherry picked from commit a2cd4ae4e17024501bb0654e71747a07ff68600d)
Signed-off-by: Lee moon soo <m...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/zeppelin/repo
Commit: http://git-wip-us.apache.org/repos/asf/zeppelin/commit/7998dd2e
Tree: http://git-wip-us.apache.org/repos/asf/zeppelin/tree/7998dd2e
Diff: http://git-wip-us.apache.org/repos/asf/zeppelin/diff/7998dd2e

Branch: refs/heads/branch-0.7
Commit: 7998dd2ec749f49543fcb4ab9f6f54e537fbdb0f
Parents: 1ff2752
Author: ess_ess <sravans2...@gmail.com>
Authored: Fri Mar 10 09:19:55 2017 +0530
Committer: Lee moon soo <m...@apache.org>
Committed: Wed Mar 15 08:42:21 2017 -0700

----------------------------------------------------------------------
 .../builtins/visualization-piechart.js          | 30 +++++++++++++-------
 1 file changed, 19 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zeppelin/blob/7998dd2e/zeppelin-web/src/app/visualization/builtins/visualization-piechart.js
----------------------------------------------------------------------
diff --git 
a/zeppelin-web/src/app/visualization/builtins/visualization-piechart.js 
b/zeppelin-web/src/app/visualization/builtins/visualization-piechart.js
index 9cc7922..f74ecd0 100644
--- a/zeppelin-web/src/app/visualization/builtins/visualization-piechart.js
+++ b/zeppelin-web/src/app/visualization/builtins/visualization-piechart.js
@@ -35,7 +35,7 @@ export default class PiechartVisualization extends 
Nvd3ChartVisualization {
   render(pivot) {
     // [ZEPPELIN-2253] New chart function will be created each time inside 
super.render()
     this.chart = null;
-    var d3Data = this.d3DataFromPivot(
+    const d3Data = this.d3DataFromPivot(
       pivot.schema,
       pivot.rows,
       pivot.keys,
@@ -44,17 +44,25 @@ export default class PiechartVisualization extends 
Nvd3ChartVisualization {
       true,
       false,
       false);
-    var d = d3Data.d3g;
-    var d3g = [];
-    if (d.length > 0) {
-      for (var i = 0; i < d[0].values.length ; i++) {
-        var e = d[0].values[i];
-        d3g.push({
-          label: e.x,
-          value: e.y
-        });
-      }
+    const d = d3Data.d3g;
+
+    let generateLabel;
+    // data is grouped
+    if (pivot.groups && pivot.groups.length > 0) {
+      generateLabel = (suffix, prefix) => `${prefix}.${suffix}`;
+    } else { // data isn't grouped
+      generateLabel = suffix => suffix;
     }
+
+    let d3g = d.map(group => {
+      return group.values.map(row => ({
+        label: generateLabel(row.x, group.key),
+        value: row.y
+      }));
+    });
+    // the map function returns d3g as a nested array
+    // [].concat flattens it, http://stackoverflow.com/a/10865042/5154397
+    d3g = [].concat.apply([], d3g);
     super.render({d3g: d3g});
   };
 

Reply via email to