2018-09-01 12:08:27 +03:00
|
|
|
import { Card, CardHeader, CardBody } from 'reactstrap';
|
|
|
|
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';
|
2018-09-01 12:08:27 +03:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
title: PropTypes.string,
|
2018-10-29 00:54:08 +03:00
|
|
|
children: PropTypes.node,
|
2018-09-01 12:08:27 +03:00
|
|
|
isBarChart: PropTypes.bool,
|
|
|
|
stats: PropTypes.object,
|
2018-10-19 21:27:25 +03:00
|
|
|
matchMedia: PropTypes.func,
|
|
|
|
};
|
|
|
|
const defaultProps = {
|
|
|
|
matchMedia: global.window ? global.window.matchMedia : () => {},
|
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
|
|
|
|
2018-10-19 21:27:25 +03:00
|
|
|
const determineGraphAspectRatio = (barsCount, isBarChart, matchMedia) => {
|
|
|
|
const determineAspectRationModifier = () => {
|
|
|
|
switch (true) {
|
|
|
|
case matchMedia('(max-width: 1200px)').matches:
|
|
|
|
return 1.5; // eslint-disable-line no-magic-numbers
|
|
|
|
case matchMedia('(max-width: 992px)').matches:
|
|
|
|
return 1.75; // eslint-disable-line no-magic-numbers
|
|
|
|
case matchMedia('(max-width: 768px)').matches:
|
|
|
|
return 2; // eslint-disable-line no-magic-numbers
|
|
|
|
case matchMedia('(max-width: 576px)').matches:
|
|
|
|
return 2.25; // eslint-disable-line no-magic-numbers
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MAX_BARS_WITHOUT_HEIGHT = 20;
|
|
|
|
const DEFAULT_ASPECT_RATION = 2;
|
|
|
|
const shouldCalculateAspectRatio = isBarChart && barsCount > MAX_BARS_WITHOUT_HEIGHT;
|
|
|
|
|
|
|
|
return shouldCalculateAspectRatio
|
|
|
|
? MAX_BARS_WITHOUT_HEIGHT / determineAspectRationModifier() * DEFAULT_ASPECT_RATION / barsCount
|
|
|
|
: DEFAULT_ASPECT_RATION;
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderGraph = (title, isBarChart, stats, matchMedia) => {
|
2018-10-19 20:04:22 +03:00
|
|
|
const Component = isBarChart ? HorizontalBar : Doughnut;
|
2018-10-19 21:27:25 +03:00
|
|
|
const labels = keys(stats);
|
|
|
|
const data = values(stats);
|
|
|
|
const aspectRatio = determineGraphAspectRatio(labels.length, isBarChart, matchMedia);
|
2018-10-19 20:04:22 +03:00
|
|
|
const options = {
|
2018-10-19 21:27:25 +03:00
|
|
|
aspectRatio,
|
2018-10-19 20:04:22 +03:00
|
|
|
legend: isBarChart ? { display: false } : { position: 'right' },
|
|
|
|
scales: isBarChart ? {
|
|
|
|
xAxes: [
|
|
|
|
{
|
|
|
|
ticks: { beginAtZero: true },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} : null,
|
2018-10-30 22:41:36 +03:00
|
|
|
tooltips: {
|
|
|
|
intersect: !isBarChart,
|
|
|
|
},
|
2018-09-01 12:08:27 +03:00
|
|
|
};
|
|
|
|
|
2018-10-19 21:27:25 +03:00
|
|
|
return <Component data={generateGraphData(title, isBarChart, labels, data)} options={options} height={null} />;
|
2018-10-19 20:04:22 +03:00
|
|
|
};
|
|
|
|
|
2018-10-29 00:54:08 +03:00
|
|
|
const GraphCard = ({ title, children, isBarChart, stats, matchMedia }) => (
|
2018-10-19 20:04:22 +03:00
|
|
|
<Card className="mt-4">
|
2018-10-29 00:54:08 +03:00
|
|
|
<CardHeader className="graph-card__header">{children || title}</CardHeader>
|
2018-10-19 21:27:25 +03:00
|
|
|
<CardBody>{renderGraph(title, isBarChart, stats, matchMedia)}</CardBody>
|
2018-10-19 20:04:22 +03:00
|
|
|
</Card>
|
|
|
|
);
|
2018-09-01 12:08:27 +03:00
|
|
|
|
|
|
|
GraphCard.propTypes = propTypes;
|
2018-10-19 21:27:25 +03:00
|
|
|
GraphCard.defaultProps = defaultProps;
|
2018-10-19 20:04:22 +03:00
|
|
|
|
|
|
|
export default GraphCard;
|