2022-12-28 23:43:22 +00:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
import { Loading } from 'react-basics';
|
2022-01-14 08:39:27 +00:00
|
|
|
import { colord } from 'colord';
|
2024-03-15 03:26:52 +00:00
|
|
|
import BarChart from 'components/charts/BarChart';
|
2023-05-25 17:26:00 +00:00
|
|
|
import { getDateArray } from 'lib/date';
|
2024-03-29 23:56:19 +00:00
|
|
|
import { useLocale, useDateRange, useWebsiteEvents } from 'components/hooks';
|
2024-03-15 03:26:52 +00:00
|
|
|
import { CHART_COLORS } from 'lib/constants';
|
|
|
|
|
import { renderDateLabels } from 'lib/charts';
|
2020-08-26 16:58:24 +00:00
|
|
|
|
2023-12-03 11:07:03 +00:00
|
|
|
export interface EventsChartProps {
|
|
|
|
|
websiteId: string;
|
|
|
|
|
className?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 23:56:19 +00:00
|
|
|
export function EventsChart({ websiteId, className }: EventsChartProps) {
|
|
|
|
|
const [{ startDate, endDate, unit }] = useDateRange(websiteId);
|
2023-05-25 17:26:00 +00:00
|
|
|
const { locale } = useLocale();
|
2024-03-29 23:56:19 +00:00
|
|
|
const { data, isLoading } = useWebsiteEvents(websiteId);
|
2020-10-15 05:09:00 +00:00
|
|
|
|
2024-03-15 03:26:52 +00:00
|
|
|
const chartData = useMemo(() => {
|
2020-08-27 20:46:05 +00:00
|
|
|
if (!data) return [];
|
|
|
|
|
|
2024-01-31 02:25:41 +00:00
|
|
|
const map = (data as any[]).reduce((obj, { x, t, y }) => {
|
2020-08-27 10:42:24 +00:00
|
|
|
if (!obj[x]) {
|
|
|
|
|
obj[x] = [];
|
2020-08-26 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-26 11:15:08 +00:00
|
|
|
obj[x].push({ x: t, y });
|
2020-08-26 16:58:24 +00:00
|
|
|
|
2020-08-27 10:42:24 +00:00
|
|
|
return obj;
|
|
|
|
|
}, {});
|
2020-08-26 16:58:24 +00:00
|
|
|
|
2020-08-27 10:42:24 +00:00
|
|
|
Object.keys(map).forEach(key => {
|
|
|
|
|
map[key] = getDateArray(map[key], startDate, endDate, unit);
|
|
|
|
|
});
|
2020-08-26 16:58:24 +00:00
|
|
|
|
2024-03-15 03:26:52 +00:00
|
|
|
return {
|
|
|
|
|
datasets: Object.keys(map).map((key, index) => {
|
|
|
|
|
const color = colord(CHART_COLORS[index % CHART_COLORS.length]);
|
|
|
|
|
return {
|
|
|
|
|
label: key,
|
|
|
|
|
data: map[key],
|
|
|
|
|
lineTension: 0,
|
|
|
|
|
backgroundColor: color.alpha(0.6).toRgbString(),
|
|
|
|
|
borderColor: color.alpha(0.7).toRgbString(),
|
|
|
|
|
borderWidth: 1,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
}, [data, startDate, endDate, unit]);
|
2020-08-26 16:58:24 +00:00
|
|
|
|
2022-12-28 23:43:22 +00:00
|
|
|
if (isLoading) {
|
2023-02-04 16:59:52 +00:00
|
|
|
return <Loading icon="dots" />;
|
2022-12-28 23:43:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-26 16:58:24 +00:00
|
|
|
return (
|
2020-08-28 01:44:20 +00:00
|
|
|
<BarChart
|
2020-10-10 03:37:24 +00:00
|
|
|
className={className}
|
2024-03-15 03:26:52 +00:00
|
|
|
data={chartData}
|
2020-08-28 01:44:20 +00:00
|
|
|
unit={unit}
|
2024-01-15 05:17:11 +00:00
|
|
|
stacked={true}
|
2023-05-25 17:26:00 +00:00
|
|
|
renderXLabel={renderDateLabels(unit, locale)}
|
2024-01-15 05:17:11 +00:00
|
|
|
isLoading={isLoading}
|
2020-08-28 01:44:20 +00:00
|
|
|
/>
|
2020-08-26 16:58:24 +00:00
|
|
|
);
|
|
|
|
|
}
|
2023-04-21 15:00:42 +00:00
|
|
|
|
|
|
|
|
export default EventsChart;
|