violin.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.Violin = void 0;
  15. const g_1 = require("@antv/g");
  16. const d3_path_1 = require("d3-path");
  17. const utils_1 = require("../utils");
  18. const selection_1 = require("../../utils/selection");
  19. const coordinate_1 = require("../../utils/coordinate");
  20. const vector_1 = require("../../utils/vector");
  21. function getPath(p, coordinate, size = 4) {
  22. const path = (0, d3_path_1.path)();
  23. if (!(0, coordinate_1.isPolar)(coordinate)) {
  24. path.moveTo(...p[2]);
  25. path.lineTo(...p[3]);
  26. path.lineTo(p[3][0] - size, p[3][1]);
  27. path.lineTo(p[10][0] - size, p[10][1]);
  28. path.lineTo(p[10][0] + size, p[10][1]);
  29. path.lineTo(p[3][0] + size, p[3][1]);
  30. path.lineTo(...p[3]);
  31. path.closePath();
  32. path.moveTo(...p[10]);
  33. path.lineTo(...p[11]);
  34. path.moveTo(p[3][0] + size / 2, p[8][1]);
  35. path.arc(p[3][0], p[8][1], size / 2, 0, Math.PI * 2);
  36. path.closePath();
  37. return path;
  38. }
  39. const center = coordinate.getCenter();
  40. const [x, y] = center;
  41. const radiusQ3 = (0, vector_1.dist)(center, p[3]);
  42. const radiusMedian = (0, vector_1.dist)(center, p[8]);
  43. const radiusQ1 = (0, vector_1.dist)(center, p[10]);
  44. const middleAngle = (0, vector_1.angle)((0, vector_1.sub)(p[2], center));
  45. const rectAngle = Math.asin(size / radiusMedian);
  46. const startAngle = middleAngle - rectAngle;
  47. const endAngle = middleAngle + rectAngle;
  48. path.moveTo(...p[2]);
  49. path.lineTo(...p[3]);
  50. path.moveTo(Math.cos(startAngle) * radiusQ3 + x, Math.sin(startAngle) * radiusQ3 + y);
  51. path.arc(x, y, radiusQ3, startAngle, endAngle);
  52. path.lineTo(Math.cos(endAngle) * radiusQ1 + x, Math.sin(endAngle) * radiusQ1 + y);
  53. path.arc(x, y, radiusQ1, endAngle, startAngle, true);
  54. path.lineTo(Math.cos(startAngle) * radiusQ3 + x, Math.sin(startAngle) * radiusQ3 + y);
  55. path.closePath();
  56. path.moveTo(...p[10]);
  57. path.lineTo(...p[11]);
  58. const a = (startAngle + endAngle) / 2;
  59. path.moveTo(Math.cos(a) * (radiusMedian + size / 2) + x, Math.sin(a) * (radiusMedian + size / 2) + y);
  60. path.arc(Math.cos(a) * radiusMedian + x, Math.sin(a) * radiusMedian + y, size / 2, a, Math.PI * 2 + a);
  61. path.closePath();
  62. return path;
  63. }
  64. const Violin = (options) => {
  65. const style = __rest(options, []);
  66. return (points, value, coordinate, theme) => {
  67. const { mark, shape, defaultShape, color, transform } = value;
  68. // TODO: how to setting it by size channel.
  69. const size = 4;
  70. const _a = (0, utils_1.getShapeTheme)(theme, mark, shape, defaultShape), { defaultColor, fill = defaultColor, stroke = defaultColor } = _a, shapeTheme = __rest(_a, ["defaultColor", "fill", "stroke"]);
  71. const path = getPath(points, coordinate, size);
  72. return (0, selection_1.select)(new g_1.Path())
  73. .call(utils_1.applyStyle, shapeTheme)
  74. .style('d', path.toString())
  75. .style('stroke', stroke)
  76. .style('fill', color || fill)
  77. .style('transform', transform)
  78. .call(utils_1.applyStyle, style)
  79. .node();
  80. };
  81. };
  82. exports.Violin = Violin;
  83. exports.Violin.props = {
  84. defaultMarker: 'point',
  85. defaultEnterAnimation: 'fadeIn',
  86. defaultUpdateAnimation: 'morphing',
  87. defaultExitAnimation: 'fadeOut',
  88. };
  89. //# sourceMappingURL=violin.js.map