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

Bar chart #1920

Merged
merged 56 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
1bfa2e2
chore 🧹: add .vercel to gitignore
squiles Nov 26, 2024
b1ea69a
feat ⚡️: replicate column chart as bar chart
squiles Nov 26, 2024
c85ea40
feat ⚡️: .
squiles Nov 29, 2024
fffca2f
feat ⚡️: remove rotation from left axis
squiles Nov 29, 2024
4d791db
feat ⚡️: adjust left margin
squiles Nov 29, 2024
db39b8d
fix 🐛: adjust interaction scale
squiles Nov 29, 2024
e63474e
fix 🐛: x scale on grouped bar charts
squiles Nov 29, 2024
d437b9c
fix 🐛: adjust grouped/stacked chart width and height
squiles Nov 29, 2024
4f4165c
refactor ♻️: addressed some of the feedback
squiles Dec 2, 2024
b670ce4
feat ⚡️: show stacked bar charts
squiles Dec 2, 2024
5852281
refactor ♻️: var adjustment
squiles Dec 2, 2024
4ba3b89
feat ⚡️: change id name
squiles Dec 2, 2024
d4209bb
docs 📑: corrected var name
squiles Dec 2, 2024
3757b40
feat ⚡️: adjusted xScale
squiles Dec 2, 2024
ac817c7
Merge branch 'main' into feat/bar-chart
squiles Dec 2, 2024
51f4602
feat ⚡️: column, bar, line, area and scaterplot added to chartConfigs…
squiles Dec 3, 2024
5300dda
fix 🐛: bad merge conflict resolution
squiles Dec 3, 2024
535f4e6
fix 🐛: tests
squiles Dec 3, 2024
51114bd
fix 🐛: PR reviews
squiles Dec 3, 2024
ebf6dcf
feat ⚡️: add map and pie configs
squiles Dec 3, 2024
4e6bf4f
fix: Bars enter animation
bprusinowski Dec 3, 2024
dda2553
fix 🐛: bar <-> pie adjuster
squiles Dec 3, 2024
65ed293
fix: Bar -> pie adjuster (pie doesn't have x field, segment is carrie…
bprusinowski Dec 3, 2024
0702e19
fix: Bar -> combo chart adjusters
bprusinowski Dec 3, 2024
1321900
fix 🐛: y label cutoff
squiles Dec 3, 2024
eadab83
refactor ♻️: ditch tooltip inverted
squiles Dec 5, 2024
893470d
refactor ♻️: got rid of getWideDataInverted
squiles Dec 5, 2024
8cf0207
refactor ♻️: got rid of unused code
squiles Dec 5, 2024
adc84a8
refactor ♻️: got rid of normalizeDataInverted
squiles Dec 5, 2024
b40eb69
refactor ♻️: got rid of getStackedTooltipValueFormatterInverted
squiles Dec 5, 2024
98d0981
refactor ♻️: got rid of useBarChartData
squiles Dec 9, 2024
f70052e
feat ⚡️: adjust margins and scroll on bar charts
squiles Dec 9, 2024
987131f
feat ⚡️: align left margin when axes are flipped
squiles Dec 9, 2024
c899526
feat ⚡️: add missing types
squiles Dec 10, 2024
befb5f5
fix 🐛: animate the correct axis
squiles Dec 10, 2024
ba28224
fix 🐛: remove unwanted animation
squiles Dec 10, 2024
468ff64
fix 🐛: label change
squiles Dec 10, 2024
1b90124
feat ⚡️: add i18n to bar sorting
squiles Dec 10, 2024
efc90bd
refactor ♻️: extracted MIN_BAR_HEIGHT into constants file
squiles Dec 10, 2024
cb28a3a
feat ⚡️: adjust stacked bars height
squiles Dec 10, 2024
90f3132
feat ⚡️: adjust bottom margin
squiles Dec 10, 2024
5d2b5fb
feat ⚡️: adjust horizontal whiskers
squiles Dec 10, 2024
f291994
feat ⚡️: adjust xScale on grouped bar charts
squiles Dec 10, 2024
f3ae19c
feat ⚡️: adjust margins on grouped bar chart
squiles Dec 10, 2024
43ec60b
feat ⚡️: adjust bottom axis according to design
squiles Dec 10, 2024
b479cd1
fix 🐛: animation on bar chart coming from pie
squiles Dec 11, 2024
49f7756
fix 🐛: remove middle circle from horizontal bar whiskers
squiles Dec 11, 2024
52cd631
refactor ♻️: rename vars
squiles Dec 11, 2024
19f50a0
fix 🐛: adjust error whiskers position
squiles Dec 11, 2024
f938a52
fix 🐛: var import
squiles Dec 11, 2024
7695a01
fix 🐛: min x value came from the data instead of 0
squiles Dec 11, 2024
7cdff4f
feat ⚡️: add missing bar fields on 18n
squiles Dec 11, 2024
d08a9ba
Merge branch 'main' into feat/bar-chart
squiles Dec 11, 2024
9c902df
feat ⚡️: remove bar instead of hiding it
squiles Dec 11, 2024
a3ef3e3
fix 🐛: bars not adjusting when width changed
squiles Dec 11, 2024
e7e620a
fix 🐛: don't reverse yScale
squiles Dec 11, 2024
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ playwright-report

app/public/storybook

certificates
certificates
.vercel
2 changes: 1 addition & 1 deletion app/charts/area/areas-state-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const useAreasStateData = (
const data = useChartData(sortedPlottableData, {
chartConfig,
timeRangeDimensionId: xDimension.id,
getXAsDate: getX,
getAxisValueAsDate: getX,
getSegmentAbbreviationOrLabel,
getTimeRangeDate,
});
Expand Down
16 changes: 8 additions & 8 deletions app/charts/area/areas-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ const useAreasState = (
if (normalize) {
return group(
normalizeData(chartData, {
yKey: yMeasure.id,
getY,
key: yMeasure.id,
getAxisValue: getY,
getTotalGroupValue: (d) => {
return sumsByX[getXAsString(d)];
},
Expand All @@ -205,9 +205,9 @@ const useAreasState = (

const chartWideData = useMemo(() => {
return getWideData({
dataGroupedByX: chartDataGroupedByX,
xKey,
getY,
dataGrouped: chartDataGroupedByX,
key: xKey,
getAxisValue: getY,
getSegment,
allSegments: segments,
imputationType: fields.y.imputationType,
Expand Down Expand Up @@ -372,8 +372,8 @@ const useAreasState = (
});
const yValueFormatter = getStackedTooltipValueFormatter({
normalize,
yMeasureId: yMeasure.id,
yMeasureUnit: yMeasure.unit,
measureId: yMeasure.id,
measureUnit: yMeasure.unit,
formatters,
formatNumber,
});
Expand All @@ -394,7 +394,7 @@ const useAreasState = (
xAnchor,
yAnchor,
placement,
xValue: timeFormatUnit(getX(datum), xDimension.timeUnit),
value: timeFormatUnit(getX(datum), xDimension.timeUnit),
datum: {
label: fields.segment && getSegmentAbbreviationOrLabel(datum),
value: yValueFormatter(getY(datum), getIdentityY(datum)),
Expand Down
157 changes: 157 additions & 0 deletions app/charts/bar/bars-grouped-state-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { ascending, rollup, sum } from "d3-array";
import orderBy from "lodash/orderBy";
import { useCallback, useMemo } from "react";

import { usePlottableData } from "@/charts/shared/chart-helpers";
import {
BandYVariables,
BaseVariables,
ChartStateData,
InteractiveFiltersVariables,
NumericalXErrorVariables,
NumericalXVariables,
RenderingVariables,
SegmentVariables,
SortingVariables,
useBandYVariables,
useBaseVariables,
useChartData,
useInteractiveFiltersVariables,
useNumericalXErrorVariables,
useNumericalXVariables,
useSegmentVariables,
} from "@/charts/shared/chart-state";
import { useRenderingKeyVariable } from "@/charts/shared/rendering-utils";
import { BarConfig, useChartConfigFilters } from "@/configurator";
import { Observation, isTemporalEntityDimension } from "@/domain/data";
import { sortByIndex } from "@/utils/array";

import { ChartProps } from "../shared/ChartProps";

export type BarsGroupedStateVariables = BaseVariables &
SortingVariables &
BandYVariables &
NumericalXVariables &
NumericalXErrorVariables &
bprusinowski marked this conversation as resolved.
Show resolved Hide resolved
SegmentVariables &
RenderingVariables &
InteractiveFiltersVariables;

export const useBarsGroupedStateVariables = (
props: ChartProps<BarConfig>
): BarsGroupedStateVariables => {
const {
chartConfig,
observations,
dimensions,
dimensionsById,
measures,
measuresById,
} = props;
const { fields, interactiveFiltersConfig } = chartConfig;
const { x, y, segment, animation } = fields;
const yDimension = dimensionsById[y.componentId];
const filters = useChartConfigFilters(chartConfig);

const baseVariables = useBaseVariables(chartConfig);
const numericalXVariables = useNumericalXVariables("bar", x, {
measuresById,
});
const bandYVariables = useBandYVariables(y, {
dimensionsById,
observations,
});
const numericalXErrorVariables = useNumericalXErrorVariables(x, {
getValue: numericalXVariables.getX,
dimensions,
measures,
});
const segmentVariables = useSegmentVariables(segment, {
dimensionsById,
observations,
});
const interactiveFiltersVariables = useInteractiveFiltersVariables(
interactiveFiltersConfig,
{ dimensionsById }
);

const { getY, getYAsDate } = bandYVariables;
const { getX } = numericalXVariables;
const sortData: BarsGroupedStateVariables["sortData"] = useCallback(
(data) => {
const { sortingOrder, sortingType } = y.sorting ?? {};
const yGetter = isTemporalEntityDimension(yDimension)
? (d: Observation) => getYAsDate(d).getTime().toString()
: getY;
const order = [
...rollup(
data,
(v) => sum(v, (d) => getX(d)),
(d) => yGetter(d)
),
]
.sort((a, b) => ascending(a[1], b[1]))
.map((d) => d[0]);

if (sortingType === "byDimensionLabel") {
return orderBy(data, yGetter, sortingOrder);
} else if (sortingType === "byMeasure") {
return sortByIndex({ data, order, getCategory: yGetter, sortingOrder });
} else {
return orderBy(data, yGetter, "asc");
}
},
[getX, getYAsDate, getY, y.sorting, yDimension]
);

const getRenderingKey = useRenderingKeyVariable(
dimensions,
filters,
interactiveFiltersConfig,
animation
);

return {
...baseVariables,
sortData,
...bandYVariables,
...numericalXVariables,
...numericalXErrorVariables,
...segmentVariables,
...interactiveFiltersVariables,
getRenderingKey,
};
};

export const useBarsGroupedStateData = (
chartProps: ChartProps<BarConfig>,
variables: BarsGroupedStateVariables
): ChartStateData => {
const { chartConfig, observations } = chartProps;
const {
sortData,
yDimension,
getYAsDate,
getX,
getSegmentAbbreviationOrLabel,
getTimeRangeDate,
} = variables;
const plottableData = usePlottableData(observations, {
getX,
});
const sortedPlottableData = useMemo(() => {
return sortData(plottableData);
}, [sortData, plottableData]);
const data = useChartData(sortedPlottableData, {
chartConfig,
timeRangeDimensionId: yDimension.id,
getAxisValueAsDate: getYAsDate,
getSegmentAbbreviationOrLabel,
getTimeRangeDate,
});

return {
...data,
allData: sortedPlottableData,
};
};
Loading
Loading