index.js 4.2 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() {
niko's avatar
niko committed
15
    this.getLengendData();
16 17 18
  }

  componentWillReceiveProps(nextProps) {
niko's avatar
niko committed
19 20
    if (this.props.data !== nextProps.data) {
      this.getLengendData();
21 22 23
    }
  }

niko's avatar
niko committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  getG2Instance = (chart) => {
    this.chart = chart;
  };

  // for custom lengend view
  getLengendData = () => {
    if (!this.chart) return;
    const geom = this.chart.getAllGeoms()[0]; // θŽ·ε–ζ‰€ζœ‰ηš„ε›Ύε½’
    const items = geom.get('dataArray') || []; // θŽ·ε–ε›Ύε½’ε―ΉεΊ”ηš„

    const legendData = items.map((item) => {
      // 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,
    });
  };
51

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

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

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

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
63 64 65
    const filteredLegendData = legendData
      .filter(l => l.checked)
      .map(l => l.name);
niko's avatar
niko committed
66

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

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

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

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

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

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

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

    return (
      <div className={styles.radar} style={{ height }}>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
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
        {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,
              },
            }}
          />
          <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>
        )}
184 185 186 187
      </div>
    );
  }
}