2023-03-09 01:37:43 +01:00
|
|
|
import { useVisible } from 'react-basics';
|
2020-09-06 02:27:01 +02:00
|
|
|
import { useIntl } from 'react-intl';
|
2022-01-14 09:39:27 +01:00
|
|
|
import { colord } from 'colord';
|
2020-08-26 18:58:24 +02:00
|
|
|
import BarChart from './BarChart';
|
2020-09-20 20:28:38 +02:00
|
|
|
import useTheme from 'hooks/useTheme';
|
2020-10-09 21:39:03 +02:00
|
|
|
import { THEME_COLORS, DEFAULT_ANIMATION_DURATION } from 'lib/constants';
|
2023-03-09 01:37:43 +01:00
|
|
|
import { labels } from 'components/messages';
|
|
|
|
import { useMemo } from 'react';
|
2020-07-26 09:12:42 +02:00
|
|
|
|
2020-10-09 10:04:06 +02:00
|
|
|
export default function PageviewsChart({
|
|
|
|
websiteId,
|
|
|
|
data,
|
|
|
|
unit,
|
|
|
|
records,
|
|
|
|
className,
|
|
|
|
loading,
|
2020-10-09 21:39:03 +02:00
|
|
|
animationDuration = DEFAULT_ANIMATION_DURATION,
|
2020-10-10 02:58:27 +02:00
|
|
|
...props
|
2020-10-09 10:04:06 +02:00
|
|
|
}) {
|
2023-03-09 01:37:43 +01:00
|
|
|
const { formatMessage } = useIntl();
|
2020-09-20 20:28:38 +02:00
|
|
|
const [theme] = useTheme();
|
2023-03-09 01:37:43 +01:00
|
|
|
const { ref, visible } = useVisible();
|
|
|
|
|
|
|
|
const colors = useMemo(() => {
|
|
|
|
const primaryColor = colord(THEME_COLORS[theme].primary);
|
|
|
|
return {
|
|
|
|
views: {
|
2023-03-15 06:37:50 +01:00
|
|
|
hoverBackgroundColor: primaryColor.alpha(0.7).toRgbString(),
|
|
|
|
backgroundColor: primaryColor.alpha(0.4).toRgbString(),
|
|
|
|
borderColor: primaryColor.alpha(0.7).toRgbString(),
|
|
|
|
hoverBorderColor: primaryColor.toRgbString(),
|
2023-03-09 01:37:43 +01:00
|
|
|
},
|
|
|
|
visitors: {
|
2023-03-15 06:37:50 +01:00
|
|
|
hoverBackgroundColor: primaryColor.alpha(0.9).toRgbString(),
|
|
|
|
backgroundColor: primaryColor.alpha(0.6).toRgbString(),
|
|
|
|
borderColor: primaryColor.alpha(0.9).toRgbString(),
|
|
|
|
hoverBorderColor: primaryColor.toRgbString(),
|
2023-03-09 01:37:43 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}, [theme]);
|
2020-09-06 02:27:01 +02:00
|
|
|
|
2020-08-26 18:58:24 +02:00
|
|
|
const handleUpdate = chart => {
|
|
|
|
const {
|
|
|
|
data: { datasets },
|
|
|
|
} = chart;
|
2020-07-29 06:50:29 +02:00
|
|
|
|
2020-10-09 08:26:05 +02:00
|
|
|
datasets[0].data = data.sessions;
|
2023-03-09 01:37:43 +01:00
|
|
|
datasets[0].label = formatMessage(labels.uniqueVisitors);
|
2020-08-26 18:58:24 +02:00
|
|
|
datasets[1].data = data.pageviews;
|
2023-03-09 01:37:43 +01:00
|
|
|
datasets[1].label = formatMessage(labels.pageViews);
|
2020-07-29 06:50:29 +02:00
|
|
|
};
|
2020-07-26 09:12:42 +02:00
|
|
|
|
2020-08-26 18:58:24 +02:00
|
|
|
if (!data) {
|
|
|
|
return null;
|
2020-07-26 09:12:42 +02:00
|
|
|
}
|
|
|
|
|
2023-03-15 06:37:50 +01:00
|
|
|
const datasets = [
|
|
|
|
{
|
|
|
|
label: formatMessage(labels.uniqueVisitors),
|
|
|
|
data: data.sessions,
|
|
|
|
lineTension: 0,
|
|
|
|
borderWidth: 1,
|
|
|
|
...colors.visitors,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: formatMessage(labels.pageViews),
|
|
|
|
data: data.pageviews,
|
|
|
|
lineTension: 0,
|
|
|
|
borderWidth: 1,
|
|
|
|
...colors.views,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-07-26 09:12:42 +02:00
|
|
|
return (
|
2023-03-09 01:37:43 +01:00
|
|
|
<div ref={ref}>
|
|
|
|
<BarChart
|
|
|
|
{...props}
|
2023-03-15 06:37:50 +01:00
|
|
|
key={websiteId}
|
2023-03-09 01:37:43 +01:00
|
|
|
className={className}
|
2023-03-15 06:37:50 +01:00
|
|
|
datasets={datasets}
|
2023-03-09 01:37:43 +01:00
|
|
|
unit={unit}
|
|
|
|
records={records}
|
|
|
|
animationDuration={visible ? animationDuration : 0}
|
|
|
|
onUpdate={handleUpdate}
|
|
|
|
loading={loading}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-07-26 09:12:42 +02:00
|
|
|
);
|
|
|
|
}
|