2023-05-29 06:37:34 +02:00
|
|
|
import { useCallback, useContext, useMemo } from 'react';
|
2023-05-25 06:40:02 +02:00
|
|
|
import { Loading } from 'react-basics';
|
2023-05-18 20:17:35 +02:00
|
|
|
import useMessages from 'hooks/useMessages';
|
|
|
|
import useTheme from 'hooks/useTheme';
|
2023-05-25 06:40:02 +02:00
|
|
|
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';
|
2023-05-29 06:37:34 +02:00
|
|
|
import { ReportContext } from '../Report';
|
2023-05-18 20:17:35 +02:00
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
export function FunnelChart({ className, loading }) {
|
|
|
|
const { report } = useContext(ReportContext);
|
2023-05-18 20:17:35 +02:00
|
|
|
const { formatMessage, labels } = useMessages();
|
2023-05-25 06:40:02 +02:00
|
|
|
const { colors } = useTheme();
|
2023-05-18 20:17:35 +02:00
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
const { parameters, data } = report || {};
|
2023-05-18 20:17:35 +02:00
|
|
|
|
2023-05-25 06:40:02 +02:00
|
|
|
const renderXLabel = useCallback(
|
|
|
|
(label, index) => {
|
|
|
|
return parameters.urls[index];
|
|
|
|
},
|
|
|
|
[parameters],
|
2023-05-18 20:17:35 +02:00
|
|
|
);
|
|
|
|
|
2023-06-16 05:15:31 +02:00
|
|
|
const renderTooltipPopup = useCallback((setTooltipPopup, model) => {
|
2023-05-25 06:40:02 +02:00
|
|
|
const { opacity, dataPoints } = model.tooltip;
|
2023-05-18 20:17:35 +02:00
|
|
|
|
|
|
|
if (!dataPoints?.length || !opacity) {
|
2023-06-16 05:15:31 +02:00
|
|
|
setTooltipPopup(null);
|
2023-05-18 20:17:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-16 05:15:31 +02:00
|
|
|
setTooltipPopup(`${formatLongNumber(dataPoints[0].raw.y)} ${formatMessage(labels.visitors)}`);
|
2023-05-18 20:17:35 +02:00
|
|
|
}, []);
|
|
|
|
|
2023-05-25 06:40:02 +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
|
|
|
},
|
2023-05-25 06:40:02 +02:00
|
|
|
];
|
|
|
|
}, [data]);
|
2023-05-18 20:17:35 +02:00
|
|
|
|
2023-05-25 06:40:02 +02:00
|
|
|
if (loading) {
|
|
|
|
return <Loading icon="dots" className={styles.loading} />;
|
|
|
|
}
|
2023-05-18 20:17:35 +02:00
|
|
|
|
|
|
|
return (
|
2023-05-25 06:40:02 +02:00
|
|
|
<BarChart
|
|
|
|
className={className}
|
|
|
|
datasets={datasets}
|
|
|
|
unit="day"
|
|
|
|
loading={loading}
|
|
|
|
renderXLabel={renderXLabel}
|
2023-06-16 05:15:31 +02:00
|
|
|
renderTooltipPopup={renderTooltipPopup}
|
2023-05-25 06:40:02 +02:00
|
|
|
XAxisType="category"
|
|
|
|
/>
|
2023-05-18 20:17:35 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FunnelChart;
|