Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions src/components/Errors/NewErrorCard/OccurrenceChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "../../../../hooks/useFetchData";
import { useConfigSelector } from "../../../../store/config/useConfigSelector";
import { isNumber } from "../../../../typeGuards/isNumber";
import { measureTextWidth } from "../../../../utils/measureTextWidth";
import { HistogramIcon } from "../../../common/icons/30px/HistogramIcon";
import { PetalsIcon } from "../../../common/icons/32px/PetalsIcon";
import { actions } from "../../actions";
Expand All @@ -29,6 +30,7 @@ import {
} from "./types";

const MAX_BAR_WIDTH = 32;
const Y_AXIS_PADDING = 4;

export const OccurrenceChart = ({
errorId,
Expand Down Expand Up @@ -92,6 +94,29 @@ export const OccurrenceChart = ({
}
}, [data, errorId]);

const maxOccurrence = useMemo(
() =>
chartData?.dailyOccurrence.reduce((acc, cur) => {
return acc >= cur.value ? acc : cur.value;
}, 0) ?? 0,
[chartData]
);

const maxYAxisTickDigitsNumber = useMemo(
() => maxOccurrence.toString().length + 1,
[maxOccurrence]
);

const YAxisWidth = useMemo(
() =>
measureTextWidth("0".repeat(maxYAxisTickDigitsNumber), {
fontSize: theme.typographies.footNote.fontSize,
fontWeight: theme.typographies.footNote.fontWeight.regular,
fontFamily: theme.mainFont
}),
[maxYAxisTickDigitsNumber, theme]
);

return (
<s.HistogramContainer>
<s.HistogramHeader>
Expand Down Expand Up @@ -140,6 +165,7 @@ export const OccurrenceChart = ({
tickLine={false}
tick={YAxisTickLabelStyles}
allowDecimals={false}
width={YAxisWidth + Y_AXIS_PADDING}
/>
<Bar
isAnimationActive={false}
Expand Down
18 changes: 18 additions & 0 deletions src/utils/measureTextWidth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface FontOptions {
fontSize: number;
fontWeight: string | number;
fontFamily: string;
}
export const measureTextWidth = (text: string, font: FontOptions) => {
const { fontSize, fontWeight, fontFamily } = font;

const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
if (!context) {
return 0;
}

context.font = `${fontWeight} ${fontSize} ${fontFamily}`;

return context.measureText(text).width;
};
Loading