Skip to content

Commit

Permalink
feat: Show error in tooltip as +- value instead of showing the range
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Feb 16, 2022
1 parent 4c5bf32 commit 8bc77b9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 71 deletions.
50 changes: 25 additions & 25 deletions app/charts/column/columns-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import { sortBy } from "lodash";
import { ReactNode, useMemo } from "react";
import { ColumnFields, SortingOrder, SortingType } from "../../configurator";
import {
formatError,
formatNumberWithUnit,
getPalette,
mkNumber,
useError,
useErrorMeasure,
useErrorRange,
useErrorVariable,
useFormatNumber,
useTimeFormatUnit,
} from "../../configurator/components/ui-helpers";
Expand Down Expand Up @@ -71,20 +72,23 @@ export interface ColumnsState {
getAnnotationInfo: (d: Observation) => TooltipInfo;
}

const useColumnsState = ({
data,
fields,
measures,
dimensions,
interactiveFiltersConfig,
aspectRatio,
}: Pick<
ChartProps,
"data" | "measures" | "dimensions" | "interactiveFiltersConfig"
> & {
fields: ColumnFields;
aspectRatio: number;
}): ColumnsState => {
const useColumnsState = (
chartProps: Pick<
ChartProps,
"data" | "measures" | "dimensions" | "interactiveFiltersConfig"
> & {
fields: ColumnFields;
aspectRatio: number;
}
): ColumnsState => {
const {
data,
fields,
measures,
dimensions,
interactiveFiltersConfig,
aspectRatio,
} = chartProps;
const width = useWidth();
const formatNumber = useFormatNumber();
const timeFormatUnit = useTimeFormatUnit();
Expand All @@ -106,12 +110,9 @@ const useColumnsState = ({
const getX = useStringVariable(fields.x.componentIri);
const getXAsDate = useTemporalVariable(fields.x.componentIri);
const getY = useOptionalNumericVariable(fields.y.componentIri);
const getYErrorRange = useError(
measures,
dimensions,
getY,
fields.y.componentIri
);
const errorMeasure = useErrorMeasure(chartProps, fields.y.componentIri);
const getYErrorRange = useErrorRange(errorMeasure, getY);
const getYError = useErrorVariable(errorMeasure);
const getSegment = useSegment(fields.segment?.componentIri);

const sortingType = fields.x.sorting?.sortingType;
Expand Down Expand Up @@ -161,7 +162,6 @@ const useColumnsState = ({
) ?? 0,
0
);
const entireMaxValue = max(sortedData, getY) as number;

const yScale = scaleLinear()
.domain([mkNumber(minValue), mkNumber(maxValue)])
Expand Down Expand Up @@ -273,8 +273,8 @@ const useColumnsState = ({
datum: {
label: fields.segment?.componentIri && getSegment(datum),
value: `${yValueFormatter(getY(datum))}`,
error: getYErrorRange
? ` ${formatError(getYErrorRange(datum), yValueFormatter)}`
error: getYError
? `${getYError(datum)}${errorMeasure?.unit ?? ""}`
: undefined,
color: colors(getSegment(datum)) as string,
},
Expand Down
43 changes: 5 additions & 38 deletions app/charts/shared/interaction/tooltip-content.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Trans } from "@lingui/macro";
import { Box, Text } from "theme-ui";
import { LegendItem } from "../legend-color";
import { TooltipValue } from "./tooltip";
Expand Down Expand Up @@ -27,43 +26,11 @@ export const TooltipSingle = ({
{segment}
</Text>
)}
{yError && yValue ? (
<>
<table width="100%">
<tr>
<td>
<Text as="div" variant="meta" color="hint">
<Trans id="tooltip.average">Average:</Trans>
</Text>
</td>
<td align="right">
<Text as="div" variant="meta">
{yValue}
</Text>
</td>
</tr>
<tr>
<td>
<Text as="div" variant="meta" color="hint">
<Trans id="tooltip.error">Error:</Trans>
</Text>
</td>
<td align="right">
<Text as="div" variant="meta">
{yError}
</Text>
</td>
</tr>
</table>
</>
) : (
<>
{yValue && (
<Text as="div" variant="meta">
{yValue}
</Text>
)}
</>
{yValue && (
<Text as="div" variant="meta">
{yValue}
{yError ? <> ± {yError}</> : null}
</Text>
)}
</Box>
);
Expand Down
30 changes: 22 additions & 8 deletions app/configurator/components/ui-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,18 +310,32 @@ export const formatNumberWithUnit = (
return `${formatter(nb)}${unit ? ` ${unit}` : ""}`;
};

export const useError = (
measures: ChartProps["measures"],
dimensions: ChartProps["dimensions"],
valueGetter: (d: Observation) => number | null,
valueIri: string
) => {
export const useErrorMeasure = (chartState: ChartProps, valueIri: string) => {
const { measures, dimensions } = chartState;
return useMemo(() => {
const errorMeasure = [...measures, ...dimensions].find((m) => {
return [...measures, ...dimensions].find((m) => {
return m.related?.some(
(r) => r.type === "StandardError" && r.iri === valueIri
);
});
}, [dimensions, measures, valueIri]);
};

export const useErrorVariable = (errorMeasure?: DimensionMetaDataFragment) => {
return useMemo(() => {
return errorMeasure
? (d: Observation) => {
return d[errorMeasure.iri];
}
: null;
}, [errorMeasure]);
};

export const useErrorRange = (
errorMeasure: DimensionMetaDataFragment | undefined,
valueGetter: (d: Observation) => number | null
) => {
return useMemo(() => {
return errorMeasure
? (d: Observation) => {
const v = valueGetter(d) as number;
Expand All @@ -337,7 +351,7 @@ export const useError = (
];
}
: null;
}, [measures, dimensions, valueIri, valueGetter]);
}, [errorMeasure, valueGetter]);
};

export const useFormatNumber = () => {
Expand Down

0 comments on commit 8bc77b9

Please sign in to comment.