violin.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var __rest = (this && this.__rest) || function (s, e) {
  2. var t = {};
  3. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  4. t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  6. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  8. t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { Path as GPath } from '@antv/g';
  13. import { path as d3path } from 'd3-path';
  14. import { applyStyle, getShapeTheme } from '../utils';
  15. import { select } from '../../utils/selection';
  16. import { isPolar } from '../../utils/coordinate';
  17. import { angle, sub, dist } from '../../utils/vector';
  18. function getPath(p, coordinate, size = 4) {
  19. const path = d3path();
  20. if (!isPolar(coordinate)) {
  21. path.moveTo(...p[2]);
  22. path.lineTo(...p[3]);
  23. path.lineTo(p[3][0] - size, p[3][1]);
  24. path.lineTo(p[10][0] - size, p[10][1]);
  25. path.lineTo(p[10][0] + size, p[10][1]);
  26. path.lineTo(p[3][0] + size, p[3][1]);
  27. path.lineTo(...p[3]);
  28. path.closePath();
  29. path.moveTo(...p[10]);
  30. path.lineTo(...p[11]);
  31. path.moveTo(p[3][0] + size / 2, p[8][1]);
  32. path.arc(p[3][0], p[8][1], size / 2, 0, Math.PI * 2);
  33. path.closePath();
  34. return path;
  35. }
  36. const center = coordinate.getCenter();
  37. const [x, y] = center;
  38. const radiusQ3 = dist(center, p[3]);
  39. const radiusMedian = dist(center, p[8]);
  40. const radiusQ1 = dist(center, p[10]);
  41. const middleAngle = angle(sub(p[2], center));
  42. const rectAngle = Math.asin(size / radiusMedian);
  43. const startAngle = middleAngle - rectAngle;
  44. const endAngle = middleAngle + rectAngle;
  45. path.moveTo(...p[2]);
  46. path.lineTo(...p[3]);
  47. path.moveTo(Math.cos(startAngle) * radiusQ3 + x, Math.sin(startAngle) * radiusQ3 + y);
  48. path.arc(x, y, radiusQ3, startAngle, endAngle);
  49. path.lineTo(Math.cos(endAngle) * radiusQ1 + x, Math.sin(endAngle) * radiusQ1 + y);
  50. path.arc(x, y, radiusQ1, endAngle, startAngle, true);
  51. path.lineTo(Math.cos(startAngle) * radiusQ3 + x, Math.sin(startAngle) * radiusQ3 + y);
  52. path.closePath();
  53. path.moveTo(...p[10]);
  54. path.lineTo(...p[11]);
  55. const a = (startAngle + endAngle) / 2;
  56. path.moveTo(Math.cos(a) * (radiusMedian + size / 2) + x, Math.sin(a) * (radiusMedian + size / 2) + y);
  57. path.arc(Math.cos(a) * radiusMedian + x, Math.sin(a) * radiusMedian + y, size / 2, a, Math.PI * 2 + a);
  58. path.closePath();
  59. return path;
  60. }
  61. export const Violin = (options) => {
  62. const style = __rest(options, []);
  63. return (points, value, coordinate, theme) => {
  64. const { mark, shape, defaultShape, color, transform } = value;
  65. // TODO: how to setting it by size channel.
  66. const size = 4;
  67. const _a = getShapeTheme(theme, mark, shape, defaultShape), { defaultColor, fill = defaultColor, stroke = defaultColor } = _a, shapeTheme = __rest(_a, ["defaultColor", "fill", "stroke"]);
  68. const path = getPath(points, coordinate, size);
  69. return select(new GPath())
  70. .call(applyStyle, shapeTheme)
  71. .style('d', path.toString())
  72. .style('stroke', stroke)
  73. .style('fill', color || fill)
  74. .style('transform', transform)
  75. .call(applyStyle, style)
  76. .node();
  77. };
  78. };
  79. Violin.props = {
  80. defaultMarker: 'point',
  81. defaultEnterAnimation: 'fadeIn',
  82. defaultUpdateAnimation: 'morphing',
  83. defaultExitAnimation: 'fadeOut',
  84. };
  85. //# sourceMappingURL=violin.js.map