index.tsx 4.97 KB
Newer Older
陈帅's avatar
陈帅 committed
1 2
import { Axis, Chart, Coord, Geom, Tooltip } from 'bizcharts';
import { Col, Row } from 'antd';
陈帅's avatar
陈帅 committed
3
import React, { Component } from 'react';
陈帅's avatar
陈帅 committed
4

愚道's avatar
愚道 committed
5
import autoHeight from './autoHeight';
陈帅's avatar
陈帅 committed
6 7 8 9 10 11 12
import styles from './index.less';

export interface IRadarProps {
  title?: React.ReactNode;
  height?: number;
  padding?: [number, number, number, number];
  hasLegend?: boolean;
陈帅's avatar
陈帅 committed
13
  data: {
陈帅's avatar
陈帅 committed
14 15
    name: string;
    label: string;
陈帅's avatar
陈帅 committed
16
    value: string | number;
陈帅's avatar
陈帅 committed
17
  }[];
陈帅's avatar
陈帅 committed
18 19 20 21 22 23 24
  colors?: string[];
  animate?: boolean;
  forceFit?: boolean;
  tickCount?: number;
  style?: React.CSSProperties;
}
interface IRadarState {
陈帅's avatar
陈帅 committed
25
  legendData: {
陈帅's avatar
陈帅 committed
26 27 28 29 30
    checked: boolean;
    name: string;
    color: string;
    percent: number;
    value: string;
陈帅's avatar
陈帅 committed
31
  }[];
陈帅's avatar
陈帅 committed
32 33 34 35 36 37
}
/* eslint react/no-danger:0 */
class Radar extends Component<IRadarProps, IRadarState> {
  state: IRadarState = {
    legendData: [],
  };
陈帅's avatar
陈帅 committed
38

陈帅's avatar
陈帅 committed
39
  chart: G2.Chart | undefined;
陈帅's avatar
陈帅 committed
40

陈帅's avatar
陈帅 committed
41
  node: HTMLDivElement | undefined;
陈帅's avatar
陈帅 committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

  componentDidMount() {
    this.getLegendData();
  }

  componentDidUpdate(preProps: IRadarProps) {
    const { data } = this.props;
    if (data !== preProps.data) {
      this.getLegendData();
    }
  }

  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,
    });
  };

  handleRef = (n: HTMLDivElement) => {
    this.node = n;
  };

  handleLegendClick = (
    item: {
      checked: boolean;
      name: string;
    },
陈帅's avatar
陈帅 committed
92
    i: string | number,
陈帅's avatar
陈帅 committed
93 94 95 96 97 98 99 100 101 102
  ) => {
    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) {
陈帅's avatar
陈帅 committed
103
      this.chart.filter('name', val => filteredLegendData.indexOf(`${val}`) > -1);
陈帅's avatar
陈帅 committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
      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 (
      <div className={styles.radar} style={{ height }}>
        {title && <h4>{title}</h4>}
        <Chart
          scale={scale}
          height={chartHeight}
          forceFit={forceFit}
          data={data}
          padding={padding}
          animate={animate}
          onGetG2Instance={this.getG2Instance}
        >
          <Tooltip />
          <Coord type="polar" />
          <Axis
            name="label"
            line={undefined}
            tickLine={undefined}
            grid={{
              lineStyle: {
                lineDash: undefined,
              },
              hideFirstLine: false,
            }}
          />
          <Axis
            name="value"
            grid={{
              type: 'polygon',
              lineStyle: {
                lineDash: undefined,
              },
            }}
          />
          <Geom type="line" position="label*value" color={['name', colors]} size={1} />
          <Geom
            type="point"
            position="label*value"
            color={['name', colors]}
            shape="circle"
            size={3}
          />
        </Chart>
        {hasLegend && (
          <Row className={styles.legend}>
            {legendData.map((item, i) => (
              <Col
                span={24 / legendData.length}
                key={item.name}
                onClick={() => this.handleLegendClick(item, i)}
              >
                <div className={styles.legendItem}>
                  <p>
                    <span
                      className={styles.dot}
                      style={{
                        backgroundColor: !item.checked ? '#aaa' : item.color,
                      }}
                    />
                    <span>{item.name}</span>
                  </p>
                  <h6>{item.value}</h6>
                </div>
              </Col>
            ))}
          </Row>
        )}
      </div>
    );
  }
}

export default autoHeight()(Radar);