server_table/src/Chart.tsx

81 lines
2.4 KiB
TypeScript
Raw Normal View History

import {
2018-07-01 14:12:00 +00:00
PieChart, Pie, ResponsiveContainer, Tooltip, Cell
} from 'recharts';
import * as React from 'react';
2018-07-01 14:12:00 +00:00
import * as _ from 'lodash';
2018-07-01 17:27:37 +00:00
import { Card, CardImage, CardHeader, CardHeaderTitle } from 'bloomer';
const randomColor = require('randomcolor');
2018-07-01 14:12:00 +00:00
import * as Papa from 'papaparse';
export class Chart extends React.Component<any, any> {
constructor(props: any) {
super(props);
2018-07-01 14:12:00 +00:00
this.state = { data: [] };
const loaded = (results: any) => {
const local = this;
local.setState({
headers: results.meta.fields,
data: results.data,
});
}
Papa.parse(this.props.csvFile, {
header: true,
download: true,
skipEmptyLines: true,
dynamicTyping: true,
complete: loaded
});
}
public render() {
2018-07-01 14:12:00 +00:00
const data = this.state.data;
const countMap = _.countBy(data, (o: any) => _.get<any>(o, this.props.chartColumn))
const chartData = _.map(_.keys(countMap), (k: any) => {
return {
2018-07-01 15:31:27 +00:00
name: k,
2018-07-01 14:12:00 +00:00
value: countMap[k],
}
})
// const RADIAN = Math.PI / 180;
// const renderCustomizedLabel = ({ cx, cy, midAngle, innerRadius, outerRadius, percent, index }:any) => {
// const radius = innerRadius + (outerRadius - innerRadius) * 0.5;
// const x = cx + radius * Math.cos(-midAngle * RADIAN);
// const y = cy + radius * Math.sin(-midAngle * RADIAN);
//
// return (
// <text x={x} y={y} fill="white" textAnchor='middle' dominantBaseline="central">
// {chartData[index].name}
// </text>
// );
// };
const colorMap = this.props.colorMap;
// const LabeText = (props: any) => {
// return (<text>{props.name}</text>);
// };
return (
2018-07-01 17:27:37 +00:00
<Card>
<CardHeader>
<CardHeaderTitle className='is-centered'>
2018-07-01 18:36:04 +00:00
{this.props.title}
2018-07-01 17:27:37 +00:00
</CardHeaderTitle>
</CardHeader>
<CardImage>
<ResponsiveContainer width="100%" height={300}>
<PieChart>
<Pie data={chartData} dataKey='value' outerRadius={100} labelLine={true} label={true}>
{
chartData.map((_0, _1) => {
const color = _.get(colorMap, _0.name, randomColor());
return (<Cell key={_1} fill={color} />)
})
}
</Pie>
<Tooltip />
</PieChart>
</ResponsiveContainer>
</CardImage>
</Card>
)
}
}