index.js 4.14 KB
Newer Older
niko's avatar
niko committed
1 2
import React, { Component } from 'react';
import { Chart, Tooltip, Geom, Coord, Axis } from 'bizcharts';
3
import { Row, Col } from 'antd';
niko's avatar
niko committed
4
import autoHeight from '../autoHeight';
5 6 7
import styles from './index.less';

/* eslint react/no-danger:0 */
niko's avatar
niko committed
8 9
@autoHeight()
export default class Radar extends Component {
10 11
  state = {
    legendData: [],
niko's avatar
niko committed
12
  };
13 14

  componentDidMount() {
15
    this.getLegendData();
16 17
  }

jim's avatar
jim committed
18 19
  componentDidUpdate(preProps) {
    if (this.props.data !== preProps.data) {
jim's avatar
jim committed
20
      this.getLegendData();
21 22 23
    }
  }

jim's avatar
jim committed
24
  getG2Instance = chart => {
niko's avatar
niko committed
25 26 27 28
    this.chart = chart;
  };

  // for custom lengend view
jim's avatar
jim committed
29
  getLegendData = () => {
niko's avatar
niko committed
30 31
    if (!this.chart) return;
    const geom = this.chart.getAllGeoms()[0]; // θŽ·ε–ζ‰€ζœ‰ηš„ε›Ύε½’
wangyun's avatar
wangyun committed
32
    if (!geom) return;
niko's avatar
niko committed
33 34
    const items = geom.get('dataArray') || []; // θŽ·ε–ε›Ύε½’ε―ΉεΊ”ηš„

jim's avatar
jim committed
35
    const legendData = items.map(item => {
niko's avatar
niko committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      // 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,
    });
  };
52

jim's avatar
jim committed
53
  handleRef = n => {
54
    this.node = n;
niko's avatar
niko committed
55
  };
56 57 58 59 60

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

afc163's avatar
afc163 committed
61
    const { legendData } = this.state;
62 63
    legendData[i] = newItem;

jim's avatar
jim committed
64
    const filteredLegendData = legendData.filter(l => l.checked).map(l => l.name);
niko's avatar
niko committed
65

66
    if (this.chart) {
niko's avatar
niko committed
67
      this.chart.filter('name', val => filteredLegendData.indexOf(val) > -1);
68 69 70 71 72 73
      this.chart.repaint();
    }

    this.setState({
      legendData,
    });
niko's avatar
niko committed
74
  };
75

niko's avatar
niko committed
76 77 78 79 80 81 82 83 84 85
  render() {
    const defaultColors = [
      '#1890FF',
      '#FACC14',
      '#2FC25B',
      '#8543E0',
      '#F04864',
      '#13C2C2',
      '#fa8c16',
      '#a0d911',
afc163's avatar
afc163 committed
86
    ];
niko's avatar
niko committed
87

niko's avatar
niko committed
88 89 90 91 92 93 94 95 96 97 98
    const {
      data = [],
      height = 0,
      title,
      hasLegend = false,
      forceFit = true,
      tickCount = 4,
      padding = [35, 30, 16, 30],
      animate = true,
      colors = defaultColors,
    } = this.props;
99

niko's avatar
niko committed
100
    const { legendData } = this.state;
101

niko's avatar
niko committed
102
    const scale = {
103 104 105 106
      value: {
        min: 0,
        tickCount,
      },
niko's avatar
niko committed
107
    };
108

niko's avatar
niko committed
109
    const chartHeight = height - (hasLegend ? 80 : 22);
110 111 112

    return (
      <div className={styles.radar} style={{ height }}>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
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
        {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={null}
            tickLine={null}
            grid={{
              lineStyle: {
                lineDash: null,
              },
              hideFirstLine: false,
            }}
          />
          <Axis
            name="value"
            grid={{
              type: 'polygon',
              lineStyle: {
                lineDash: null,
              },
            }}
          />
jim's avatar
jim committed
145
          <Geom type="line" position="label*value" color={['name', colors]} size={1} />
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
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
          <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>
jim's avatar
jim committed
175
            ))}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
176 177
          </Row>
        )}
178 179 180 181
      </div>
    );
  }
}