import React, { Component } from 'react'; import { Chart, Tooltip, Geom, Coord, Axis } from 'bizcharts'; import { Row, Col } from 'antd'; import autoHeight from './autoHeight'; import styles from './index.less'; export interface IRadarProps { title?: React.ReactNode; height?: number; padding?: [number, number, number, number]; hasLegend?: boolean; data: Array<{ name: string; label: string; value: string | number; }>; colors?: string[]; animate?: boolean; forceFit?: boolean; tickCount?: number; style?: React.CSSProperties; } interface IRadarState { legendData: Array<{ checked: boolean; name: string; color: string; percent: number; value: string; }>; } /* eslint react/no-danger:0 */ class Radar extends Component { state: IRadarState = { legendData: [], }; componentDidMount() { this.getLegendData(); } componentDidUpdate(preProps: IRadarProps) { const { data } = this.props; if (data !== preProps.data) { this.getLegendData(); } } chart: G2.Chart | undefined; getG2Instance = (chart: G2.Chart) => { this.chart = chart; }; // for custom lengend view getLegendData = () => { if (!this.chart) return; const geom = this.chart.getAllGeoms()[0]; // 获取所有的图形 if (!geom) return; const items = geom.get('dataArray') || []; // 获取图形对应的 const legendData = items.map((item: { color: any; _origin: any }[]) => { // eslint-disable-next-line const origins = item.map(t => t._origin); const result = { name: origins[0].name, color: item[0].color, checked: true, value: origins.reduce((p, n) => p + n.value, 0), }; return result; }); this.setState({ legendData, }); }; node: HTMLDivElement | undefined; handleRef = (n: HTMLDivElement) => { this.node = n; }; handleLegendClick = ( item: { checked: boolean; name: string; }, i: string | number ) => { const newItem = item; newItem.checked = !newItem.checked; const { legendData } = this.state; legendData[i] = newItem; const filteredLegendData = legendData.filter(l => l.checked).map(l => l.name); if (this.chart) { this.chart.filter('name', val => filteredLegendData.indexOf(val + '') > -1); this.chart.repaint(); } this.setState({ legendData, }); }; render() { const defaultColors = [ '#1890FF', '#FACC14', '#2FC25B', '#8543E0', '#F04864', '#13C2C2', '#fa8c16', '#a0d911', ]; const { data = [], height = 0, title, hasLegend = false, forceFit = true, tickCount = 5, padding = [35, 30, 16, 30] as [number, number, number, number], animate = true, colors = defaultColors, } = this.props; const { legendData } = this.state; const scale = { value: { min: 0, tickCount, }, }; const chartHeight = height - (hasLegend ? 80 : 22); return (
{title &&

{title}

} {hasLegend && ( {legendData.map((item, i) => ( this.handleLegendClick(item, i)} >

{item.name}

{item.value}
))}
)}
); } } export default autoHeight()(Radar);