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: Column chart negative values Y origin #1045

Merged
merged 1 commit into from
May 31, 2023
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ You can also check the [release page](https://github.com/visualize-admin/visuali

## Unreleased

Nothing yet.
- Fixes
- Bars with negative values are now correctly attached to 0 again.

## [3.19.18] - 2023-05-16

Expand Down
3 changes: 2 additions & 1 deletion app/charts/column/columns-simple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ export const Columns = () => {
const xScaled = xScale(x) as number;
const y = getY(d) ?? NaN;
const yScaled = yScale(y);
const yRender = yScale(Math.max(y, 0));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should not be done in the scale via clamp() ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure, as clamping limits the values to scale's domain, and in fact we can have negative values in the domain, which is the case for broken chart from #1044. There we just need to explicitly set its anchor to yScale(0) 👀

const height = Math.abs(yScaled - y0);
const color = getColor(y);

return {
key: x,
x: xScaled,
y: yScaled,
y: yRender,
width: bandwidth,
height,
color,
Expand Down
5 changes: 2 additions & 3 deletions app/charts/column/columns-stacked.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,20 @@ export const ColumnsStacked = () => {
x: xScale(x) as number,
y: yScale(segment[1]),
width: bandwidth,
height: y0 - yScale(segment[1] - segment[0]),
height: yScale(segment[0]) - yScale(segment[1]),
color,
};
}),
};
});
}, [bandwidth, colors, getX, series, xScale, y0, yScale]);
}, [bandwidth, colors, getX, series, xScale, yScale]);

React.useEffect(() => {
if (ref.current) {
select(ref.current)
.selectAll<SVGGElement, StackedRenderDatum>("g")
.data(renderData, (d) => d.key)
.join("g")
.attr("data-n", (d) => d.key)
.selectAll<SVGRectElement, RenderDatum>("rect")
.data(
(d) => d.data,
Expand Down