index.js 3.61 KB
Newer Older
1 2 3
import React, { PureComponent } from 'react';
import G2 from 'g2';
import { Row, Col } from 'antd';
nikogu's avatar
nikogu committed
4
import equal from '../equal';
5 6 7 8 9 10 11 12 13 14 15 16 17
import styles from './index.less';

/* eslint react/no-danger:0 */
class Radar extends PureComponent {
  state = {
    legendData: [],
  }

  componentDidMount() {
    this.renderChart(this.props.data);
  }

  componentWillReceiveProps(nextProps) {
nikogu's avatar
nikogu committed
18
    if (!equal(this.props, nextProps)) {
19 20 21 22
      this.renderChart(nextProps.data);
    }
  }

nikogu's avatar
nikogu committed
23 24 25 26 27 28
  componentWillUnmount() {
    if (this.chart) {
      this.chart.destroy();
    }
  }

29 30 31 32 33 34 35 36 37 38 39 40 41 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 92 93 94 95 96 97 98 99 100 101 102 103 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
  handleRef = (n) => {
    this.node = n;
  }

  handleLegendClick = (item, i) => {
    const newItem = item;
    newItem.checked = !newItem.checked;

    const legendData = this.state.legendData;
    legendData[i] = newItem;

    if (this.chart) {
      const filterItem = legendData.filter(l => l.checked).map(l => l.name);
      this.chart.filter('name', filterItem);
      this.chart.repaint();
    }

    this.setState({
      legendData,
    });
  }

  renderChart(data) {
    const { height = 0,
      hasLegend = true,
      fit = true,
      tickCount = 4,
      margin = [16, 0, 16, 0] } = this.props;

    if (!data || (data && data.length < 1)) {
      return;
    }

    // clean
    this.node.innerHTML = '';

    const chart = new G2.Chart({
      container: this.node,
      forceFit: fit,
      height: height - 22,
      plotCfg: {
        margin,
      },
    });

    this.chart = chart;

    chart.source(data, {
      value: {
        min: 0,
        tickCount,
      },
    });

    chart.coord('polar');
    chart.legend(false);

    chart.axis('label', {
      line: null,
    });

    chart.axis('value', {
      grid: {
        type: 'polygon',
      },
    });

    chart.line().position('label*value').color('name');
    chart.point().position('label*value').color('name').shape('circle');

    chart.render();

    if (hasLegend) {
      const geom = chart.getGeoms()[0]; // 获取所有的图形
      const items = geom.getData(); // 获取图形对应的数据
      const legendData = items.map((item) => {
        /* eslint no-underscore-dangle:0 */
        const origin = item._origin;
        const result = {
          name: origin[0].name,
          color: item.color,
          checked: true,
          value: origin.reduce((p, n) => p + n.value, 0),
        };

        return result;
      });

      this.setState({
        legendData,
      });
    }
  }

  render() {
    const { height, title, hasLegend } = this.props;
    const { legendData } = this.state;

    return (
      <div className={styles.radar} style={{ height }}>
        <div>
          { title && <h4>{title}</h4>}
          <div ref={this.handleRef} />
          {
            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>
                      {
                        i !== (legendData.length - 1) && <div className={styles.split} />
                      }
                    </div>
                  </Col>
                ))
              }
            </Row>
          }
        </div>
      </div>
    );
  }
}

export default Radar;