box.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Box = void 0;
  4. const utils_1 = require("./utils");
  5. /**
  6. * Convert value for each channel to box shapes.
  7. *
  8. * p0 p2 p1
  9. * ──────────┬──────────
  10. * │
  11. * │
  12. * │
  13. * │
  14. * │
  15. * │
  16. * │ p3
  17. * p4 ┌─────────┴──────────┐ p5
  18. * │ │
  19. * │ │
  20. * p8 ├────────────────────┤ p9
  21. * │ │
  22. * │ p10 │
  23. * p7 └─────────┬──────────┘ p6
  24. * │
  25. * │
  26. * │
  27. * │
  28. * │
  29. * │
  30. * │
  31. * │
  32. * ───────────┴───────────
  33. * p12 p11 p13
  34. */
  35. const Box = () => {
  36. return (index, scale, value, coordinate) => {
  37. const { x: X, y: Y, y1: Y1, y2: Y2, y3: Y3, y4: Y4, series: S } = value;
  38. // Calc width for each box.
  39. // The scales for x and series channels must be band scale.
  40. const xScale = scale.x;
  41. const series = scale.series;
  42. const P = Array.from(index, (i) => {
  43. const groupWidth = xScale.getBandWidth(xScale.invert(+X[i]));
  44. const ratio = series ? series.getBandWidth(series.invert(+(S === null || S === void 0 ? void 0 : S[i]))) : 1;
  45. const width = groupWidth * ratio;
  46. const offset = (+(S === null || S === void 0 ? void 0 : S[i]) || 0) * groupWidth;
  47. const x = +X[i] + offset + width / 2;
  48. const [low, q1, median, q3, high] = [
  49. +Y[i],
  50. +Y1[i],
  51. +Y2[i],
  52. +Y3[i],
  53. +Y4[i],
  54. ];
  55. const P13 = [
  56. [x - width / 2, high],
  57. [x + width / 2, high],
  58. [x, high],
  59. [x, q3],
  60. [x - width / 2, q3],
  61. [x + width / 2, q3],
  62. [x + width / 2, q1],
  63. [x - width / 2, q1],
  64. [x - width / 2, median],
  65. [x + width / 2, median],
  66. [x, q1],
  67. [x, low],
  68. [x - width / 2, low],
  69. [x + width / 2, low],
  70. ];
  71. return P13.map((d) => coordinate.map(d));
  72. });
  73. return [index, P];
  74. };
  75. };
  76. exports.Box = Box;
  77. exports.Box.props = {
  78. defaultShape: 'box',
  79. defaultLabelShape: 'label',
  80. composite: false,
  81. channels: [
  82. ...(0, utils_1.baseGeometryChannels)({ shapes: ['box'] }),
  83. { name: 'x', scale: 'band', required: true },
  84. { name: 'y', required: true },
  85. { name: 'series', scale: 'band' },
  86. ],
  87. preInference: [...(0, utils_1.basePreInference)(), { type: 'maybeZeroX' }],
  88. postInference: [...(0, utils_1.basePostInference)(), ...(0, utils_1.tooltip1d)()],
  89. interaction: {
  90. shareTooltip: true,
  91. },
  92. };
  93. //# sourceMappingURL=box.js.map