index.js 4.14 KB
Newer Older
1 2
import React, { PureComponent } from 'react';
import G2 from 'g2';
nikogu's avatar
nikogu committed
3
import equal from '../equal';
4

afc163's avatar
afc163 committed
5
const { Shape } = G2;
6

afc163's avatar
afc163 committed
7 8 9
const primaryColor = '#2F9CFF';
const backgroundColor = '#F0F2F5';

10 11 12
/* eslint no-underscore-dangle: 0 */
class Gauge extends PureComponent {
  componentDidMount() {
afc163's avatar
afc163 committed
13 14 15
    setTimeout(() => {
      this.renderChart();
    }, 10);
16 17 18
  }

  componentWillReceiveProps(nextProps) {
nikogu's avatar
nikogu committed
19
    if (!equal(this.props, nextProps)) {
afc163's avatar
afc163 committed
20 21 22
      setTimeout(() => {
        this.renderChart(nextProps);
      }, 10);
nikogu's avatar
nikogu committed
23
    }
24 25
  }

nikogu's avatar
nikogu committed
26 27 28 29 30 31
  componentWillUnmount() {
    if (this.chart) {
      this.chart.destroy();
    }
  }

32 33 34 35 36
  handleRef = (n) => {
    this.node = n;
  }

  initChart(nextProps) {
afc163's avatar
afc163 committed
37
    const { title, color = primaryColor } = nextProps || this.props;
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

    Shape.registShape('point', 'dashBoard', {
      drawShape(cfg, group) {
        const originPoint = cfg.points[0];
        const point = this.parsePoint({ x: originPoint.x, y: 0.4 });

        const center = this.parsePoint({
          x: 0,
          y: 0,
        });

        const shape = group.addShape('polygon', {
          attrs: {
            points: [
              [center.x, center.y],
              [point.x + 8, point.y],
              [point.x + 8, point.y - 2],
              [center.x, center.y - 2],
            ],
            radius: 2,
            lineWidth: 2,
            arrow: false,
            fill: color,
          },
        });

        group.addShape('Marker', {
          attrs: {
            symbol: 'circle',
            lineWidth: 2,
            fill: color,
            radius: 8,
            x: center.x,
            y: center.y,
          },
        });
        group.addShape('Marker', {
          attrs: {
            symbol: 'circle',
            lineWidth: 2,
            fill: '#fff',
            radius: 5,
            x: center.x,
            y: center.y,
          },
        });

afc163's avatar
afc163 committed
85
        const { origin } = cfg;
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
        group.addShape('text', {
          attrs: {
            x: center.x,
            y: center.y + 80,
            text: `${origin._origin.value}%`,
            textAlign: 'center',
            fontSize: 24,
            fill: 'rgba(0, 0, 0, 0.85)',
          },
        });
        group.addShape('text', {
          attrs: {
            x: center.x,
            y: center.y + 45,
            text: title,
            textAlign: 'center',
            fontSize: 14,
            fill: 'rgba(0, 0, 0, 0.43)',
          },
        });

        return shape;
      },
    });
  }

  renderChart(nextProps) {
afc163's avatar
afc163 committed
113 114 115
    const {
      height, color = primaryColor, bgColor = backgroundColor, title, percent, format,
    } = nextProps || this.props;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    const data = [{ name: title, value: percent }];

    if (this.chart) {
      this.chart.clear();
    }
    if (this.node) {
      this.node.innerHTML = '';
    }

    this.initChart(nextProps);

    const chart = new G2.Chart({
      container: this.node,
      forceFit: true,
      height,
      animate: false,
      plotCfg: {
niko's avatar
niko committed
133
        margin: [10, 10, 30, 10],
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
      },
    });

    chart.source(data);

    chart.tooltip(false);

    chart.coord('gauge', {
      startAngle: -1.2 * Math.PI,
      endAngle: 0.20 * Math.PI,
    });
    chart.col('value', {
      type: 'linear',
      nice: true,
      min: 0,
      max: 100,
      tickCount: 6,
    });
    chart.axis('value', {
afc163's avatar
afc163 committed
153
      subTick: false,
154 155
      tickLine: {
        stroke: color,
afc163's avatar
afc163 committed
156 157
        lineWidth: 2,
        value: -14,
158 159
      },
      labelOffset: -12,
niko's avatar
niko committed
160
      formatter: format,
161 162 163 164 165 166 167
    });
    chart.point().position('value').shape('dashBoard');
    draw(data);

    /* eslint no-shadow: 0 */
    function draw(data) {
      const val = data[0].value;
afc163's avatar
afc163 committed
168
      const lineWidth = 12;
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
      chart.guide().clear();

      chart.guide().arc(() => {
        return [0, 0.95];
      }, () => {
        return [val, 0.95];
      }, {
        stroke: color,
        lineWidth,
      });

      chart.guide().arc(() => {
        return [val, 0.95];
      }, (arg) => {
        return [arg.max, 0.95];
      }, {
        stroke: bgColor,
        lineWidth,
      });

      chart.changeData(data);
    }

    this.chart = chart;
  }

  render() {
    return (
      <div ref={this.handleRef} />
    );
  }
}

export default Gauge;