|
| 1 | +import { Gauge, G2 } from '@antv/g2plot'; |
| 2 | + |
| 3 | +const { registerShape, Util } = G2; |
| 4 | + |
| 5 | +// 自定义 Shape 部分 |
| 6 | +registerShape('point', 'custom-gauge-indicator2', { |
| 7 | + draw(cfg, container) { |
| 8 | + // 使用 customInfo 传递参数 |
| 9 | + const { indicator, defaultColor } = cfg.customInfo; |
| 10 | + const { pointer, pin } = indicator; |
| 11 | + |
| 12 | + const group = container.addGroup(); |
| 13 | + // 获取极坐标系下画布中心点 |
| 14 | + const center = this.parsePoint({ x: 0, y: 0 }); |
| 15 | + // 绘制指针 |
| 16 | + if (pointer) { |
| 17 | + const { startAngle, endAngle } = Util.getAngle(cfg, this.coordinate); |
| 18 | + const radius = this.coordinate.getRadius(); |
| 19 | + const midAngle = (startAngle + endAngle) / 2; |
| 20 | + const { x: x1, y: y1 } = Util.polarToCartesian(center.x, center.y, radius / 15, midAngle + 1 / Math.PI); |
| 21 | + const { x: x2, y: y2 } = Util.polarToCartesian(center.x, center.y, radius / 15, midAngle - 1 / Math.PI); |
| 22 | + const { x, y } = Util.polarToCartesian(center.x, center.y, radius * 0.65, midAngle); |
| 23 | + const { x: x0, y: y0 } = Util.polarToCartesian(center.x, center.y, radius * 0.1, midAngle + Math.PI); |
| 24 | + |
| 25 | + const path = [['M', x0, y0], ['L', x1, y1], ['L', x, y], ['L', x2, y2], ['Z']]; |
| 26 | + // pointer |
| 27 | + group.addShape('path', { |
| 28 | + name: 'pointer', |
| 29 | + attrs: { |
| 30 | + path, |
| 31 | + fill: defaultColor, |
| 32 | + ...pointer.style, |
| 33 | + }, |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + if (pin) { |
| 38 | + const pinStyle = pin.style || {}; |
| 39 | + const { lineWidth = 2, fill = defaultColor, stroke = defaultColor } = pinStyle; |
| 40 | + const r = 6; |
| 41 | + group.addShape('circle', { |
| 42 | + name: 'pin-outer', |
| 43 | + attrs: { |
| 44 | + x: center.x, |
| 45 | + y: center.y, |
| 46 | + ...pin.style, |
| 47 | + fill: 'transparent', |
| 48 | + r: r * 1.5, |
| 49 | + lineWidth, |
| 50 | + stroke: stroke, |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + group.addShape('circle', { |
| 55 | + name: 'pin-inner', |
| 56 | + attrs: { |
| 57 | + x: center.x, |
| 58 | + y: center.y, |
| 59 | + r, |
| 60 | + stroke: 'transparent', |
| 61 | + fill, |
| 62 | + }, |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + return group; |
| 67 | + }, |
| 68 | +}); |
| 69 | + |
| 70 | +const gauge = new Gauge('container', { |
| 71 | + percent: 0.78, |
| 72 | + range: { |
| 73 | + color: '#30BF78', |
| 74 | + }, |
| 75 | + indicator: { |
| 76 | + shape: 'custom-gauge-indicator2', |
| 77 | + pointer: { |
| 78 | + style: { |
| 79 | + stroke: '#D0D0D0', |
| 80 | + lineWidth: 1, |
| 81 | + fill: '#D0D0D0', |
| 82 | + }, |
| 83 | + }, |
| 84 | + pin: { |
| 85 | + style: { |
| 86 | + lineWidth: 2, |
| 87 | + stroke: '#D0D0D0', |
| 88 | + fill: '#D0D0D0', |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | +}); |
| 93 | + |
| 94 | +gauge.render(); |
0 commit comments