curve.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.Curve = void 0;
  15. const d3_shape_1 = require("d3-shape");
  16. const g_1 = require("@antv/g");
  17. const selection_1 = require("../../utils/selection");
  18. const coordinate_1 = require("../../utils/coordinate");
  19. const vector_1 = require("../../utils/vector");
  20. const utils_1 = require("../utils");
  21. const helper_1 = require("../../utils/helper");
  22. const createElement_1 = require("../../utils/createElement");
  23. const DoubleArea = (0, createElement_1.createElement)((g) => {
  24. const { areaPath, connectPath, areaStyle, connectStyle } = g.attributes;
  25. (0, selection_1.select)(g)
  26. .maybeAppend('connect-path', () => new g_1.Path({}))
  27. .style('d', connectPath)
  28. .call(utils_1.applyStyle, connectStyle);
  29. (0, selection_1.select)(g)
  30. .maybeAppend('area-path', () => new g_1.Path({}))
  31. .style('d', areaPath)
  32. .call(utils_1.applyStyle, areaStyle);
  33. });
  34. /**
  35. * Given a points sequence, split it into an array of defined points
  36. * and an array of undefined segments.
  37. *
  38. * Input - [p0, p1, p2, p3, p4, p5], p1 ~ p2 is `Y1`, p3 ~ p5 is `Y0`.
  39. * Output - When all of Y1 & Y0 is defined, move into defined points, or else undefined segments.
  40. */
  41. function segmentation(points, defined) {
  42. const definedPointsY1 = [];
  43. const definedPointsY0 = [];
  44. const segments = [];
  45. let m = false; // Is in a undefined sequence.
  46. let dp = null; // The previous defined point.
  47. const mid = points.length / 2;
  48. for (let i = 0; i < mid; i++) {
  49. const y1 = points[i];
  50. const y0 = points[i + mid];
  51. // If current point is a undefined point,
  52. // enter a undefined sequence.
  53. if ([...y1, ...y0].some((v) => !defined(v)))
  54. m = true;
  55. else {
  56. definedPointsY1.push(y1);
  57. definedPointsY0.push(y0);
  58. // If current point is a defined point,
  59. // and is in a undefined sequence, save
  60. // the two closest defined points as this
  61. // undefined sequence and exit it.
  62. if (m && dp) {
  63. m = false;
  64. const [dpy1, dpy0] = dp;
  65. segments.push([dpy1, y1, dpy0, y0]);
  66. }
  67. // Update the previous defined point.
  68. dp = [y1, y0];
  69. }
  70. }
  71. return [definedPointsY1.concat(definedPointsY0), segments];
  72. }
  73. const Curve = (options) => {
  74. const { curve, gradient = false, defined = (d) => !Number.isNaN(d) && d !== undefined && d !== null, connect: connectNulls = false } = options, style = __rest(options, ["curve", "gradient", "defined", "connect"]);
  75. return (P, value, coordinate, theme) => {
  76. const { mark, shape, defaultShape } = value;
  77. const _a = (0, utils_1.getShapeTheme)(theme, mark, shape, defaultShape), { defaultColor } = _a, defaults = __rest(_a, ["defaultColor"]);
  78. const { color = defaultColor, seriesColor: sc, seriesX: sx, seriesY: sy, } = value;
  79. const transform = (0, utils_1.getTransform)(coordinate, value);
  80. const fill = gradient && sc ? (0, utils_1.computeGradient)(sc, sx, sy, gradient) : color;
  81. const finalStyle = Object.assign(Object.assign(Object.assign(Object.assign({}, defaults), { stroke: fill, fill: fill }), (transform && { transform })), style);
  82. const [DP, MS] = segmentation(P, defined);
  83. const connectStyle = (0, helper_1.subObject)(finalStyle, 'connect');
  84. const missing = !!MS.length;
  85. const getPathNode = (path) => {
  86. return (0, selection_1.select)(new g_1.Path({}))
  87. .style('d', path)
  88. .call(utils_1.applyStyle, finalStyle)
  89. .node();
  90. };
  91. if (!(0, coordinate_1.isPolar)(coordinate)) {
  92. /**
  93. * Draw area shape by points.
  94. */
  95. const areaPath = (points) => {
  96. const Y1 = points.slice(0, points.length / 2);
  97. const Y0 = points.slice(points.length / 2);
  98. return (0, coordinate_1.isTranspose)(coordinate)
  99. ? (0, d3_shape_1.area)()
  100. .y((_, idx) => Y1[idx][1])
  101. .x1((_, idx) => Y1[idx][0])
  102. .x0((_, idx) => Y0[idx][0])
  103. .defined((_, idx) => [...Y1[idx], ...Y0[idx]].every(defined))
  104. .curve(curve)(Y1)
  105. : (0, d3_shape_1.area)()
  106. .x((_, idx) => Y1[idx][0])
  107. .y1((_, idx) => Y1[idx][1])
  108. .y0((_, idx) => Y0[idx][1])
  109. .defined((_, idx) => [...Y1[idx], ...Y0[idx]].every(defined))
  110. .curve(curve)(Y1);
  111. };
  112. // Draw one area of connected defined points.
  113. if (!missing || (connectNulls && !Object.keys(connectStyle).length)) {
  114. return getPathNode(areaPath(DP));
  115. }
  116. // Draw one area of unconnected defined points.
  117. if (missing && !connectNulls) {
  118. return getPathNode(areaPath(P));
  119. }
  120. // Draw two area.
  121. // One for unconnected defined points.
  122. // One for connected segments.
  123. return (0, selection_1.select)(new DoubleArea())
  124. .style('areaStyle', finalStyle)
  125. .style('connectStyle', Object.assign(Object.assign({}, connectStyle), style))
  126. .style('areaPath', areaPath(P))
  127. .style('connectPath', MS.map(areaPath).join(''))
  128. .node();
  129. }
  130. else {
  131. /**
  132. * Draw areaRadial shape by points.
  133. */
  134. const areaRadialPath = (points) => {
  135. const center = coordinate.getCenter();
  136. const Y1 = points.slice(0, points.length / 2);
  137. const Y0 = points.slice(points.length / 2);
  138. return (0, d3_shape_1.areaRadial)()
  139. .angle((_, idx) => (0, vector_1.angleWithQuadrant)((0, vector_1.sub)(Y1[idx], center)))
  140. .outerRadius((_, idx) => (0, vector_1.dist)(Y1[idx], center))
  141. .innerRadius((_, idx) => (0, vector_1.dist)(Y0[idx], center))
  142. .defined((_, idx) => [...Y1[idx], ...Y0[idx]].every(defined))
  143. .curve(curve)(Y0);
  144. };
  145. // Draw one area of connected defined points.
  146. if (!missing || (connectNulls && !Object.keys(connectStyle).length)) {
  147. return getPathNode(areaRadialPath(DP));
  148. }
  149. // Draw one area of unconnected defined points.
  150. if (missing && !connectNulls) {
  151. return getPathNode(areaRadialPath(P));
  152. }
  153. // Draw two area.
  154. // One for unconnected defined points.
  155. // One for connected segments.
  156. return (0, selection_1.select)(new DoubleArea())
  157. .style('areaStyle', finalStyle)
  158. .style('connectStyle', Object.assign(Object.assign({}, connectStyle), style))
  159. .style('areaPath', areaRadialPath(P))
  160. .style('connectPath', MS.map(areaRadialPath).join(''))
  161. .node();
  162. }
  163. };
  164. };
  165. exports.Curve = Curve;
  166. exports.Curve.props = {
  167. defaultMarker: 'smooth',
  168. defaultEnterAnimation: 'fadeIn',
  169. defaultUpdateAnimation: 'morphing',
  170. defaultExitAnimation: 'fadeOut',
  171. };
  172. //# sourceMappingURL=curve.js.map