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): ♻️ remove ErrorWhisker and Tooltip when error is null #1252

Merged
merged 3 commits into from
Nov 8, 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
- 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);
Loading