2019-03-10 10:28:14 +03:00
|
|
|
import { Card, CardHeader, CardBody, CardFooter } from 'reactstrap';
|
2018-09-01 12:08:27 +03:00
|
|
|
import { Doughnut, HorizontalBar } from 'react-chartjs-2';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2018-09-08 10:06:18 +03:00
|
|
|
import { keys, values } from 'ramda';
|
2019-03-10 12:08:42 +03:00
|
|
|
import './GraphCard.scss';
|
2018-09-01 12:08:27 +03:00
|
|
|
|
|
|
|
const propTypes = {
|
2019-03-10 19:55:02 +03:00
|
|
|
title: PropTypes.oneOfType([ PropTypes.string, PropTypes.func ]),
|
2019-03-10 10:28:14 +03:00
|
|
|
footer: PropTypes.oneOfType([ PropTypes.string, PropTypes.node ]),
|
2018-09-01 12:08:27 +03:00
|
|
|
isBarChart: PropTypes.bool,
|
|
|
|
stats: PropTypes.object,
|
2019-03-10 10:28:14 +03:00
|
|
|
max: PropTypes.number,
|
|
|
|
redraw: PropTypes.bool,
|
2018-09-01 12:08:27 +03:00
|
|
|
};
|
|
|
|
|
2018-10-19 21:27:25 +03:00
|
|
|
const generateGraphData = (title, isBarChart, labels, data) => ({
|
|
|
|
labels,
|
2018-10-19 20:04:22 +03:00
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
title,
|
2018-10-19 21:27:25 +03:00
|
|
|
data,
|
2018-10-19 20:04:22 +03:00
|
|
|
backgroundColor: isBarChart ? 'rgba(70, 150, 229, 0.4)' : [
|
|
|
|
'#97BBCD',
|
|
|
|
'#DCDCDC',
|
|
|
|
'#F7464A',
|
|
|
|
'#46BFBD',
|
|
|
|
'#FDB45C',
|
|
|
|
'#949FB1',
|
|
|
|
'#4D5360',
|
|
|
|
],
|
|
|
|
borderColor: isBarChart ? 'rgba(70, 150, 229, 1)' : 'white',
|
|
|
|
borderWidth: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2018-09-01 12:08:27 +03:00
|
|
|
|
2019-03-10 10:28:14 +03:00
|
|
|
const dropLabelIfHidden = (label) => label.startsWith('hidden') ? '' : label;
|
2018-10-19 21:27:25 +03:00
|
|
|
|
2019-03-10 10:28:14 +03:00
|
|
|
const renderGraph = (title, isBarChart, stats, max, redraw) => {
|
2018-10-19 20:04:22 +03:00
|
|
|
const Component = isBarChart ? HorizontalBar : Doughnut;
|
2019-03-10 10:28:14 +03:00
|
|
|
const labels = keys(stats).map(dropLabelIfHidden);
|
2018-10-19 21:27:25 +03:00
|
|
|
const data = values(stats);
|
2018-10-19 20:04:22 +03:00
|
|
|
const options = {
|
|
|
|
legend: isBarChart ? { display: false } : { position: 'right' },
|
|
|
|
scales: isBarChart ? {
|
|
|
|
xAxes: [
|
|
|
|
{
|
2019-03-10 10:28:14 +03:00
|
|
|
ticks: { beginAtZero: true, max },
|
2018-10-19 20:04:22 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
} : null,
|
2018-10-30 22:41:36 +03:00
|
|
|
tooltips: {
|
|
|
|
intersect: !isBarChart,
|
2019-03-10 10:28:14 +03:00
|
|
|
|
|
|
|
// Do not show tooltip on items with empty label when in a bar chart
|
|
|
|
filter: ({ yLabel }) => !isBarChart || yLabel !== '',
|
2018-10-30 22:41:36 +03:00
|
|
|
},
|
2018-09-01 12:08:27 +03:00
|
|
|
};
|
2019-03-10 10:28:14 +03:00
|
|
|
const graphData = generateGraphData(title, isBarChart, labels, data);
|
|
|
|
const height = labels.length < 20 ? null : labels.length * 8;
|
2018-09-01 12:08:27 +03:00
|
|
|
|
2019-03-10 10:28:14 +03:00
|
|
|
return <Component data={graphData} options={options} height={height} redraw={redraw} />;
|
2018-10-19 20:04:22 +03:00
|
|
|
};
|
|
|
|
|
2019-03-10 10:28:14 +03:00
|
|
|
const GraphCard = ({ title, footer, isBarChart, stats, max, redraw = false }) => (
|
2018-10-19 20:04:22 +03:00
|
|
|
<Card className="mt-4">
|
2019-03-10 19:55:02 +03:00
|
|
|
<CardHeader className="graph-card__header">{typeof title === 'function' ? title() : title}</CardHeader>
|
2019-03-10 10:28:14 +03:00
|
|
|
<CardBody>{renderGraph(title, isBarChart, stats, max, redraw)}</CardBody>
|
2019-03-10 12:08:42 +03:00
|
|
|
{footer && <CardFooter className="graph-card__footer--sticky">{footer}</CardFooter>}
|
2018-10-19 20:04:22 +03:00
|
|
|
</Card>
|
|
|
|
);
|
2018-09-01 12:08:27 +03:00
|
|
|
|
|
|
|
GraphCard.propTypes = propTypes;
|
2018-10-19 20:04:22 +03:00
|
|
|
|
|
|
|
export default GraphCard;
|