index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import React, { PureComponent } from 'react';
  2. import G2 from 'g2';
  3. import equal from '../equal';
  4. const { Shape } = G2;
  5. const primaryColor = '#2F9CFF';
  6. const backgroundColor = '#F0F2F5';
  7. /* eslint no-underscore-dangle: 0 */
  8. class Gauge extends PureComponent {
  9. componentDidMount() {
  10. setTimeout(() => {
  11. this.renderChart();
  12. }, 10);
  13. }
  14. componentWillReceiveProps(nextProps) {
  15. if (!equal(this.props, nextProps)) {
  16. setTimeout(() => {
  17. this.renderChart(nextProps);
  18. }, 10);
  19. }
  20. }
  21. componentWillUnmount() {
  22. if (this.chart) {
  23. this.chart.destroy();
  24. }
  25. }
  26. handleRef = (n) => {
  27. this.node = n;
  28. }
  29. initChart(nextProps) {
  30. const { title, color = primaryColor } = nextProps || this.props;
  31. Shape.registShape('point', 'dashBoard', {
  32. drawShape(cfg, group) {
  33. const originPoint = cfg.points[0];
  34. const point = this.parsePoint({ x: originPoint.x, y: 0.4 });
  35. const center = this.parsePoint({
  36. x: 0,
  37. y: 0,
  38. });
  39. const shape = group.addShape('polygon', {
  40. attrs: {
  41. points: [
  42. [center.x, center.y],
  43. [point.x + 8, point.y],
  44. [point.x + 8, point.y - 2],
  45. [center.x, center.y - 2],
  46. ],
  47. radius: 2,
  48. lineWidth: 2,
  49. arrow: false,
  50. fill: color,
  51. },
  52. });
  53. group.addShape('Marker', {
  54. attrs: {
  55. symbol: 'circle',
  56. lineWidth: 2,
  57. fill: color,
  58. radius: 8,
  59. x: center.x,
  60. y: center.y,
  61. },
  62. });
  63. group.addShape('Marker', {
  64. attrs: {
  65. symbol: 'circle',
  66. lineWidth: 2,
  67. fill: '#fff',
  68. radius: 5,
  69. x: center.x,
  70. y: center.y,
  71. },
  72. });
  73. const { origin } = cfg;
  74. group.addShape('text', {
  75. attrs: {
  76. x: center.x,
  77. y: center.y + 80,
  78. text: `${origin._origin.value}%`,
  79. textAlign: 'center',
  80. fontSize: 24,
  81. fill: 'rgba(0, 0, 0, 0.85)',
  82. },
  83. });
  84. group.addShape('text', {
  85. attrs: {
  86. x: center.x,
  87. y: center.y + 45,
  88. text: title,
  89. textAlign: 'center',
  90. fontSize: 14,
  91. fill: 'rgba(0, 0, 0, 0.43)',
  92. },
  93. });
  94. return shape;
  95. },
  96. });
  97. }
  98. renderChart(nextProps) {
  99. const {
  100. height, color = primaryColor, bgColor = backgroundColor, title, percent, format,
  101. } = nextProps || this.props;
  102. const data = [{ name: title, value: percent }];
  103. if (this.chart) {
  104. this.chart.clear();
  105. }
  106. if (this.node) {
  107. this.node.innerHTML = '';
  108. }
  109. this.initChart(nextProps);
  110. const chart = new G2.Chart({
  111. container: this.node,
  112. forceFit: true,
  113. height,
  114. animate: false,
  115. plotCfg: {
  116. margin: [10, 10, 30, 10],
  117. },
  118. });
  119. chart.source(data);
  120. chart.tooltip(false);
  121. chart.coord('gauge', {
  122. startAngle: -1.2 * Math.PI,
  123. endAngle: 0.20 * Math.PI,
  124. });
  125. chart.col('value', {
  126. type: 'linear',
  127. nice: true,
  128. min: 0,
  129. max: 100,
  130. tickCount: 6,
  131. });
  132. chart.axis('value', {
  133. subTick: false,
  134. tickLine: {
  135. stroke: color,
  136. lineWidth: 2,
  137. value: -14,
  138. },
  139. labelOffset: -12,
  140. formatter: format,
  141. });
  142. chart.point().position('value').shape('dashBoard');
  143. draw(data);
  144. /* eslint no-shadow: 0 */
  145. function draw(data) {
  146. const val = data[0].value;
  147. const lineWidth = 12;
  148. chart.guide().clear();
  149. chart.guide().arc(() => {
  150. return [0, 0.95];
  151. }, () => {
  152. return [val, 0.95];
  153. }, {
  154. stroke: color,
  155. lineWidth,
  156. });
  157. chart.guide().arc(() => {
  158. return [val, 0.95];
  159. }, (arg) => {
  160. return [arg.max, 0.95];
  161. }, {
  162. stroke: bgColor,
  163. lineWidth,
  164. });
  165. chart.changeData(data);
  166. }
  167. this.chart = chart;
  168. }
  169. render() {
  170. return (
  171. <div ref={this.handleRef} />
  172. );
  173. }
  174. }
  175. export default Gauge;