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

Error bars 1 #352

Merged
merged 9 commits into from
Feb 16, 2022
75 changes: 75 additions & 0 deletions app/charts/column/columns-simple.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,83 @@
import { memo } from "react";
import { useTheme } from "../../themes";
import { useChartState } from "../shared/use-chart-state";
import { ColumnsState } from "./columns-state";
import { Column } from "./rendering-utils";

export const VerticalWhisker = memo(
({
x,
y1,
y2,
width,
}: {
x: number;
y1: number;
y2: number;
width: number;
color?: string;
}) => {
return (
<>
<rect
x={x}
y={y1}
width={width}
height={2}
fill={"black"}
stroke="none"
/>
<rect
x={x + width / 2}
y={y2}
width={2}
height={y1 - y2}
fill={"black"}
stroke="none"
/>
<rect
x={x}
y={y2}
width={width}
height={2}
fill={"black"}
stroke="none"
/>
</>
);
}
);

export const ErrorWhiskers = () => {
const { preparedData, bounds, getX, xScale, getY, getYError, yScale } =
useChartState() as ColumnsState;
const { margins } = bounds;

if (!getYError) {
return null;
}

return (
<g transform={`translate(${margins.left} ${margins.top})`}>
{preparedData.map((d, i) => {
const x0 = xScale(getX(d)) as number;
const bandwidth = xScale.bandwidth();
const barwidth = bandwidth / 4;
const [y1, y2] = getYError(d);
return (
<VerticalWhisker
key={i}
x={x0 + bandwidth / 2 - barwidth / 2}
width={barwidth}
y1={yScale(y1)}
y2={yScale(y2)}
/>
);
})}
</g>
);
};

export const Columns = () => {
const { preparedData, bounds, getX, xScale, getY, yScale } =
useChartState() as ColumnsState;
Expand Down
17 changes: 17 additions & 0 deletions app/charts/column/columns-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface ColumnsState {
xEntireScale: ScaleTime<number, number>;
xScaleInteraction: ScaleBand<string>;
getY: (d: Observation) => number | null;
getYError: null | ((d: Observation) => [number, number]);
yScale: ScaleLinear<number, number>;
getSegment: (d: Observation) => string;
segments: string[];
Expand Down Expand Up @@ -102,7 +103,22 @@ const useColumnsState = ({
const getX = useStringVariable(fields.x.componentIri);
const getXAsDate = useTemporalVariable(fields.x.componentIri);
const getY = useOptionalNumericVariable(fields.y.componentIri);
const errorIri = useMemo(() => {
const yMeasure = measures.find((m) => m.iri === fields.y.componentIri);
return yMeasure?.related?.errorIri;
}, [fields.y.componentIri, measures]);
const getSegment = useSegment(fields.segment?.componentIri);
const getYError = errorIri
? (d: Observation) => {
const y = getY(d) as number;
const error =
d[errorIri] !== null ? parseFloat(d[errorIri] as string) : null;
return (error === null ? [y, y] : [y - error, y + error]) as [
number,
number
];
}
: null;
ptbrowne marked this conversation as resolved.
Show resolved Hide resolved

const sortingType = fields.x.sorting?.sortingType;
const sortingOrder = fields.x.sorting?.sortingOrder;
Expand Down Expand Up @@ -271,6 +287,7 @@ const useColumnsState = ({
timeUnit,
xScaleInteraction,
getY,
getYError,
yScale,
getSegment,
yAxisLabel,
Expand Down
Loading