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

62 lines
1.5 KiB
JavaScript
Raw Normal View History

import { useCallback, 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';
export function FunnelChart({ report, data, loading, className }) {
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 } = 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;