interval.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Interval = void 0;
  4. const utils_1 = require("./utils");
  5. function bandWidth(scale, x) {
  6. return scale.getBandWidth(scale.invert(x));
  7. }
  8. /**
  9. * Convert value for each channel to rect shapes.
  10. * p0 p1
  11. * ┌────┐
  12. * │ │
  13. * │ │
  14. * p3 └────┘ p2
  15. */
  16. const Interval = () => {
  17. return (index, scale, value, coordinate) => {
  18. const { x: X, y: Y, y1: Y1, series: S, size: SZ } = value;
  19. // Calc width for each interval.
  20. // The scales for x and series channels must be band scale.
  21. const x = scale.x;
  22. const series = scale.series;
  23. const [width] = coordinate.getSize();
  24. const NSZ = SZ ? SZ.map((d) => +d / width) : null;
  25. const x1x2 = !SZ
  26. ? (x, w, i) => [x, x + w]
  27. : (x, w, i) => {
  28. const mx = x + w / 2;
  29. const s = NSZ[i];
  30. return [mx - s / 2, mx + s / 2];
  31. };
  32. // Calc the points of bounding box for the interval.
  33. // They are start from left-top corner in clock wise order.
  34. const P = Array.from(index, (i) => {
  35. const groupWidth = bandWidth(x, X[i]);
  36. const ratio = series ? bandWidth(series, S === null || S === void 0 ? void 0 : S[i]) : 1;
  37. const width = groupWidth * ratio;
  38. const offset = (+(S === null || S === void 0 ? void 0 : S[i]) || 0) * groupWidth;
  39. const x0 = +X[i] + offset;
  40. const [x1, x2] = x1x2(x0, width, i);
  41. const y1 = +Y[i];
  42. const y2 = +Y1[i];
  43. const p1 = [x1, y1];
  44. const p2 = [x2, y1];
  45. const p3 = [x2, y2];
  46. const p4 = [x1, y2];
  47. return [p1, p2, p3, p4].map((d) => coordinate.map(d));
  48. });
  49. return [index, P];
  50. };
  51. };
  52. exports.Interval = Interval;
  53. const shapes = ['rect', 'hollow', 'funnel', 'pyramid'];
  54. exports.Interval.props = {
  55. defaultShape: 'rect',
  56. defaultLabelShape: 'label',
  57. composite: false,
  58. channels: [
  59. ...(0, utils_1.baseGeometryChannels)({ shapes }),
  60. { name: 'x', scale: 'band', required: true },
  61. { name: 'y', required: true },
  62. { name: 'series', scale: 'band' },
  63. { name: 'size' },
  64. ],
  65. preInference: [
  66. ...(0, utils_1.basePreInference)(),
  67. { type: 'maybeZeroY1' },
  68. { type: 'maybeZeroX' },
  69. ],
  70. postInference: [...(0, utils_1.basePostInference)(), ...(0, utils_1.tooltip1d)()],
  71. interaction: {
  72. shareTooltip: true,
  73. },
  74. };
  75. //# sourceMappingURL=interval.js.map