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: Fixed tooltip for Mobile Area Charts #1817

Merged
merged 11 commits into from
Nov 11, 2024
21 changes: 15 additions & 6 deletions app/charts/area/areas-state.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useMediaQuery, useTheme } from "@mui/material";
import { extent, group, rollup, sum } from "d3-array";
import {
ScaleLinear,
Expand Down Expand Up @@ -352,6 +353,9 @@ const useAreasState = (
xScaleTimeRange.range([0, chartWidth]);
yScale.range([chartHeight, 0]);

const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
bprusinowski marked this conversation as resolved.
Show resolved Hide resolved

/** Tooltip */
const getAnnotationInfo = useCallback(
(datum: Observation): TooltipInfo => {
Expand All @@ -372,18 +376,21 @@ const useAreasState = (
formatNumber,
});
const xAnchor = xScale(getX(datum));
const yAnchor = normalize
const yNormalized = normalize
bprusinowski marked this conversation as resolved.
Show resolved Hide resolved
? yScale.range()[0] * 0.5
: yScale(sum(yValues) * (fields.segment ? 0.5 : 1));
const yAnchor = isMobile ? chartHeight : yNormalized;

return {
xAnchor,
yAnchor,
placement: getCenteredTooltipPlacement({
chartWidth,
xAnchor,
topAnchor: !fields.segment,
}),
placement: isMobile
? { x: "center", y: "bottom" }
: getCenteredTooltipPlacement({
chartWidth,
xAnchor,
topAnchor: !fields.segment,
}),
xValue: timeFormatUnit(getX(datum), xDimension.timeUnit),
datum: {
label: fields.segment && getSegmentAbbreviationOrLabel(datum),
Expand Down Expand Up @@ -420,6 +427,8 @@ const useAreasState = (
normalize,
getIdentityY,
chartWidth,
chartHeight,
isMobile,
]
);

Expand Down
26 changes: 16 additions & 10 deletions app/charts/shared/interaction/ruler.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Theme } from "@mui/material";
import { Box, Theme, useMediaQuery, useTheme } from "@mui/material";
import { makeStyles } from "@mui/styles";

import { ComboLineColumnState } from "@/charts/combo/combo-line-column-state";
Expand Down Expand Up @@ -63,6 +63,7 @@ type RulerContentProps = {
xAnchor: number;
datum: TooltipValue;
placement: TooltipPlacement;
showXValue?: boolean;
};

const useStyles = makeStyles<Theme, { rotate: boolean }>((theme: Theme) => ({
Expand Down Expand Up @@ -94,6 +95,9 @@ export const RulerContent = (props: RulerContentProps) => {
const { rotate, xValue, chartHeight, margins, xAnchor } = props;
const classes = useStyles({ rotate });

const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));

return (
<>
<Box
Expand All @@ -104,15 +108,17 @@ export const RulerContent = (props: RulerContentProps) => {
top: margins.top,
}}
/>
<Box
className={classes.right}
style={{
left: xAnchor + margins.left,
top: chartHeight + margins.top + 6,
}}
>
{xValue}
</Box>
{!isMobile && (
<Box
className={classes.right}
style={{
left: xAnchor + margins.left,
top: chartHeight + margins.top + 6,
}}
>
{xValue}
</Box>
)}
</>
);
};
Loading