Skip to content

Commit

Permalink
fix(chart): STACKED BARS VALUES did not show correctly when data has …
Browse files Browse the repository at this point in the history
…negative value
  • Loading branch information
ppjmiao committed Mar 3, 2022
1 parent be88cb9 commit 749b760
Showing 1 changed file with 71 additions and 28 deletions.
99 changes: 71 additions & 28 deletions superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,26 @@ export function getTimeOrNumberFormatter(format) {
: getNumberFormatter(format);
}

function getRectPosInfo(rectObj, yValue) {
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 isPositive = rectObj.attr('class').includes('positive');
return {

Check warning on line 57 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L51-L57

Added lines #L51 - L57 were not covered by tests
xPos,
yPos,
rectWidth,
rectHeight,
transformAttr,
yValue,
isPositive,
};
}

export function drawBarValues(svg, data, stacked, axisFormat) {
const format = getNumberFormatter(axisFormat);
const countSeriesDisplayed = data.filter(d => !d.disabled).length;
const totalStackedValues =
stacked && data.length !== 0
? data[0].values.map((bar, iBar) => {
Expand All @@ -67,34 +84,60 @@ export function drawBarValues(svg, data, stacked, axisFormat) {
.select('g.nv-barsWrap')
.append('g')
.attr('class', 'bar-chart-label-group');
svg
.selectAll('g.nv-group')
.filter((d, i) => !stacked || i === countSeriesDisplayed - 1)
.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')) {
textEls.attr('y', yPos - 5);
} else {
textEls.attr('y', yPos + rectHeight + labelHeight);
}
const rectPosArr = [];
if (!stacked) {
svg

Check warning on line 89 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L87-L89

Added lines #L87 - L89 were not covered by tests
.selectAll('g.nv-group')
.selectAll('rect')
.each(function each(d) {
const rectObj = d3.select(this);
rectPosArr.push(getRectPosInfo(rectObj, d.y));

Check warning on line 94 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L93-L94

Added lines #L93 - L94 were not covered by tests
});
} else {
totalStackedValues.forEach((d, index) => {
svg.selectAll('g.nv-group').each(function each() {
d3.select(this)

Check warning on line 99 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L97-L99

Added lines #L97 - L99 were not covered by tests
.selectAll('rect')
.filter((f, i) => i === index)

Check warning on line 101 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L101

Added line #L101 was not covered by tests
.each(function each() {
const rectObj = d3.select(this);
const current = rectPosArr[index];
const rectInfo = getRectPosInfo(

Check warning on line 105 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L103-L105

Added lines #L103 - L105 were not covered by tests
rectObj,
totalStackedValues[index],
);
if (!current) {
rectPosArr[index] = rectInfo;
} else if (

Check warning on line 111 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L109-L111

Added lines #L109 - L111 were not covered by tests
// when totalStackedValue is positive, show the value text on the top of the bar, otherwise on the bottom
totalStackedValues[index] > 0
? rectInfo.yPos < current.yPos
: rectInfo.yPos > current.yPos
) {
rectPosArr[index] = rectInfo;

Check warning on line 117 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L117

Added line #L117 was not covered by tests
}
});
});
});
}
rectPosArr.forEach(function each(d) {
const textEls = groupLabels

Check warning on line 124 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L123-L124

Added lines #L123 - L124 were not covered by tests
.append('text')
.text(format(d.yValue))
.attr('transform', d.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', d.xPos + d.rectWidth / 2 - labelWidth / 2);
if (d.yValue >= 0) {
textEls.attr('y', d.yPos - 5);

Check warning on line 136 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L131-L136

Added lines #L131 - L136 were not covered by tests
} else {
textEls.attr('y', d.yPos + d.rectHeight + labelHeight);

Check warning on line 138 in superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js

View check run for this annotation

Codecov / codecov/patch

superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.js#L138

Added line #L138 was not covered by tests
}
});
}, ANIMATION_TIME);
}

Expand Down

0 comments on commit 749b760

Please sign in to comment.