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,
|
|
|
|
isBarChart: PropTypes.bool,
|
|
|
|
stats: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
export function GraphCard({ title, isBarChart, stats }) {
|
|
|
|
const generateGraphData = (stats) => ({
|
2018-09-08 10:06:18 +03:00
|
|
|
labels: keys(stats),
|
2018-09-01 12:08:27 +03:00
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
title,
|
2018-09-08 10:06:18 +03:00
|
|
|
data: values(stats),
|
2018-09-01 12:08:27 +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,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const renderGraph = () => {
|
|
|
|
const Component = isBarChart ? HorizontalBar : Doughnut;
|
2018-09-08 21:34:04 +03:00
|
|
|
const options = {
|
|
|
|
legend: isBarChart ? { display: false } : { position: 'right' },
|
|
|
|
scales: isBarChart ? {
|
|
|
|
xAxes: [
|
|
|
|
{
|
|
|
|
ticks: { beginAtZero: true },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} : null,
|
|
|
|
};
|
2018-09-01 12:08:27 +03:00
|
|
|
|
2018-09-08 21:34:04 +03:00
|
|
|
return <Component data={generateGraphData(stats)} options={options} />;
|
2018-09-01 12:08:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card className="mt-4">
|
|
|
|
<CardHeader>{title}</CardHeader>
|
|
|
|
<CardBody>{renderGraph()}</CardBody>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphCard.propTypes = propTypes;
|