index.js 3.82 KB
Newer Older
niko's avatar
niko committed
1 2 3 4 5 6
import React from 'react';
import { Chart, Geom, Axis, Coord, Guide, Shape } from 'bizcharts';
import autoHeight from '../autoHeight';

const { Arc, Html, Line } = Guide;

jim's avatar
jim committed
7
const defaultFormatter = val => {
niko's avatar
niko committed
8 9 10 11 12 13 14 15 16 17 18
  switch (val) {
    case '2':
      return 'ๅทฎ';
    case '4':
      return 'ไธญ';
    case '6':
      return '่‰ฏ';
    case '8':
      return 'ไผ˜';
    default:
      return '';
19
  }
niko's avatar
niko committed
20 21 22 23 24 25 26 27 28
};

Shape.registerShape('point', 'pointer', {
  drawShape(cfg, group) {
    let point = cfg.points[0];
    point = this.parsePoint(point);
    const center = this.parsePoint({
      x: 0,
      y: 0,
29
    });
niko's avatar
niko committed
30 31 32 33 34 35 36 37 38
    group.addShape('line', {
      attrs: {
        x1: center.x,
        y1: center.y,
        x2: point.x,
        y2: point.y,
        stroke: cfg.color,
        lineWidth: 2,
        lineCap: 'round',
39 40
      },
    });
niko's avatar
niko committed
41 42 43 44 45 46 47 48
    return group.addShape('circle', {
      attrs: {
        x: center.x,
        y: center.y,
        r: 6,
        stroke: cfg.color,
        lineWidth: 3,
        fill: '#fff',
49 50
      },
    });
niko's avatar
niko committed
51 52
  },
});
53

niko's avatar
niko committed
54
@autoHeight()
afc163's avatar
afc163 committed
55
class Gauge extends React.Component {
56
  render() {
niko's avatar
niko committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    const {
      title,
      height,
      percent,
      forceFit = true,
      formatter = defaultFormatter,
      color = '#2F9CFF',
      bgColor = '#F0F2F5',
    } = this.props;
    const cols = {
      value: {
        type: 'linear',
        min: 0,
        max: 10,
        tickCount: 6,
        nice: true,
      },
    };
75
    const data = [{ value: percent / 10 }];
76
    return (
niko's avatar
niko committed
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
      <Chart height={height} data={data} scale={cols} padding={[-16, 0, 16, 0]} forceFit={forceFit}>
        <Coord type="polar" startAngle={-1.25 * Math.PI} endAngle={0.25 * Math.PI} radius={0.8} />
        <Axis name="1" line={null} />
        <Axis
          line={null}
          tickLine={null}
          subTickLine={null}
          name="value"
          zIndex={2}
          gird={null}
          label={{
            offset: -12,
            formatter,
            textStyle: {
              fontSize: 12,
              fill: 'rgba(0, 0, 0, 0.65)',
              textAlign: 'center',
            },
          }}
        />
        <Guide>
          <Line
            start={[3, 0.905]}
            end={[3, 0.85]}
            lineStyle={{
              stroke: color,
              lineDash: null,
              lineWidth: 2,
            }}
          />
          <Line
            start={[5, 0.905]}
            end={[5, 0.85]}
            lineStyle={{
              stroke: color,
              lineDash: null,
              lineWidth: 3,
            }}
          />
          <Line
            start={[7, 0.905]}
            end={[7, 0.85]}
            lineStyle={{
              stroke: color,
              lineDash: null,
              lineWidth: 3,
            }}
          />
          <Arc
            zIndex={0}
            start={[0, 0.965]}
            end={[10, 0.965]}
            style={{
              stroke: bgColor,
              lineWidth: 10,
            }}
          />
          <Arc
            zIndex={1}
            start={[0, 0.965]}
137
            end={[data[0].value, 0.965]}
niko's avatar
niko committed
138 139 140 141 142 143 144
            style={{
              stroke: color,
              lineWidth: 10,
            }}
          />
          <Html
            position={['50%', '95%']}
145
            html={() => `
niko's avatar
niko committed
146 147 148
                <div style="width: 300px;text-align: center;font-size: 12px!important;">
                  <p style="font-size: 14px; color: rgba(0,0,0,0.43);margin: 0;">${title}</p>
                  <p style="font-size: 24px;color: rgba(0,0,0,0.85);margin: 0;">
chen shuai's avatar
chen shuai committed
149
                    ${(data[0].value * 10).toFixed(2)}%
niko's avatar
niko committed
150
                  </p>
151
                </div>`}
niko's avatar
niko committed
152 153 154 155 156 157 158 159 160 161 162
          />
        </Guide>
        <Geom
          line={false}
          type="point"
          position="value*1"
          shape="pointer"
          color={color}
          active={false}
        />
      </Chart>
163 164 165
    );
  }
}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
166 167

export default Gauge;