2020-08-27 12:42:24 +02:00
|
|
|
import React from 'react';
|
2020-09-06 02:27:01 +02:00
|
|
|
import { useIntl } from 'react-intl';
|
2020-09-20 20:28:38 +02:00
|
|
|
import tinycolor from 'tinycolor2';
|
2020-08-28 08:45:37 +02:00
|
|
|
import CheckVisible from 'components/helpers/CheckVisible';
|
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';
|
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
|
|
|
}) {
|
2020-09-06 02:27:01 +02:00
|
|
|
const intl = useIntl();
|
2020-09-20 20:28:38 +02:00
|
|
|
const [theme] = useTheme();
|
|
|
|
const primaryColor = tinycolor(THEME_COLORS[theme].primary);
|
|
|
|
const colors = {
|
|
|
|
views: {
|
|
|
|
background: primaryColor.setAlpha(0.4).toRgbString(),
|
|
|
|
border: primaryColor.setAlpha(0.5).toRgbString(),
|
|
|
|
},
|
|
|
|
visitors: {
|
|
|
|
background: primaryColor.setAlpha(0.6).toRgbString(),
|
|
|
|
border: primaryColor.setAlpha(0.7).toRgbString(),
|
|
|
|
},
|
|
|
|
};
|
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;
|
2020-09-08 06:32:45 +02:00
|
|
|
datasets[0].label = intl.formatMessage({
|
|
|
|
id: 'metrics.unique-visitors',
|
|
|
|
defaultMessage: 'Unique visitors',
|
|
|
|
});
|
2020-08-26 18:58:24 +02:00
|
|
|
datasets[1].data = data.pageviews;
|
2020-09-08 06:32:45 +02:00
|
|
|
datasets[1].label = intl.formatMessage({
|
|
|
|
id: 'metrics.page-views',
|
|
|
|
defaultMessage: 'Page views',
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-08-28 08:45:37 +02:00
|
|
|
<CheckVisible>
|
|
|
|
{visible => (
|
|
|
|
<BarChart
|
2020-10-10 02:58:27 +02:00
|
|
|
{...props}
|
2020-08-28 08:45:37 +02:00
|
|
|
className={className}
|
|
|
|
chartId={websiteId}
|
|
|
|
datasets={[
|
|
|
|
{
|
2020-09-06 02:27:01 +02:00
|
|
|
label: intl.formatMessage({
|
|
|
|
id: 'metrics.unique-visitors',
|
|
|
|
defaultMessage: 'Unique visitors',
|
|
|
|
}),
|
2020-10-09 08:26:05 +02:00
|
|
|
data: data.sessions,
|
2020-08-28 08:45:37 +02:00
|
|
|
lineTension: 0,
|
2020-09-20 20:28:38 +02:00
|
|
|
backgroundColor: colors.visitors.background,
|
|
|
|
borderColor: colors.visitors.border,
|
2020-08-28 08:45:37 +02:00
|
|
|
borderWidth: 1,
|
|
|
|
},
|
|
|
|
{
|
2020-09-06 02:27:01 +02:00
|
|
|
label: intl.formatMessage({
|
|
|
|
id: 'metrics.page-views',
|
|
|
|
defaultMessage: 'Page views',
|
|
|
|
}),
|
2020-08-28 08:45:37 +02:00
|
|
|
data: data.pageviews,
|
|
|
|
lineTension: 0,
|
2020-09-20 20:28:38 +02:00
|
|
|
backgroundColor: colors.views.background,
|
|
|
|
borderColor: colors.views.border,
|
2020-08-28 08:45:37 +02:00
|
|
|
borderWidth: 1,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
unit={unit}
|
2020-08-31 23:11:30 +02:00
|
|
|
records={records}
|
2020-10-09 10:04:06 +02:00
|
|
|
animationDuration={visible ? animationDuration : 0}
|
2020-08-28 08:45:37 +02:00
|
|
|
onUpdate={handleUpdate}
|
2020-09-20 20:28:38 +02:00
|
|
|
loading={loading}
|
2020-08-28 08:45:37 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</CheckVisible>
|
2020-07-26 09:12:42 +02:00
|
|
|
);
|
|
|
|
}
|