index.tsx 3.94 KB
Newer Older
陈帅's avatar
陈帅 committed
1 2
import { Axis, Chart, Coord, Geom, Guide, Shape } from 'bizcharts';

陈帅's avatar
陈帅 committed
3 4 5 6 7
import React from 'react';
import autoHeight from '../autoHeight';

const { Arc, Html, Line } = Guide;

陈帅's avatar
陈帅 committed
8
export interface GaugeProps {
陈帅's avatar
陈帅 committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  title: React.ReactNode;
  color?: string;
  height?: number;
  bgColor?: number;
  percent: number;
  forceFit?: boolean;
  style?: React.CSSProperties;
  formatter?: (value: string) => string;
}

const defaultFormatter = (val: string): string => {
  switch (val) {
    case '2':
      return '';
    case '4':
      return '';
    case '6':
      return '';
    case '8':
      return '';
    default:
      return '';
  }
};

陈帅's avatar
陈帅 committed
34 35 36 37 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 85 86 87
if (Shape.registerShape) {
  Shape.registerShape('point', 'pointer', {
    drawShape(cfg: any, group: any) {
      let point = cfg.points[0];
      point = (this as any).parsePoint(point);
      const center = (this as any).parsePoint({
        x: 0,
        y: 0,
      });
      group.addShape('line', {
        attrs: {
          x1: center.x,
          y1: center.y,
          x2: point.x,
          y2: point.y,
          stroke: cfg.color,
          lineWidth: 2,
          lineCap: 'round',
        },
      });
      return group.addShape('circle', {
        attrs: {
          x: center.x,
          y: center.y,
          r: 6,
          stroke: cfg.color,
          lineWidth: 3,
          fill: '#fff',
        },
      });
    },
  });
}

const Gauge: React.FC<GaugeProps> = props => {
  const {
    title,
    height = 1,
    percent,
    forceFit = true,
    formatter = defaultFormatter,
    color = '#2F9CFF',
    bgColor = '#F0F2F5',
  } = props;
  const cols = {
    value: {
      type: 'linear',
      min: 0,
      max: 10,
      tickCount: 6,
      nice: true,
    },
  };
  const data = [{ value: percent / 10 }];
陈帅's avatar
陈帅 committed
88

陈帅's avatar
陈帅 committed
89
  const renderHtml = () => `
陈帅's avatar
陈帅 committed
90 91 92 93 94 95
    <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;">
        ${(data[0].value * 10).toFixed(2)}%
      </p>
    </div>`;
陈帅's avatar
陈帅 committed
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
  const textStyle: {
    fontSize: number;
    fill: string;
    textAlign: 'center';
  } = {
    fontSize: 12,
    fill: 'rgba(0, 0, 0, 0.65)',
    textAlign: 'center',
  };
  return (
    <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={undefined} />
      <Axis
        line={undefined}
        tickLine={undefined}
        subTickLine={undefined}
        name="value"
        zIndex={2}
        label={{
          offset: -12,
          formatter,
          textStyle,
        }}
      />
      <Guide>
        <Line
          start={[3, 0.905]}
          end={[3, 0.85]}
          lineStyle={{
            stroke: color,
            lineDash: undefined,
            lineWidth: 2,
陈帅's avatar
陈帅 committed
129 130
          }}
        />
陈帅's avatar
陈帅 committed
131 132 133 134 135 136 137 138
        <Line
          start={[5, 0.905]}
          end={[5, 0.85]}
          lineStyle={{
            stroke: color,
            lineDash: undefined,
            lineWidth: 3,
          }}
陈帅's avatar
陈帅 committed
139
        />
陈帅's avatar
陈帅 committed
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
        <Line
          start={[7, 0.905]}
          end={[7, 0.85]}
          lineStyle={{
            stroke: color,
            lineDash: undefined,
            lineWidth: 3,
          }}
        />
        <Arc
          start={[0, 0.965]}
          end={[10, 0.965]}
          style={{
            stroke: bgColor,
            lineWidth: 10,
          }}
        />
        <Arc
          start={[0, 0.965]}
          end={[data[0].value, 0.965]}
          style={{
            stroke: color,
            lineWidth: 10,
          }}
        />
        <Html position={['50%', '95%']} html={renderHtml()} />
      </Guide>
      <Geom
        line={false}
        type="point"
        position="value*1"
        shape="pointer"
        color={color}
        active={false}
      />
    </Chart>
  );
};
陈帅's avatar
陈帅 committed
178 179

export default autoHeight()(Gauge);