index.js 4.12 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
  handleRef = (n) => {
    this.node = n;
  }

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

afc163's avatar
afc163 committed
37
    const { legendData } = this.state;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    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,
afc163's avatar
afc163 committed
56
      margin = [24, 30, 16, 30] } = this.props;
57

afc163's avatar
afc163 committed
58 59 60
    const colors = [
      '#1890FF', '#FACC14', '#2FC25B', '#8543E0', '#F04864', '#13C2C2', '#fa8c16', '#a0d911',
    ];
niko's avatar
niko committed
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
    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,
afc163's avatar
afc163 committed
92 93 94 95 96 97 98 99 100 101 102 103 104
      labelOffset: 8,
      labels: {
        label: {
          fill: 'rgba(0, 0, 0, .65)',
        },
      },
      grid: {
        line: {
          stroke: '#e9e9e9',
          lineWidth: 1,
          lineDash: [0, 0],
        },
      },
105 106 107 108 109
    });

    chart.axis('value', {
      grid: {
        type: 'polygon',
afc163's avatar
afc163 committed
110 111 112 113 114 115 116 117 118 119
        line: {
          stroke: '#e9e9e9',
          lineWidth: 1,
          lineDash: [0, 0],
        },
      },
      labels: {
        label: {
          fill: 'rgba(0, 0, 0, .65)',
        },
120 121 122
      },
    });

niko's avatar
niko committed
123
    chart.line().position('label*value').color('name', colors);
afc163's avatar
afc163 committed
124 125
    chart.point().position('label*value').color('name', colors).shape('circle')
      .size(3);
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

    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>
afc163's avatar
afc163 committed
158
          {title && <h4>{title}</h4>}
159 160
          <div ref={this.handleRef} />
          {
afc163's avatar
afc163 committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
            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>
            )
182 183 184 185 186 187 188 189
          }
        </div>
      </div>
    );
  }
}

export default Radar;