2020-09-03 21:34:22 +03:00
|
|
|
import React, { ChangeEvent, useRef } from 'react';
|
2020-06-06 11:35:13 +03:00
|
|
|
import { Doughnut, HorizontalBar } from 'react-chartjs-2';
|
|
|
|
import { keys, values } from 'ramda';
|
2020-06-06 12:58:25 +03:00
|
|
|
import classNames from 'classnames';
|
2020-09-03 21:34:22 +03:00
|
|
|
import Chart, { ChartData, ChartDataSets, ChartOptions } from 'chart.js';
|
2020-06-06 11:35:13 +03:00
|
|
|
import { fillTheGaps } from '../../utils/helpers/visits';
|
2020-09-03 21:34:22 +03:00
|
|
|
import { Stats } from '../types';
|
2020-06-06 12:58:25 +03:00
|
|
|
import './DefaultChart.scss';
|
2020-06-06 11:35:13 +03:00
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
export interface DefaultChartProps {
|
|
|
|
title: Function | string;
|
|
|
|
stats: Stats;
|
|
|
|
isBarChart?: boolean;
|
|
|
|
max?: number;
|
|
|
|
highlightedStats?: Stats;
|
|
|
|
highlightedLabel?: string;
|
|
|
|
onClick?: (label: string) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const generateGraphData = (
|
|
|
|
title: Function | string,
|
|
|
|
isBarChart: boolean,
|
|
|
|
labels: string[],
|
|
|
|
data: number[],
|
|
|
|
highlightedData?: number[],
|
|
|
|
highlightedLabel?: string,
|
|
|
|
): ChartData => ({
|
2020-06-06 11:35:13 +03:00
|
|
|
labels,
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
title,
|
|
|
|
label: highlightedData ? 'Non-selected' : 'Visits',
|
|
|
|
data,
|
|
|
|
backgroundColor: isBarChart ? 'rgba(70, 150, 229, 0.4)' : [
|
|
|
|
'#97BBCD',
|
|
|
|
'#F7464A',
|
|
|
|
'#46BFBD',
|
|
|
|
'#FDB45C',
|
|
|
|
'#949FB1',
|
|
|
|
'#57A773',
|
|
|
|
'#414066',
|
|
|
|
'#08B2E3',
|
|
|
|
'#B6C454',
|
|
|
|
'#DCDCDC',
|
|
|
|
'#463730',
|
|
|
|
],
|
|
|
|
borderColor: isBarChart ? 'rgba(70, 150, 229, 1)' : 'white',
|
|
|
|
borderWidth: 2,
|
|
|
|
},
|
2020-09-03 21:34:22 +03:00
|
|
|
(highlightedData && {
|
2020-06-06 11:35:13 +03:00
|
|
|
title,
|
2020-09-03 21:34:22 +03:00
|
|
|
label: highlightedLabel ?? 'Selected',
|
2020-06-06 11:35:13 +03:00
|
|
|
data: highlightedData,
|
|
|
|
backgroundColor: 'rgba(247, 127, 40, 0.4)',
|
|
|
|
borderColor: '#F77F28',
|
|
|
|
borderWidth: 2,
|
2020-09-03 21:34:22 +03:00
|
|
|
}) as unknown as ChartDataSets,
|
2020-06-06 11:35:13 +03:00
|
|
|
].filter(Boolean),
|
|
|
|
});
|
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
const dropLabelIfHidden = (label: string) => label.startsWith('hidden') ? '' : label;
|
2020-06-06 11:35:13 +03:00
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
const determineHeight = (isBarChart: boolean, labels: string[]): number | undefined => {
|
2020-06-06 13:08:21 +03:00
|
|
|
if (!isBarChart) {
|
|
|
|
return 300;
|
2020-06-06 11:35:13 +03:00
|
|
|
}
|
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
return isBarChart && labels.length > 20 ? labels.length * 8 : undefined;
|
2020-06-06 11:35:13 +03:00
|
|
|
};
|
|
|
|
|
2020-06-06 12:58:25 +03:00
|
|
|
/* eslint-disable react/prop-types */
|
2020-09-03 21:34:22 +03:00
|
|
|
const renderPieChartLegend = ({ config }: Chart) => {
|
|
|
|
const { labels = [], datasets = [] } = config.data ?? {};
|
|
|
|
const { defaultColor } = config.options ?? {} as any;
|
2020-06-06 12:58:25 +03:00
|
|
|
const [{ backgroundColor: colors }] = datasets;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className="default-chart__pie-chart-legend">
|
|
|
|
{labels.map((label, index) => (
|
2020-09-03 21:34:22 +03:00
|
|
|
<li key={label as string} className="default-chart__pie-chart-legend-item d-flex">
|
2020-06-06 12:58:25 +03:00
|
|
|
<div
|
|
|
|
className="default-chart__pie-chart-legend-item-color"
|
2020-09-03 21:34:22 +03:00
|
|
|
style={{ backgroundColor: (colors as string[])[index] || defaultColor }}
|
2020-06-06 12:58:25 +03:00
|
|
|
/>
|
|
|
|
<small className="default-chart__pie-chart-legend-item-text flex-fill">{label}</small>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
/* eslint-enable react/prop-types */
|
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
const chartElementAtEvent = (onClick?: (label: string) => void) => ([ chart ]: [{ _index: number; _chart: Chart }]) => {
|
2020-06-06 12:58:25 +03:00
|
|
|
if (!onClick || !chart) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { _index, _chart: { data } } = chart;
|
|
|
|
const { labels } = data;
|
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
onClick(labels?.[_index] as string);
|
2020-06-06 12:58:25 +03:00
|
|
|
};
|
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
const statsAreDefined = (stats: Stats | undefined): stats is Stats => !!stats && Object.keys(stats).length > 0;
|
|
|
|
|
|
|
|
const DefaultChart = (
|
|
|
|
{ title, isBarChart = false, stats, max, highlightedStats, highlightedLabel, onClick }: DefaultChartProps,
|
|
|
|
) => {
|
2020-06-06 11:35:13 +03:00
|
|
|
const Component = isBarChart ? HorizontalBar : Doughnut;
|
|
|
|
const labels = keys(stats).map(dropLabelIfHidden);
|
2020-09-03 21:34:22 +03:00
|
|
|
const data = values(
|
|
|
|
!statsAreDefined(highlightedStats) ? stats : keys(highlightedStats).reduce((acc, highlightedKey) => {
|
|
|
|
if (acc[highlightedKey]) {
|
|
|
|
acc[highlightedKey] -= highlightedStats[highlightedKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, { ...stats }),
|
|
|
|
);
|
|
|
|
const highlightedData = statsAreDefined(highlightedStats) ? fillTheGaps(highlightedStats, labels) : undefined;
|
|
|
|
const chartRef = useRef<HorizontalBar | Doughnut>();
|
2020-06-06 11:35:13 +03:00
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
const options: ChartOptions = {
|
2020-06-06 12:58:25 +03:00
|
|
|
legend: { display: false },
|
2020-09-03 21:34:22 +03:00
|
|
|
legendCallback: !isBarChart && renderPieChartLegend as any,
|
|
|
|
scales: !isBarChart ? undefined : {
|
2020-06-06 11:35:13 +03:00
|
|
|
xAxes: [
|
|
|
|
{
|
2020-09-03 21:34:22 +03:00
|
|
|
ticks: { beginAtZero: true, precision: 0, max } as any,
|
2020-06-06 11:35:13 +03:00
|
|
|
stacked: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
yAxes: [{ stacked: true }],
|
|
|
|
},
|
|
|
|
tooltips: {
|
|
|
|
intersect: !isBarChart,
|
|
|
|
|
|
|
|
// Do not show tooltip on items with empty label when in a bar chart
|
|
|
|
filter: ({ yLabel }) => !isBarChart || yLabel !== '',
|
|
|
|
},
|
2020-09-03 21:34:22 +03:00
|
|
|
onHover: !isBarChart ? undefined : ((e: ChangeEvent<HTMLElement>, chartElement: HorizontalBar[] | Doughnut[]) => {
|
|
|
|
const { target } = e;
|
|
|
|
|
2020-06-06 11:35:13 +03:00
|
|
|
target.style.cursor = chartElement[0] ? 'pointer' : 'default';
|
2020-09-03 21:34:22 +03:00
|
|
|
}) as any, // TODO Types seem to be incorrectly defined
|
2020-06-06 11:35:13 +03:00
|
|
|
};
|
|
|
|
const graphData = generateGraphData(title, isBarChart, labels, data, highlightedData, highlightedLabel);
|
|
|
|
const height = determineHeight(isBarChart, labels);
|
|
|
|
|
|
|
|
// Provide a key based on the height, so that every time the dataset changes, a new graph is rendered
|
|
|
|
return (
|
2020-06-06 12:58:25 +03:00
|
|
|
<div className="row">
|
|
|
|
<div className={classNames('col-sm-12', { 'col-md-7': !isBarChart })}>
|
|
|
|
<Component
|
2020-09-03 21:34:22 +03:00
|
|
|
ref={chartRef as any}
|
2020-06-06 12:58:25 +03:00
|
|
|
key={height}
|
|
|
|
data={graphData}
|
|
|
|
options={options}
|
|
|
|
height={height}
|
|
|
|
getElementAtEvent={chartElementAtEvent(onClick)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{!isBarChart && (
|
|
|
|
<div className="col-sm-12 col-md-5">
|
2020-09-03 21:34:22 +03:00
|
|
|
{chartRef.current?.chartInstance.generateLegend()}
|
2020-06-06 12:58:25 +03:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-06-06 11:35:13 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DefaultChart;
|