density.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Density = void 0;
  4. const utils_1 = require("./utils");
  5. const Density = () => {
  6. return (index, scale, value, coordinate) => {
  7. const { x: X, series: S } = value;
  8. const Yn = Object.entries(value)
  9. .filter(([key]) => key.startsWith('y'))
  10. .map(([, value]) => value);
  11. const SZn = Object.entries(value)
  12. .filter(([key]) => key.startsWith('size'))
  13. .map(([, value]) => value);
  14. // Because x and y channel is not strictly required in Line.props,
  15. // it should throw error with empty x or y channels.
  16. if (X === undefined || Yn === undefined || SZn === undefined) {
  17. throw new Error('Missing encode for x or y or size channel.');
  18. }
  19. // Calc width for each box.
  20. // The scales for x and series channels must be band scale.
  21. const xScale = scale.x;
  22. const series = scale.series;
  23. const P = Array.from(index, (i) => {
  24. const groupWidth = xScale.getBandWidth(xScale.invert(+X[i]));
  25. const ratio = series ? series.getBandWidth(series.invert(+(S === null || S === void 0 ? void 0 : S[i]))) : 1;
  26. const width = groupWidth * ratio;
  27. const offset = (+(S === null || S === void 0 ? void 0 : S[i]) || 0) * groupWidth;
  28. const x = +X[i] + offset + width / 2;
  29. const PN = [
  30. ...Yn.map((_, idx) => [x + +SZn[idx][i] / index.length, +Yn[idx][i]]),
  31. ...Yn.map((_, idx) => [
  32. x - +SZn[idx][i] / index.length,
  33. +Yn[idx][i],
  34. ]).reverse(), // left
  35. ];
  36. return PN.map((p) => coordinate.map(p));
  37. });
  38. return [index, P];
  39. };
  40. };
  41. exports.Density = Density;
  42. const shapes = ['density'];
  43. exports.Density.props = {
  44. defaultShape: 'density',
  45. defaultLabelShape: 'label',
  46. composite: false,
  47. channels: [
  48. ...(0, utils_1.baseGeometryChannels)({ shapes }),
  49. { name: 'x', scale: 'band', required: true },
  50. { name: 'y', required: true },
  51. { name: 'size', required: true },
  52. { name: 'series', scale: 'band' },
  53. { name: 'size', required: true, scale: 'identity' },
  54. ],
  55. preInference: [
  56. ...(0, utils_1.basePreInference)(),
  57. { type: 'maybeZeroY1' },
  58. { type: 'maybeZeroX' },
  59. ],
  60. postInference: [...(0, utils_1.basePostInference)(), ...(0, utils_1.tooltip1d)()],
  61. interaction: {
  62. shareTooltip: true,
  63. },
  64. };
  65. //# sourceMappingURL=density.js.map