From 8a2dbdc48f8556c0271d930b3833d1d6e39531d2 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Tue, 5 Mar 2019 21:15:45 +0200 Subject: [PATCH] feat(legacy-preset-chart-nvd3): show negative values on bars (#8) * fix(legacy-preset-chart-nvd3): redraw bar values after legend change * fix(legacy-preset-chart-nvd3): show labels on negative bars * fix(legacy-preset-chart-nvd3): fix lint * fix(legacy-preset-chart-nvd3): improve remove --- .../src/utils.js | 31 ++++++++----- .../legacy-preset-chart-nvd3/Bar/Stories.jsx | 46 ++++++++++++++++++- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/packages/superset-ui-legacy-preset-chart-nvd3/src/utils.js b/packages/superset-ui-legacy-preset-chart-nvd3/src/utils.js index 376ccca98..028fec1f4 100644 --- a/packages/superset-ui-legacy-preset-chart-nvd3/src/utils.js +++ b/packages/superset-ui-legacy-preset-chart-nvd3/src/utils.js @@ -72,19 +72,26 @@ export function drawBarValues(svg, data, stacked, axisFormat) { .selectAll('rect') .each(function each(d, index) { const rectObj = d3.select(this); + const transformAttr = rectObj.attr('transform'); + const xPos = parseFloat(rectObj.attr('x')); + const yPos = parseFloat(rectObj.attr('y')); + const rectWidth = parseFloat(rectObj.attr('width')); + const rectHeight = parseFloat(rectObj.attr('height')); + const textEls = groupLabels + .append('text') + .text(format(stacked ? totalStackedValues[index] : d.y)) + .attr('transform', transformAttr) + .attr('class', 'bar-chart-label'); + + // fine tune text position + const bbox = textEls.node().getBBox(); + const labelWidth = bbox.width; + const labelHeight = bbox.height; + textEls.attr('x', xPos + rectWidth / 2 - labelWidth / 2); if (rectObj.attr('class').includes('positive')) { - const transformAttr = rectObj.attr('transform'); - const yPos = parseFloat(rectObj.attr('y')); - const xPos = parseFloat(rectObj.attr('x')); - const rectWidth = parseFloat(rectObj.attr('width')); - const textEls = groupLabels - .append('text') - .attr('y', yPos - 5) - .text(format(stacked ? totalStackedValues[index] : d.y)) - .attr('transform', transformAttr) - .attr('class', 'bar-chart-label'); - const labelWidth = textEls.node().getBBox().width; - textEls.attr('x', xPos + rectWidth / 2 - labelWidth / 2); // fine tune + textEls.attr('y', yPos - 5); + } else { + textEls.attr('y', yPos + rectHeight + labelHeight); } }); }, ANIMATION_TIME); diff --git a/packages/superset-ui-plugins-demo/storybook/stories/legacy-preset-chart-nvd3/Bar/Stories.jsx b/packages/superset-ui-plugins-demo/storybook/stories/legacy-preset-chart-nvd3/Bar/Stories.jsx index 0019b9a17..0e028bbeb 100644 --- a/packages/superset-ui-plugins-demo/storybook/stories/legacy-preset-chart-nvd3/Bar/Stories.jsx +++ b/packages/superset-ui-plugins-demo/storybook/stories/legacy-preset-chart-nvd3/Bar/Stories.jsx @@ -79,7 +79,51 @@ export default [ }} /> ), - storyName: 'Bar values', + storyName: 'Bar with values', + storyPath: 'legacy-|preset-chart-nvd3|BarChartPlugin', + }, + { + renderStory: () => ( + ({ + ...group, + values: group.values.map(pair => ({ ...pair, y: (i % 2 === 0 ? 1 : -1) * pair.y })), + })), + }, + width: 400, + }} + /> + ), + storyName: 'Bar with positive and negative values', storyPath: 'legacy-|preset-chart-nvd3|BarChartPlugin', }, ];