From c5252d0f43621999030d3dcf3d2e8bfee5452510 Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Mon, 18 Sep 2017 21:32:39 -0700 Subject: [PATCH] [heatmap] account for bounds = 0 (#3474) * [heatmap] account for bounds = 0 * Fix sorting * linting --- .../javascripts/explore/stores/controls.jsx | 8 ++++---- superset/assets/visualizations/heatmap.js | 18 +++++++++++++----- superset/viz.py | 4 ++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/superset/assets/javascripts/explore/stores/controls.jsx b/superset/assets/javascripts/explore/stores/controls.jsx index b87e9881a..c226f66cd 100644 --- a/superset/assets/javascripts/explore/stores/controls.jsx +++ b/superset/assets/javascripts/explore/stores/controls.jsx @@ -37,10 +37,10 @@ const timeColumnOption = { 'account'), }; const sortAxisChoices = [ - ['alpha_asc', 'Alphabetical ascending'], - ['alpha_desc', 'Alphabetical descending'], - ['value_asc', 'Value ascending'], - ['value_desc', 'Value descending'], + ['alpha_asc', 'Axis ascending'], + ['alpha_desc', 'Axis descending'], + ['value_asc', 'sum(value) ascending'], + ['value_desc', 'sum(value) descending'], ]; const groupByControl = { diff --git a/superset/assets/visualizations/heatmap.js b/superset/assets/visualizations/heatmap.js index 1f76a3acb..002af8a70 100644 --- a/superset/assets/visualizations/heatmap.js +++ b/superset/assets/visualizations/heatmap.js @@ -7,6 +7,10 @@ import { colorScalerFactory } from '../javascripts/modules/colors'; import '../stylesheets/d3tip.css'; import './heatmap.css'; +function cmp(a, b) { + return a > b ? 1 : -1; +} + // Inspired from http://bl.ocks.org/mbostock/3074470 // https://jsfiddle.net/cyril123/h0reyumq/ function heatmapVis(slice, payload) { @@ -52,17 +56,21 @@ function heatmapVis(slice, payload) { function ordScale(k, rangeBands, sortMethod) { let domain = {}; + const actualKeys = {}; // hack to preserve type of keys when number data.forEach((d) => { - domain[d[k]] = domain[d[k]] || 0 + d.v; + domain[d[k]] = (domain[d[k]] || 0) + d.v; + actualKeys[d[k]] = d[k]; }); + // Not usgin object.keys() as it converts to strings + const keys = Object.keys(actualKeys).map(s => actualKeys[s]); if (sortMethod === 'alpha_asc') { - domain = Object.keys(domain).sort(); + domain = keys.sort(cmp); } else if (sortMethod === 'alpha_desc') { - domain = Object.keys(domain).sort().reverse(); + domain = keys.sort(cmp).reverse(); } else if (sortMethod === 'value_desc') { - domain = Object.keys(domain).sort((d1, d2) => domain[d2] - domain[d1]); + domain = Object.keys(domain).sort((a, b) => domain[a] > domain[b] ? -1 : 1); } else if (sortMethod === 'value_asc') { - domain = Object.keys(domain).sort((d1, d2) => domain[d1] - domain[d2]); + domain = Object.keys(domain).sort((a, b) => domain[b] > domain[a] ? -1 : 1); } if (k === 'y' && rangeBands) { diff --git a/superset/viz.py b/superset/viz.py index 1f844edef..b22af0855 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -1484,9 +1484,9 @@ class HeatmapViz(BaseViz): max_ = df.v.max() min_ = df.v.min() bounds = fd.get('y_axis_bounds') - if bounds and bounds[0]: + if bounds and bounds[0] is not None: min_ = bounds[0] - if bounds and bounds[1]: + if bounds and bounds[1] is not None: max_ = bounds[1] if norm == 'heatmap': overall = True