interval.js 2.5 KB

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