Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't relocate text in "Stacked" mode. #4177

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/chart/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
<body onresize="doResize()">
<div>
Stacked:
<input type="checkbox" value="stacked" onclick="doStacked(this.checked)" />
<input type="checkbox" value="stacked" checked onclick="doStacked(this.checked)" />
Bar:
<input type="checkbox" value="bar" onclick="doBar(this.checked)" />
Percentage:
<input type="checkbox" value="percentage" checked onclick="doPercentage(this.checked)" />
</div>
<div id="placeholder">
</div>
Expand Down Expand Up @@ -75,6 +77,14 @@
.lazyRender();
}
}

function doPercentage(percentage) {
if (app) {
app
.showValueAsPercent(percentage ? "domain" : undefined)
.lazyRender();
}
}
</script>
</body>

Expand Down
12 changes: 7 additions & 5 deletions packages/chart/src/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class Column extends XYAxis {
if (this.useClonedPalette()) {
this._palette = this._palette.cloneNotExists(this.paletteID() + "_" + this.id());
}
const formatPct = d3Format(context.showValueAsPercentFormat());

let dataLen = 10;
let offset = 0;
Expand Down Expand Up @@ -202,11 +203,11 @@ export class Column extends XYAxis {
switch (context.showValueAsPercent()) {
case "series":
const seriesSum = typeof dm.sum !== "undefined" ? dm.sum : seriesSums[d.idx];
valueText = d3Format(context.showValueAsPercentFormat())(valueText / seriesSum);
valueText = formatPct(valueText / seriesSum);
break;
case "domain":
const domainSum = typeof dm.sum !== "undefined" ? dm.sum : domainSums[d.idx - 1];
valueText = d3Format(context.showValueAsPercentFormat())(valueText / domainSum);
const domainSum = typeof dm.sum !== "undefined" ? dm.sum : domainSums[dataRowIdx];
valueText = formatPct(valueText / domainSum);
break;
case null:
default:
Expand Down Expand Up @@ -391,7 +392,7 @@ export class Column extends XYAxis {
pos.y = dataRect.y + (dataRect.height / 2);
}
} else { // Bar
noRoomInside = context.yAxisStacked() ? false : dataRect.width < textSize.width;
noRoomInside = dataRect.width < textSize.width;
isOutside = !context.valueCentered() || noRoomInside;

pos.y = dataRect.y + (dataRect.height / 2);
Expand Down Expand Up @@ -431,7 +432,8 @@ export class Column extends XYAxis {

// Prevent overlapping labels on stacked columns
const columns = context.columns();
const hideValue = isOutside && context.yAxisStacked() && columns.indexOf(d.column) !== columns.length - 1;
const hideValue = (context.yAxisStacked() && noRoomInside) ||
(isOutside && context.yAxisStacked() && columns.indexOf(d.column) !== columns.length - 1);
context.textLocal.get(this)
.pos(pos)
.anchor(valueAnchor)
Expand Down
6 changes: 3 additions & 3 deletions packages/chart/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class Test3 extends Column {
.columns(["", "Dismissed", "Pending", "Assigned"])
.data([
["Joe Cocker", 10, 1, 10],
["Steve Tyler", 18, 1, 12],
["Einar Solberg", 19, 1, 21]
["Steve Tyler", 20, 2, 11],
["Einar Solberg", 30, 3, 12]
])
// .paletteID("siuReportPalette")
.showValue(true)
Expand All @@ -30,7 +30,7 @@ export class Test3 extends Column {
.valueCentered(true)
.valueAnchor("middle")
// .xAxisSeriesPaddingInner(3)
// .yAxisStacked(true)
.yAxisStacked(true)
.yAxisGuideLines(true)
.yAxisHidden(true)
.xAxisHidden(false)
Expand Down
Loading