2023-02-18 13:11:01 +03:00
|
|
|
import type { ChartData, ChartDataset, ChartOptions, InteractionItem } from 'chart.js';
|
2021-06-25 20:15:19 +03:00
|
|
|
import {
|
|
|
|
add,
|
|
|
|
differenceInDays,
|
|
|
|
differenceInHours,
|
|
|
|
differenceInMonths,
|
|
|
|
differenceInWeeks,
|
2023-02-18 13:11:01 +03:00
|
|
|
endOfISOWeek,
|
2021-06-25 20:15:19 +03:00
|
|
|
format,
|
2023-02-18 13:11:01 +03:00
|
|
|
parseISO,
|
2021-06-25 20:15:19 +03:00
|
|
|
startOfISOWeek,
|
|
|
|
} from 'date-fns';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { always, cond, countBy, reverse } from 'ramda';
|
|
|
|
import type { MutableRefObject } from 'react';
|
|
|
|
import { useMemo, useRef, useState } from 'react';
|
|
|
|
import { getElementAtEvent, Line } from 'react-chartjs-2';
|
|
|
|
import {
|
|
|
|
Card,
|
|
|
|
CardBody,
|
|
|
|
CardHeader,
|
|
|
|
DropdownItem,
|
|
|
|
DropdownMenu,
|
|
|
|
DropdownToggle,
|
|
|
|
UncontrolledDropdown,
|
|
|
|
} from 'reactstrap';
|
2023-07-24 21:14:59 +03:00
|
|
|
import { pointerOnHover, renderChartLabel } from '../../../src/utils/helpers/charts';
|
|
|
|
import { STANDARD_DATE_FORMAT } from '../../../src/utils/helpers/date';
|
|
|
|
import { useToggle } from '../../../src/utils/helpers/hooks';
|
|
|
|
import { prettify } from '../../../src/utils/helpers/numbers';
|
|
|
|
import { HIGHLIGHTED_COLOR, MAIN_COLOR } from '../../../src/utils/theme';
|
|
|
|
import { ToggleSwitch } from '../../../src/utils/ToggleSwitch';
|
|
|
|
import { rangeOf } from '../../../src/utils/utils';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { NormalizedVisit, Stats } from '../types';
|
2023-07-16 09:47:10 +03:00
|
|
|
import { fillTheGaps } from '../utils';
|
2022-12-26 00:44:43 +03:00
|
|
|
import './LineChartCard.scss';
|
2020-05-30 10:25:15 +03:00
|
|
|
|
2020-09-04 19:43:26 +03:00
|
|
|
interface LineChartCardProps {
|
|
|
|
title: string;
|
|
|
|
highlightedLabel?: string;
|
2020-09-06 11:22:21 +03:00
|
|
|
visits: NormalizedVisit[];
|
2020-09-05 09:49:18 +03:00
|
|
|
highlightedVisits: NormalizedVisit[];
|
2020-09-20 12:43:24 +03:00
|
|
|
setSelectedVisits?: (visits: NormalizedVisit[]) => void;
|
2020-09-04 19:43:26 +03:00
|
|
|
}
|
2020-05-30 10:25:15 +03:00
|
|
|
|
2020-09-04 19:43:26 +03:00
|
|
|
type Step = 'monthly' | 'weekly' | 'daily' | 'hourly';
|
|
|
|
|
|
|
|
const STEPS_MAP: Record<Step, string> = {
|
2020-05-30 18:39:08 +03:00
|
|
|
monthly: 'Month',
|
|
|
|
weekly: 'Week',
|
|
|
|
daily: 'Day',
|
|
|
|
hourly: 'Hour',
|
|
|
|
};
|
|
|
|
|
2021-06-25 20:33:18 +03:00
|
|
|
const STEP_TO_DURATION_MAP: Record<Step, (amount: number) => Duration> = {
|
|
|
|
hourly: (hours: number) => ({ hours }),
|
|
|
|
daily: (days: number) => ({ days }),
|
|
|
|
weekly: (weeks: number) => ({ weeks }),
|
|
|
|
monthly: (months: number) => ({ months }),
|
2021-06-25 20:15:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const STEP_TO_DIFF_FUNC_MAP: Record<Step, (dateLeft: Date, dateRight: Date) => number> = {
|
|
|
|
hourly: differenceInHours,
|
|
|
|
daily: differenceInDays,
|
|
|
|
weekly: differenceInWeeks,
|
|
|
|
monthly: differenceInMonths,
|
2020-05-30 18:39:08 +03:00
|
|
|
};
|
2020-05-30 10:57:21 +03:00
|
|
|
|
2021-06-25 20:15:19 +03:00
|
|
|
const STEP_TO_DATE_FORMAT: Record<Step, (date: Date) => string> = {
|
|
|
|
hourly: (date) => format(date, 'yyyy-MM-dd HH:00'),
|
2022-10-23 11:43:01 +03:00
|
|
|
daily: (date) => format(date, STANDARD_DATE_FORMAT),
|
2020-05-30 10:57:21 +03:00
|
|
|
weekly(date) {
|
2022-10-23 11:43:01 +03:00
|
|
|
const firstWeekDay = format(startOfISOWeek(date), STANDARD_DATE_FORMAT);
|
|
|
|
const lastWeekDay = format(endOfISOWeek(date), STANDARD_DATE_FORMAT);
|
2020-05-30 10:57:21 +03:00
|
|
|
|
|
|
|
return `${firstWeekDay} - ${lastWeekDay}`;
|
|
|
|
},
|
2021-06-25 20:15:19 +03:00
|
|
|
monthly: (date) => format(date, 'yyyy-MM'),
|
2020-05-30 10:25:15 +03:00
|
|
|
};
|
|
|
|
|
2020-09-04 19:43:26 +03:00
|
|
|
const determineInitialStep = (oldestVisitDate: string): Step => {
|
2021-06-25 20:15:19 +03:00
|
|
|
const now = new Date();
|
|
|
|
const oldestDate = parseISO(oldestVisitDate);
|
2020-09-04 19:43:26 +03:00
|
|
|
const matcher = cond<never, Step | undefined>([
|
2022-03-26 14:17:42 +03:00
|
|
|
[() => differenceInDays(now, oldestDate) <= 2, always<Step>('hourly')], // Less than 2 days
|
|
|
|
[() => differenceInMonths(now, oldestDate) <= 1, always<Step>('daily')], // Between 2 days and 1 month
|
|
|
|
[() => differenceInMonths(now, oldestDate) <= 6, always<Step>('weekly')], // Between 1 and 6 months
|
2020-05-31 21:16:15 +03:00
|
|
|
]);
|
2020-05-31 21:03:59 +03:00
|
|
|
|
2020-09-04 19:43:26 +03:00
|
|
|
return matcher() ?? 'monthly';
|
2020-05-31 21:03:59 +03:00
|
|
|
};
|
|
|
|
|
2021-03-28 17:27:31 +03:00
|
|
|
const groupVisitsByStep = (step: Step, visits: NormalizedVisit[]): Stats => countBy(
|
2021-06-25 20:15:19 +03:00
|
|
|
(visit) => STEP_TO_DATE_FORMAT[step](parseISO(visit.date)),
|
2021-03-28 17:27:31 +03:00
|
|
|
visits,
|
2020-09-05 09:49:18 +03:00
|
|
|
);
|
2020-05-30 10:25:15 +03:00
|
|
|
|
2021-02-28 14:56:56 +03:00
|
|
|
const visitsToDatasetGroups = (step: Step, visits: NormalizedVisit[]) =>
|
|
|
|
visits.reduce<Record<string, NormalizedVisit[]>>(
|
|
|
|
(acc, visit) => {
|
2021-06-25 20:15:19 +03:00
|
|
|
const key = STEP_TO_DATE_FORMAT[step](parseISO(visit.date));
|
2020-09-20 12:43:24 +03:00
|
|
|
|
2021-02-28 14:56:56 +03:00
|
|
|
acc[key] = acc[key] ?? [];
|
|
|
|
acc[key].push(visit);
|
2020-09-20 12:43:24 +03:00
|
|
|
|
2021-02-28 14:56:56 +03:00
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
2020-09-20 12:43:24 +03:00
|
|
|
|
2020-09-06 11:22:21 +03:00
|
|
|
const generateLabels = (step: Step, visits: NormalizedVisit[]): string[] => {
|
2021-06-25 20:15:19 +03:00
|
|
|
const diffFunc = STEP_TO_DIFF_FUNC_MAP[step];
|
2020-05-30 18:43:13 +03:00
|
|
|
const formatter = STEP_TO_DATE_FORMAT[step];
|
2021-06-25 20:15:19 +03:00
|
|
|
const newerDate = parseISO(visits[0].date);
|
|
|
|
const oldestDate = parseISO(visits[visits.length - 1].date);
|
|
|
|
const size = diffFunc(newerDate, oldestDate);
|
|
|
|
const duration = STEP_TO_DURATION_MAP[step];
|
2020-05-30 18:39:08 +03:00
|
|
|
|
|
|
|
return [
|
2020-05-30 18:43:13 +03:00
|
|
|
formatter(oldestDate),
|
2021-06-25 20:33:18 +03:00
|
|
|
...rangeOf(size, (num) => formatter(add(oldestDate, duration(num)))),
|
2020-05-30 18:39:08 +03:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2020-09-04 19:43:26 +03:00
|
|
|
const generateLabelsAndGroupedVisits = (
|
2020-09-06 11:22:21 +03:00
|
|
|
visits: NormalizedVisit[],
|
2020-09-04 19:43:26 +03:00
|
|
|
groupedVisitsWithGaps: Stats,
|
|
|
|
step: Step,
|
|
|
|
skipNoElements: boolean,
|
|
|
|
): [string[], number[]] => {
|
2020-05-30 18:39:08 +03:00
|
|
|
if (skipNoElements) {
|
2022-03-26 14:17:42 +03:00
|
|
|
return [Object.keys(groupedVisitsWithGaps), Object.values(groupedVisitsWithGaps)];
|
2020-05-30 18:39:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const labels = generateLabels(step, visits);
|
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
return [labels, fillTheGaps(groupedVisitsWithGaps, labels)];
|
2020-05-30 18:39:08 +03:00
|
|
|
};
|
|
|
|
|
2021-08-29 22:54:21 +03:00
|
|
|
const generateDataset = (data: number[], label: string, color: string): ChartDataset => ({
|
2020-05-30 10:25:15 +03:00
|
|
|
label,
|
2020-09-04 19:43:26 +03:00
|
|
|
data,
|
2020-05-30 10:25:15 +03:00
|
|
|
fill: false,
|
2021-08-29 22:54:21 +03:00
|
|
|
tension: 0.2,
|
2020-05-30 10:25:15 +03:00
|
|
|
borderColor: color,
|
|
|
|
backgroundColor: color,
|
|
|
|
});
|
|
|
|
|
2020-09-20 12:43:24 +03:00
|
|
|
let selectedLabel: string | null = null;
|
|
|
|
|
|
|
|
const chartElementAtEvent = (
|
2021-08-29 23:50:05 +03:00
|
|
|
labels: string[],
|
2020-09-20 12:43:24 +03:00
|
|
|
datasetsByPoint: Record<string, NormalizedVisit[]>,
|
2022-05-02 12:35:05 +03:00
|
|
|
[chart]: InteractionItem[],
|
2020-09-20 12:43:24 +03:00
|
|
|
setSelectedVisits?: (visits: NormalizedVisit[]) => void,
|
2022-05-02 12:35:05 +03:00
|
|
|
) => {
|
2020-09-20 12:43:24 +03:00
|
|
|
if (!setSelectedVisits || !chart) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-29 23:50:05 +03:00
|
|
|
const { index } = chart;
|
2020-09-20 12:43:24 +03:00
|
|
|
|
|
|
|
if (selectedLabel === labels[index]) {
|
|
|
|
setSelectedVisits([]);
|
|
|
|
selectedLabel = null;
|
|
|
|
} else {
|
2022-05-02 12:35:05 +03:00
|
|
|
setSelectedVisits(labels[index] && datasetsByPoint[labels[index]] ? datasetsByPoint[labels[index]] : []);
|
2020-09-20 12:43:24 +03:00
|
|
|
selectedLabel = labels[index] ?? null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-28 11:34:12 +03:00
|
|
|
export const LineChartCard = (
|
2020-09-20 12:43:24 +03:00
|
|
|
{ title, visits, highlightedVisits, highlightedLabel = 'Selected', setSelectedVisits }: LineChartCardProps,
|
|
|
|
) => {
|
2022-03-26 14:17:42 +03:00
|
|
|
const [step, setStep] = useState<Step>(
|
2020-08-22 09:06:41 +03:00
|
|
|
visits.length > 0 ? determineInitialStep(visits[visits.length - 1].date) : 'monthly',
|
2020-05-31 21:03:59 +03:00
|
|
|
);
|
2022-03-26 14:17:42 +03:00
|
|
|
const [skipNoVisits, toggleSkipNoVisits] = useToggle(true);
|
2022-05-02 12:35:05 +03:00
|
|
|
const refWithHighlightedVisits = useRef(null);
|
|
|
|
const refWithoutHighlightedVisits = useRef(null);
|
2020-05-30 18:39:08 +03:00
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
const datasetsByPoint = useMemo(() => visitsToDatasetGroups(step, visits), [step, visits]);
|
|
|
|
const groupedVisitsWithGaps = useMemo(() => groupVisitsByStep(step, reverse(visits)), [step, visits]);
|
|
|
|
const [labels, groupedVisits] = useMemo(
|
2020-05-31 09:55:52 +03:00
|
|
|
() => generateLabelsAndGroupedVisits(visits, groupedVisitsWithGaps, step, skipNoVisits),
|
2022-03-26 14:17:42 +03:00
|
|
|
[visits, step, skipNoVisits],
|
2020-05-30 18:39:08 +03:00
|
|
|
);
|
2020-05-30 10:25:15 +03:00
|
|
|
const groupedHighlighted = useMemo(
|
|
|
|
() => fillTheGaps(groupVisitsByStep(step, reverse(highlightedVisits)), labels),
|
2022-03-26 14:17:42 +03:00
|
|
|
[highlightedVisits, step, labels],
|
2020-05-30 10:25:15 +03:00
|
|
|
);
|
2021-09-18 13:07:05 +03:00
|
|
|
const generateChartDatasets = (): ChartDataset[] => {
|
|
|
|
const mainDataset = generateDataset(groupedVisits, 'Visits', MAIN_COLOR);
|
2020-05-30 10:25:15 +03:00
|
|
|
|
2021-09-18 13:07:05 +03:00
|
|
|
if (highlightedVisits.length === 0) {
|
2022-03-26 14:17:42 +03:00
|
|
|
return [mainDataset];
|
2021-09-18 13:07:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const highlightedDataset = generateDataset(groupedHighlighted, highlightedLabel, HIGHLIGHTED_COLOR);
|
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
return [mainDataset, highlightedDataset];
|
2020-05-30 10:25:15 +03:00
|
|
|
};
|
2021-09-18 13:07:05 +03:00
|
|
|
const generateChartData = (): ChartData => ({ labels, datasets: generateChartDatasets() });
|
|
|
|
|
2020-09-13 12:11:17 +03:00
|
|
|
const options: ChartOptions = {
|
2020-05-30 11:26:52 +03:00
|
|
|
maintainAspectRatio: false,
|
2021-08-29 22:54:21 +03:00
|
|
|
plugins: {
|
|
|
|
legend: { display: false },
|
|
|
|
tooltip: {
|
|
|
|
intersect: false,
|
|
|
|
axis: 'x',
|
|
|
|
callbacks: { label: renderChartLabel },
|
|
|
|
},
|
|
|
|
},
|
2020-05-30 10:25:15 +03:00
|
|
|
scales: {
|
2021-08-29 22:54:21 +03:00
|
|
|
y: {
|
|
|
|
beginAtZero: true,
|
|
|
|
ticks: {
|
|
|
|
precision: 0,
|
|
|
|
callback: prettify,
|
2020-05-30 18:39:08 +03:00
|
|
|
},
|
2021-08-29 22:54:21 +03:00
|
|
|
},
|
|
|
|
x: {
|
|
|
|
title: { display: true, text: STEPS_MAP[step] },
|
2020-09-13 12:11:17 +03:00
|
|
|
},
|
2020-05-30 10:25:15 +03:00
|
|
|
},
|
2021-09-18 13:07:05 +03:00
|
|
|
onHover: pointerOnHover,
|
2020-05-30 10:25:15 +03:00
|
|
|
};
|
2022-05-02 12:35:05 +03:00
|
|
|
const renderLineChart = (theRef: MutableRefObject<any>) => (
|
2021-09-18 13:07:05 +03:00
|
|
|
<Line
|
2022-05-02 12:35:05 +03:00
|
|
|
ref={theRef}
|
2022-03-07 19:39:03 +03:00
|
|
|
data={generateChartData() as any}
|
|
|
|
options={options as any}
|
2022-05-02 12:35:05 +03:00
|
|
|
onClick={(e) =>
|
|
|
|
chartElementAtEvent(labels, datasetsByPoint, getElementAtEvent(theRef.current, e), setSelectedVisits)}
|
2021-09-18 13:07:05 +03:00
|
|
|
/>
|
|
|
|
);
|
2020-05-30 10:25:15 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Card>
|
2022-05-28 13:54:33 +03:00
|
|
|
<CardHeader role="heading">
|
2020-05-30 10:57:21 +03:00
|
|
|
{title}
|
2022-03-05 15:26:28 +03:00
|
|
|
<div className="float-end">
|
2020-05-30 10:57:21 +03:00
|
|
|
<UncontrolledDropdown>
|
|
|
|
<DropdownToggle caret color="link" className="btn-sm p-0">
|
|
|
|
Group by
|
|
|
|
</DropdownToggle>
|
2022-03-11 18:37:41 +03:00
|
|
|
<DropdownMenu end>
|
2022-03-26 14:17:42 +03:00
|
|
|
{Object.entries(STEPS_MAP).map(([value, menuText]) => (
|
2020-09-04 19:43:26 +03:00
|
|
|
<DropdownItem key={value} active={step === value} onClick={() => setStep(value as Step)}>
|
2020-05-30 10:57:21 +03:00
|
|
|
{menuText}
|
|
|
|
</DropdownItem>
|
|
|
|
))}
|
|
|
|
</DropdownMenu>
|
|
|
|
</UncontrolledDropdown>
|
|
|
|
</div>
|
2022-03-05 15:26:28 +03:00
|
|
|
<div className="float-end me-2">
|
2020-07-14 17:05:00 +03:00
|
|
|
<ToggleSwitch checked={skipNoVisits} onChange={toggleSkipNoVisits}>
|
2020-05-30 18:39:08 +03:00
|
|
|
<small>Skip dates with no visits</small>
|
2020-07-14 17:05:00 +03:00
|
|
|
</ToggleSwitch>
|
2020-05-30 18:39:08 +03:00
|
|
|
</div>
|
2020-05-30 10:57:21 +03:00
|
|
|
</CardHeader>
|
2020-05-30 11:26:52 +03:00
|
|
|
<CardBody className="line-chart-card__body">
|
2021-09-18 13:07:05 +03:00
|
|
|
{/* It's VERY IMPORTANT to render two different components here, as one has 1 dataset and the other has 2 */}
|
|
|
|
{/* Using the same component causes a crash when switching from 1 to 2 datasets, and then back to 1 dataset */}
|
2022-05-02 12:35:05 +03:00
|
|
|
{highlightedVisits.length > 0 && renderLineChart(refWithHighlightedVisits)}
|
|
|
|
{highlightedVisits.length === 0 && renderLineChart(refWithoutHighlightedVisits)}
|
2020-05-30 10:25:15 +03:00
|
|
|
</CardBody>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|