gauge.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use strict";
  2. var __rest = (this && this.__rest) || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  5. t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  7. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  9. t[p[i]] = s[p[i]];
  10. }
  11. return t;
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.Gauge = void 0;
  15. const util_1 = require("@antv/util");
  16. const g_1 = require("@antv/g");
  17. const helper_1 = require("../utils/helper");
  18. const coordinate_1 = require("../utils/coordinate");
  19. const coordinate_2 = require("../coordinate");
  20. const utils_1 = require("../shape/utils");
  21. const selection_1 = require("../utils/selection");
  22. function dataTransform(data, scale) {
  23. const { name = 'score', target, total, percent, thresholds = [] } = data;
  24. const _target = percent || target;
  25. const _total = percent ? 1 : total;
  26. const newScale = Object.assign({ y: {
  27. domain: [0, _total],
  28. } }, scale);
  29. if (!thresholds.length) {
  30. return {
  31. targetData: [{ x: name, y: _target, color: 'target' }],
  32. totalData: [
  33. { x: name, y: _target, color: 'target' },
  34. { x: name, y: _total - _target, color: 'total' },
  35. ],
  36. target: _target,
  37. total: _total,
  38. scale: newScale,
  39. };
  40. }
  41. return {
  42. targetData: [{ x: name, y: _target, color: 'target' }],
  43. totalData: thresholds.map((d, i) => ({
  44. x: name,
  45. y: i >= 1 ? d - thresholds[i - 1] : d,
  46. color: i,
  47. })),
  48. target: _target,
  49. total: _total,
  50. scale: newScale,
  51. };
  52. }
  53. function getTextContent(textStyle, { target, total }) {
  54. const { content } = textStyle;
  55. return content ? content(target, total) : target.toString();
  56. }
  57. const indicatorShape = (options) => {
  58. const { shape, radius } = options, style = __rest(options, ["shape", "radius"]);
  59. const pointerStyle = (0, helper_1.subObject)(style, 'pointer');
  60. const pinStyle = (0, helper_1.subObject)(style, 'pin');
  61. const { shape: pointerShape } = pointerStyle, resPointerStyle = __rest(pointerStyle, ["shape"]);
  62. const { shape: pinShape } = pinStyle, resPinStyle = __rest(pinStyle, ["shape"]);
  63. return (points, value, coordinate, theme) => {
  64. // Invert points.
  65. const invertedPoints = points.map((p) => coordinate.invert(p));
  66. // Get new coordinate.
  67. const [startAngle, endAngle, innerRadius] = (0, coordinate_1.getTransformOptions)(coordinate, 'polar');
  68. const newCoordinate = coordinate.clone();
  69. const { color: stroke } = value;
  70. const newTransformations = (0, coordinate_2.Radial)({
  71. startAngle,
  72. endAngle,
  73. innerRadius,
  74. outerRadius: radius,
  75. });
  76. newTransformations.push(['cartesian']);
  77. newCoordinate.update({
  78. transformations: newTransformations,
  79. });
  80. const newPoints = invertedPoints.map((p) => newCoordinate.map(p));
  81. const [x, y] = (0, utils_1.getOrigin)(newPoints);
  82. const [cx, cy] = coordinate.getCenter();
  83. const pointerAttrs = Object.assign(Object.assign({ x1: x, y1: y, x2: cx, y2: cy, stroke }, resPointerStyle), style);
  84. const pinAttrs = Object.assign(Object.assign({ cx,
  85. cy,
  86. stroke }, resPinStyle), style);
  87. const indicatorGroup = (0, selection_1.select)(new g_1.Group());
  88. if (!(0, helper_1.isUnset)(pointerShape)) {
  89. typeof pointerShape === 'function'
  90. ? indicatorGroup.append(() => pointerShape(newPoints, value, newCoordinate, theme))
  91. : indicatorGroup.append('line').call(utils_1.applyStyle, pointerAttrs).node();
  92. }
  93. if (!(0, helper_1.isUnset)(pinShape)) {
  94. typeof pinShape === 'function'
  95. ? indicatorGroup.append(() => pinShape(newPoints, value, newCoordinate, theme))
  96. : indicatorGroup.append('circle').call(utils_1.applyStyle, pinAttrs).node();
  97. }
  98. return indicatorGroup.node();
  99. };
  100. };
  101. const Gauge = (options) => {
  102. const DEFAULT_OPTIONS = {
  103. coordinate: {
  104. type: 'radial',
  105. innerRadius: 0.9,
  106. outerRadius: 1,
  107. startAngle: (-11 / 10) * Math.PI,
  108. endAngle: (1 / 10) * Math.PI,
  109. },
  110. axis: {
  111. x: false,
  112. },
  113. legend: false,
  114. tooltip: false,
  115. encode: {
  116. x: 'x',
  117. y: 'y',
  118. color: 'color',
  119. },
  120. scale: {
  121. color: {
  122. range: ['#30BF78', '#D0D0D0'],
  123. },
  124. },
  125. };
  126. const DEFAULT_INDICATOR_OPTIONS = {
  127. style: {
  128. shape: indicatorShape,
  129. lineWidth: 4,
  130. pointerLineCap: 'round',
  131. pinR: 10,
  132. pinFill: '#fff',
  133. radius: 0.6,
  134. },
  135. };
  136. const DEFAULT_TEXT_OPTIONS = {
  137. type: 'text',
  138. style: {
  139. x: '50%',
  140. y: '60%',
  141. textAlign: 'center',
  142. textBaseline: 'middle',
  143. fontSize: 20,
  144. fontWeight: 800,
  145. fill: '#888',
  146. },
  147. };
  148. return () => {
  149. const { data = {}, scale = {}, style = {}, animate = {}, transform = [] } = options, resOptions = __rest(options, ["data", "scale", "style", "animate", "transform"]);
  150. const { targetData, totalData, target, total, scale: newScale, } = dataTransform(data, scale);
  151. const textStyle = (0, helper_1.subObject)(style, 'text');
  152. // pointer + pin
  153. const indicatorStyle = (0, helper_1.filterPrefixObject)(style, ['pointer', 'pin']);
  154. return [
  155. (0, util_1.deepMix)({}, DEFAULT_OPTIONS, Object.assign({ type: 'interval', transform: [{ type: 'stackY' }], data: totalData, scale: newScale, style: (0, helper_1.subObject)(style, 'arc'), animate: typeof animate === 'object' ? (0, helper_1.subObject)(animate, 'arc') : animate }, resOptions)),
  156. (0, util_1.deepMix)({}, DEFAULT_OPTIONS, DEFAULT_INDICATOR_OPTIONS, Object.assign({ type: 'point', data: targetData, scale: newScale, style: indicatorStyle, animate: typeof animate === 'object'
  157. ? (0, helper_1.subObject)(animate, 'indicator')
  158. : animate }, resOptions)),
  159. (0, util_1.deepMix)({}, DEFAULT_TEXT_OPTIONS, {
  160. style: Object.assign({ text: getTextContent(textStyle, { target, total }) }, textStyle),
  161. animate: typeof animate === 'object' ? (0, helper_1.subObject)(animate, 'text') : animate,
  162. }),
  163. ];
  164. };
  165. };
  166. exports.Gauge = Gauge;
  167. exports.Gauge.props = {};
  168. //# sourceMappingURL=gauge.js.map