From d612e91c6b45393446c5100958e2789b0a7e0be4 Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Tue, 6 Feb 2018 00:21:00 -0800 Subject: [PATCH 1/2] Minor fixes to sunburst closes https://github.com/apache/incubator-superset/issues/4340 --- .../javascripts/explore/stores/visTypes.js | 5 ++-- superset/assets/visualizations/sunburst.js | 2 +- superset/viz.py | 28 +++++++++---------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/superset/assets/javascripts/explore/stores/visTypes.js b/superset/assets/javascripts/explore/stores/visTypes.js index 38ff52d23aaf0..9d62eada883f4 100644 --- a/superset/assets/javascripts/explore/stores/visTypes.js +++ b/superset/assets/javascripts/explore/stores/visTypes.js @@ -1112,9 +1112,10 @@ export const visTypes = { }, secondary_metric: { label: t('Secondary Metric'), - description: t('This secondary metric is used to ' + + default: null, + description: t('[optional] this secondary metric is used to ' + 'define the color as a ratio against the primary metric. ' + - 'If the two metrics match, color is mapped level groups'), + 'When omitted, the color is categorical and based on labels'), }, groupby: { label: t('Hierarchy'), diff --git a/superset/assets/visualizations/sunburst.js b/superset/assets/visualizations/sunburst.js index 41859e3a2d87c..199d182a33813 100644 --- a/superset/assets/visualizations/sunburst.js +++ b/superset/assets/visualizations/sunburst.js @@ -347,7 +347,7 @@ function sunburstVis(slice, payload) { let ext; const fd = slice.formData; - if (fd.metric !== fd.secondary_metric) { + if (fd.metric !== fd.secondary_metric && fd.secondary_metric) { colorByCategory = false; ext = d3.extent(nodes, d => d.m2 / d.m1); colorScale = d3.scale.linear() diff --git a/superset/viz.py b/superset/viz.py index 6fc8cf9887095..f32bc532b900c 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -1346,24 +1346,22 @@ class SunburstViz(BaseViz): '@bl.ocks.org') def get_data(self, df): - - # if m1 == m2 duplicate the metric column - cols = self.form_data.get('groupby') - metric = self.form_data.get('metric') - secondary_metric = self.form_data.get('secondary_metric') - if metric == secondary_metric: - ndf = df - ndf.columns = [cols + ['m1', 'm2']] - else: - cols += [ - self.form_data['metric'], self.form_data['secondary_metric']] - ndf = df[cols] - return json.loads(ndf.to_json(orient='values')) # TODO fix this nonsense + fd = self.form_data + cols = fd.get('groupby') + metric = fd.get('metric') + secondary_metric = fd.get('secondary_metric') + if metric == secondary_metric or secondary_metric is None: + df.columns = cols + ['m1'] + df['m2'] = df['m1'] + return json.loads(df.to_json(orient='values')) def query_obj(self): qry = super(SunburstViz, self).query_obj() - qry['metrics'] = [ - self.form_data['metric'], self.form_data['secondary_metric']] + fd = self.form_data + qry['metrics'] = [fd['metric']] + sm = fd.get('secondary_metric') + if sm and sm != fd['metric']: + qry['metrics'].append(sm) return qry From 988d1485fd71435178ba74b3650b78ade77d6e69 Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Tue, 6 Feb 2018 15:32:12 -0800 Subject: [PATCH 2/2] nit --- superset/viz.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/superset/viz.py b/superset/viz.py index f32bc532b900c..0cfceae36572e 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -1359,9 +1359,9 @@ def query_obj(self): qry = super(SunburstViz, self).query_obj() fd = self.form_data qry['metrics'] = [fd['metric']] - sm = fd.get('secondary_metric') - if sm and sm != fd['metric']: - qry['metrics'].append(sm) + secondary_metric = fd.get('secondary_metric') + if secondary_metric and secondary_metric != fd['metric']: + qry['metrics'].append(secondary_metric) return qry