Skip to content

Commit

Permalink
fix(column): 🐛 remove ErrorWhisker and Tooltip when error is null (#1252
Browse files Browse the repository at this point in the history
)

* refactor(column): ♻️ remove ErrorWhisker and Tooltip when error is null
* refactor(column): ♻️ apply same logic ad column to columns-grouped
* docs: 📝 update CHANGELOG
  • Loading branch information
lloydrichards authored Nov 8, 2023
1 parent 35dd61f commit 8fa45c8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
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
- remove ErrorWhisker and Tooltip when error is null in Column charts

# [3.24.0] - 2023-11-08

Expand Down
2 changes: 1 addition & 1 deletion app/charts/column/columns-grouped-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ const useColumnsGroupedState = (
});

const getError = (d: Observation) => {
if (!showYStandardError || !getYError) {
if (!showYStandardError || !getYError || getYError(d) == null) {
return;
}

Expand Down
35 changes: 20 additions & 15 deletions app/charts/column/columns-grouped.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GroupedColumnsState } from "@/charts/column/columns-grouped-state";
import {
RenderColumnDatum,
RenderWhiskerDatum,
filterWithoutErrors,
renderColumns,
renderWhiskers,
} from "@/charts/column/rendering-utils";
Expand All @@ -17,6 +18,7 @@ export const ErrorWhiskers = () => {
xScale,
xScaleIn,
getYErrorRange,
getYError,
yScale,
getSegment,
grouped,
Expand All @@ -31,25 +33,28 @@ export const ErrorWhiskers = () => {
return [];
}

return grouped.flatMap(([segment, observations]) =>
observations.map((d) => {
const x0 = xScaleIn(getSegment(d)) as number;
const bandwidth = xScaleIn.bandwidth();
const barwidth = Math.min(bandwidth, 15);
const [y1, y2] = getYErrorRange(d);
return grouped
.filter((d) => d[1].some(filterWithoutErrors(getYError)))
.flatMap(([segment, observations]) =>
observations.map((d) => {
const x0 = xScaleIn(getSegment(d)) as number;
const bandwidth = xScaleIn.bandwidth();
const barwidth = Math.min(bandwidth, 15);
const [y1, y2] = getYErrorRange(d);

return {
key: `${segment}-${getSegment(d)}`,
x: (xScale(segment) as number) + x0 + bandwidth / 2 - barwidth / 2,
y1: yScale(y1),
y2: yScale(y2),
width: barwidth,
};
})
);
return {
key: `${segment}-${getSegment(d)}`,
x: (xScale(segment) as number) + x0 + bandwidth / 2 - barwidth / 2,
y1: yScale(y1),
y2: yScale(y2),
width: barwidth,
};
})
);
}, [
getSegment,
getYErrorRange,
getYError,
grouped,
showYStandardError,
xScale,
Expand Down
2 changes: 1 addition & 1 deletion app/charts/column/columns-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ const useColumnsState = (
);

const getError = (d: Observation) => {
if (!showYStandardError || !getYError) {
if (!showYStandardError || !getYError || getYError(d) === null) {
return;
}

Expand Down
14 changes: 12 additions & 2 deletions app/charts/column/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from "react";

import { ColumnsState } from "@/charts/column/columns-state";
import {
filterWithoutErrors,
RenderColumnDatum,
renderColumns,
RenderWhiskerDatum,
Expand All @@ -16,6 +17,7 @@ import { useTheme } from "@/themes";
export const ErrorWhiskers = () => {
const {
getX,
getYError,
getYErrorRange,
chartData,
yScale,
Expand All @@ -32,7 +34,7 @@ export const ErrorWhiskers = () => {
return [];
}

return chartData.map((d, i) => {
return chartData.filter(filterWithoutErrors(getYError)).map((d, i) => {
const x0 = xScale(getX(d)) as number;
const bandwidth = xScale.bandwidth();
const barwidth = Math.min(bandwidth, 15);
Expand All @@ -46,7 +48,15 @@ export const ErrorWhiskers = () => {
width: barwidth,
};
});
}, [chartData, getX, getYErrorRange, showYStandardError, xScale, yScale]);
}, [
chartData,
getX,
getYError,
getYErrorRange,
showYStandardError,
xScale,
yScale,
]);

React.useEffect(() => {
if (ref.current) {
Expand Down
6 changes: 6 additions & 0 deletions app/charts/column/rendering-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
RenderOptions,
maybeTransition,
} from "@/charts/shared/rendering-utils";
import { Observation, ObservationValue } from "@/domain/data";

export type RenderColumnDatum = {
key: string;
Expand Down Expand Up @@ -158,3 +159,8 @@ export const renderWhiskers = (
})
);
};

export const filterWithoutErrors =
(getError: ((d: Observation) => ObservationValue) | null) =>
(d: Observation): boolean =>
!!getError?.(d);

1 comment on commit 8fa45c8

@vercel
Copy link

@vercel vercel bot commented on 8fa45c8 Nov 8, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

visualization-tool – ./

visualization-tool-alpha.vercel.app
visualization-tool-ixt1.vercel.app
visualization-tool-git-main-ixt1.vercel.app

Please sign in to comment.