2020-08-31 00:29:31 +02:00
|
|
|
import React, { useMemo } from 'react';
|
2022-01-14 09:39:27 +01:00
|
|
|
import { colord } from 'colord';
|
2020-08-27 12:42:24 +02:00
|
|
|
import BarChart from './BarChart';
|
2020-09-19 19:35:05 +02:00
|
|
|
import { getDateArray, getDateLength } from 'lib/date';
|
2020-08-31 00:29:31 +02:00
|
|
|
import useFetch from 'hooks/useFetch';
|
2020-09-18 07:52:20 +02:00
|
|
|
import useDateRange from 'hooks/useDateRange';
|
2020-09-19 19:35:05 +02:00
|
|
|
import useTimezone from 'hooks/useTimezone';
|
2020-10-08 03:24:53 +02:00
|
|
|
import usePageQuery from 'hooks/usePageQuery';
|
2022-05-06 04:04:28 +02:00
|
|
|
import { EVENT_COLORS } from 'lib/constants';
|
2020-08-26 18:58:24 +02:00
|
|
|
|
2020-10-10 05:37:24 +02:00
|
|
|
export default function EventsChart({ websiteId, className, token }) {
|
2022-03-05 18:42:32 +01:00
|
|
|
const [{ startDate, endDate, unit, modified }] = useDateRange(websiteId);
|
2020-09-19 19:35:05 +02:00
|
|
|
const [timezone] = useTimezone();
|
2022-03-18 04:56:43 +01:00
|
|
|
const {
|
2022-07-30 07:30:09 +02:00
|
|
|
query: { url, eventName },
|
2022-03-18 04:56:43 +01:00
|
|
|
} = usePageQuery();
|
2020-09-19 19:35:05 +02:00
|
|
|
|
2020-10-15 07:09:00 +02:00
|
|
|
const { data, loading } = useFetch(
|
2022-10-04 02:17:53 +02:00
|
|
|
`/websites/${websiteId}/events`,
|
2020-08-31 23:11:30 +02:00
|
|
|
{
|
2020-10-09 21:39:03 +02:00
|
|
|
params: {
|
|
|
|
start_at: +startDate,
|
|
|
|
end_at: +endDate,
|
|
|
|
unit,
|
|
|
|
tz: timezone,
|
2022-03-18 04:56:43 +01:00
|
|
|
url,
|
2022-07-30 07:30:09 +02:00
|
|
|
event_name: eventName,
|
2020-10-09 21:39:03 +02:00
|
|
|
token,
|
|
|
|
},
|
2020-08-31 23:11:30 +02:00
|
|
|
},
|
2022-07-30 07:30:09 +02:00
|
|
|
[modified, eventName],
|
2020-08-31 23:11:30 +02:00
|
|
|
);
|
2020-10-15 07:09:00 +02:00
|
|
|
|
2020-08-27 22:46:05 +02:00
|
|
|
const datasets = useMemo(() => {
|
|
|
|
if (!data) return [];
|
2020-10-15 07:09:00 +02:00
|
|
|
if (loading) return data;
|
2020-08-27 22:46:05 +02:00
|
|
|
|
2020-08-27 12:42:24 +02:00
|
|
|
const map = data.reduce((obj, { x, t, y }) => {
|
|
|
|
if (!obj[x]) {
|
|
|
|
obj[x] = [];
|
2020-08-26 18:58:24 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 12:42:24 +02:00
|
|
|
obj[x].push({ t, y });
|
2020-08-26 18:58:24 +02:00
|
|
|
|
2020-08-27 12:42:24 +02:00
|
|
|
return obj;
|
|
|
|
}, {});
|
2020-08-26 18:58:24 +02:00
|
|
|
|
2020-08-27 12:42:24 +02:00
|
|
|
Object.keys(map).forEach(key => {
|
|
|
|
map[key] = getDateArray(map[key], startDate, endDate, unit);
|
|
|
|
});
|
2020-08-26 18:58:24 +02:00
|
|
|
|
2020-08-31 00:29:31 +02:00
|
|
|
return Object.keys(map).map((key, index) => {
|
2022-01-14 09:39:27 +01:00
|
|
|
const color = colord(EVENT_COLORS[index % EVENT_COLORS.length]);
|
2020-08-31 00:29:31 +02:00
|
|
|
return {
|
|
|
|
label: key,
|
|
|
|
data: map[key],
|
|
|
|
lineTension: 0,
|
2022-01-14 09:39:27 +01:00
|
|
|
backgroundColor: color.alpha(0.6).toRgbString(),
|
|
|
|
borderColor: color.alpha(0.7).toRgbString(),
|
2020-08-31 00:29:31 +02:00
|
|
|
borderWidth: 1,
|
|
|
|
};
|
|
|
|
});
|
2020-10-15 07:09:00 +02:00
|
|
|
}, [data, loading]);
|
2020-08-26 18:58:24 +02:00
|
|
|
|
2020-08-27 12:42:24 +02:00
|
|
|
function handleUpdate(chart) {
|
2020-08-27 22:46:05 +02:00
|
|
|
chart.data.datasets = datasets;
|
2020-08-26 18:58:24 +02:00
|
|
|
|
2020-08-27 12:42:24 +02:00
|
|
|
chart.update();
|
2020-08-26 18:58:24 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 12:42:24 +02:00
|
|
|
if (!data) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-08-26 18:58:24 +02:00
|
|
|
|
|
|
|
return (
|
2020-08-28 03:44:20 +02:00
|
|
|
<BarChart
|
|
|
|
chartId={`events-${websiteId}`}
|
2020-10-10 05:37:24 +02:00
|
|
|
className={className}
|
2020-08-28 03:44:20 +02:00
|
|
|
datasets={datasets}
|
|
|
|
unit={unit}
|
2020-10-15 18:10:59 +02:00
|
|
|
height={300}
|
2020-08-28 03:44:20 +02:00
|
|
|
records={getDateLength(startDate, endDate, unit)}
|
|
|
|
onUpdate={handleUpdate}
|
2020-10-15 07:09:00 +02:00
|
|
|
loading={loading}
|
2020-08-28 03:44:20 +02:00
|
|
|
stacked
|
|
|
|
/>
|
2020-08-26 18:58:24 +02:00
|
|
|
);
|
|
|
|
}
|