Skip to content

Commit 74507ec

Browse files
committed
feat: Use getStackedY in area and stacked column charts
1 parent 8c54404 commit 74507ec

2 files changed

Lines changed: 72 additions & 41 deletions

File tree

app/charts/area/areas-state.tsx

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
CommonChartState,
4343
InteractiveXTimeRangeState,
4444
} from "@/charts/shared/chart-state";
45-
import { TooltipInfo } from "@/charts/shared/interaction/tooltip";
45+
import { TooltipInfo, TooltipValue } from "@/charts/shared/interaction/tooltip";
4646
import {
4747
getCenteredTooltipPlacement,
4848
MOBILE_TOOLTIP_PLACEMENT,
@@ -54,6 +54,7 @@ import {
5454
} from "@/charts/shared/show-values-utils";
5555
import {
5656
getStackedTooltipValueFormatter,
57+
getStackedY,
5758
getStackedYScale,
5859
} from "@/charts/shared/stacked-helpers";
5960
import { useChartFormatters } from "@/charts/shared/use-chart-formatters";
@@ -458,38 +459,51 @@ const useAreasState = (
458459
value: yValueFormatter(getY(datum), getIdentityY(datum)),
459460
color: colors(getSegment(datum)),
460461
},
461-
values: fields.segment
462-
? sortedTooltipValues.map((d) => ({
463-
label: getSegmentAbbreviationOrLabel(d),
464-
value: yValueFormatter(getY(d), getIdentityY(d)),
465-
color: colors(getSegment(d)),
466-
}))
467-
: undefined,
468-
};
462+
values: sortedTooltipValues.map((d) => {
463+
const yPos = getStackedY({
464+
observation: d,
465+
series,
466+
xKey,
467+
getX: getXAsString,
468+
yScale,
469+
fallbackY: yScale(getY(d) ?? 0),
470+
getSegment,
471+
});
472+
473+
return {
474+
label: getSegmentAbbreviationOrLabel(d),
475+
value: yValueFormatter(getY(d), getIdentityY(d)),
476+
yPos,
477+
color: colors(getSegment(d)),
478+
} satisfies TooltipValue;
479+
}),
480+
} satisfies TooltipInfo;
469481
},
470482
[
471-
yScale,
472-
colors,
473-
fields.segment,
474-
formatNumber,
475-
formatters,
476-
getSegment,
477-
getSegmentAbbreviationOrLabel,
478-
getX,
479483
getXAsString,
480-
getY,
481484
chartDataGroupedByX,
485+
getY,
482486
segments,
483-
timeFormatUnit,
484-
xDimension.timeUnit,
485-
xScale,
487+
getSegment,
488+
normalize,
486489
yMeasure.id,
487490
yMeasure.unit,
488-
normalize,
489-
getIdentityY,
490-
chartWidth,
491-
chartHeight,
491+
formatters,
492+
formatNumber,
493+
xScale,
494+
getX,
495+
yScale,
496+
fields.segment,
492497
isMobile,
498+
chartHeight,
499+
chartWidth,
500+
timeFormatUnit,
501+
xDimension.timeUnit,
502+
getSegmentAbbreviationOrLabel,
503+
getIdentityY,
504+
colors,
505+
series,
506+
xKey,
493507
]
494508
);
495509

app/charts/column/columns-stacked-state.tsx

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import {
4444
CommonChartState,
4545
InteractiveXTimeRangeState,
4646
} from "@/charts/shared/chart-state";
47-
import { TooltipInfo } from "@/charts/shared/interaction/tooltip";
47+
import { TooltipInfo, TooltipValue } from "@/charts/shared/interaction/tooltip";
4848
import {
4949
getCenteredTooltipPlacement,
5050
MOBILE_TOOLTIP_PLACEMENT,
@@ -56,6 +56,7 @@ import {
5656
} from "@/charts/shared/show-values-utils";
5757
import {
5858
getStackedTooltipValueFormatter,
59+
getStackedY,
5960
getStackedYScale,
6061
} from "@/charts/shared/stacked-helpers";
6162
import { useChartFormatters } from "@/charts/shared/use-chart-formatters";
@@ -223,7 +224,7 @@ const useColumnsStackedState = (
223224
allSegments: segments,
224225
imputationType: "zeros",
225226
});
226-
}, [getSegment, getY, chartDataGroupedByX, segments, xKey]);
227+
}, [chartDataGroupedByX, xKey, getY, getSegment, segments]);
227228

228229
const xFilter = chartConfig.cubes.find((d) => d.iri === xDimension.cubeIri)
229230
?.filters[xDimension.id];
@@ -500,35 +501,51 @@ const useColumnsStackedState = (
500501
value: yValueFormatter(getY(datum), getIdentityY(datum)),
501502
color: colors(getSegment(datum)),
502503
},
503-
values: sortedTooltipValues.map((d) => ({
504-
label: getSegmentAbbreviationOrLabel(d),
505-
value: yValueFormatter(getY(d), getIdentityY(d)),
506-
color: colors(getSegment(d)),
507-
})),
504+
values: sortedTooltipValues.map((d) => {
505+
const yPos = getStackedY({
506+
observation: d,
507+
series,
508+
xKey,
509+
getX,
510+
yScale,
511+
fallbackY: yScale(getY(d) ?? 0),
512+
getSegment,
513+
});
514+
515+
return {
516+
label: getSegmentAbbreviationOrLabel(d),
517+
value: yValueFormatter(getY(d), getIdentityY(d)),
518+
yPos,
519+
color: colors(getSegment(d)),
520+
} satisfies TooltipValue;
521+
}),
508522
};
509523
},
510524
[
511-
getX,
512525
xScale,
526+
getX,
513527
chartDataGroupedByX,
528+
getY,
514529
segments,
515530
getSegment,
531+
normalize,
516532
yMeasure.id,
517533
yMeasure.unit,
518534
formatters,
519535
formatNumber,
520-
getXAbbreviationOrLabel,
536+
isMobile,
537+
chartHeight,
538+
yScale,
539+
chartWidth,
521540
fields.segment,
541+
getXAbbreviationOrLabel,
542+
isEditingAnnotation,
543+
formatXAxisTick,
522544
getSegmentAbbreviationOrLabel,
523-
getY,
524545
getIdentityY,
525546
colors,
526-
chartWidth,
527-
chartHeight,
528-
isMobile,
529-
normalize,
530-
yScale,
531-
formatXAxisTick,
547+
series,
548+
xKey,
532549
]
533550
);
534551

0 commit comments

Comments
 (0)