umami/components/pages/reports/funnel/FunnelChart.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

import { useCallback, useContext, useMemo } from 'react';
import { Loading } from 'react-basics';
2023-05-18 20:17:35 +02:00
import useMessages from 'hooks/useMessages';
import useTheme from 'hooks/useTheme';
import BarChart from 'components/metrics/BarChart';
2023-05-18 20:17:35 +02:00
import { formatLongNumber } from 'lib/format';
import styles from './FunnelChart.module.css';
import { ReportContext } from '../Report';
2023-05-18 20:17:35 +02:00
export function FunnelChart({ className, loading }) {
const { report } = useContext(ReportContext);
2023-05-18 20:17:35 +02:00
const { formatMessage, labels } = useMessages();
const { colors } = useTheme();
2023-05-18 20:17:35 +02:00
const { parameters, data } = report || {};
2023-05-18 20:17:35 +02:00
const renderXLabel = useCallback(
(label, index) => {
return parameters.urls[index];
},
[parameters],
2023-05-18 20:17:35 +02:00
);
const renderTooltip = useCallback((setTooltip, model) => {
const { opacity, dataPoints } = model.tooltip;
2023-05-18 20:17:35 +02:00
if (!dataPoints?.length || !opacity) {
setTooltip(null);
return;
}
setTooltip(`${formatLongNumber(dataPoints[0].raw.y)} ${formatMessage(labels.visitors)}`);
2023-05-18 20:17:35 +02:00
}, []);
const datasets = useMemo(() => {
return [
{
label: formatMessage(labels.uniqueVisitors),
data: data,
borderWidth: 1,
...colors.chart.visitors,
2023-05-18 20:17:35 +02:00
},
];
}, [data]);
2023-05-18 20:17:35 +02:00
if (loading) {
return <Loading icon="dots" className={styles.loading} />;
}
2023-05-18 20:17:35 +02:00
return (
<BarChart
className={className}
datasets={datasets}
unit="day"
loading={loading}
renderXLabel={renderXLabel}
renderTooltip={renderTooltip}
XAxisType="category"
/>
2023-05-18 20:17:35 +02:00
);
}
export default FunnelChart;