area.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { group } from 'd3-array';
  2. import { baseGeometryChannels, basePostInference, basePreInference, tooltip1d, } from './utils';
  3. /*
  4. * Convert value for each channel to area shapes.
  5. *
  6. * ▲
  7. * │
  8. * │ y2
  9. * │
  10. * │ y1 xxxxxxxxxxxxx
  11. * │ xxxx x
  12. * │ xxx x
  13. * │ xxx x
  14. * │ y0 xxx x
  15. * │ xxxxxxx x
  16. * │ x x
  17. * │ xx x
  18. * │ x x
  19. * │ x x
  20. * │ x x
  21. * │ x x
  22. * │ x x
  23. * │ x x
  24. * │ x x
  25. * ────┼─────────x───────────────────────────────x──────────────►
  26. * │ y3 y4 y5
  27. */
  28. export const Area = () => {
  29. return (index, scale, value, coordinate) => {
  30. var _a, _b;
  31. const { x: X, y: Y, y1: Y1, series: S } = value;
  32. const { x, y } = scale;
  33. // Group data by series field.
  34. const series = S ? Array.from(group(index, (i) => S[i]).values()) : [index];
  35. const I = series.map((group) => group[0]).filter((i) => i !== undefined);
  36. // A group of data corresponds to one area.
  37. const xoffset = (((_a = x === null || x === void 0 ? void 0 : x.getBandWidth) === null || _a === void 0 ? void 0 : _a.call(x)) || 0) / 2;
  38. const yoffset = (((_b = y === null || y === void 0 ? void 0 : y.getBandWidth) === null || _b === void 0 ? void 0 : _b.call(y)) || 0) / 2;
  39. const P = Array.from(series, (SI) => {
  40. const l = SI.length;
  41. const points = new Array(l * 2);
  42. for (let idx = 0; idx < SI.length; idx++) {
  43. const i = SI[idx];
  44. points[idx] = coordinate.map([+X[i] + xoffset, +Y[i] + yoffset]); // y1
  45. points[l + idx] = coordinate.map([+X[i] + xoffset, +Y1[i] + yoffset]); // y0
  46. }
  47. return points;
  48. });
  49. return [I, P, series];
  50. };
  51. };
  52. const shapes = ['area', 'smooth', 'hvh', 'hv', 'vh'];
  53. Area.props = {
  54. defaultShape: 'area',
  55. defaultLabelShape: 'label',
  56. composite: false,
  57. channels: [
  58. ...baseGeometryChannels({ shapes }),
  59. { name: 'x', required: true },
  60. { name: 'y', required: true },
  61. { name: 'size' },
  62. { name: 'series', scale: 'identity' },
  63. ],
  64. preInference: [
  65. ...basePreInference(),
  66. { type: 'maybeSeries' },
  67. { type: 'maybeZeroY1' },
  68. { type: 'maybeZeroPadding' },
  69. ],
  70. postInference: [...basePostInference(), ...tooltip1d()],
  71. interaction: {
  72. shareTooltip: true,
  73. seriesTooltip: true,
  74. crosshairs: true,
  75. },
  76. };
  77. //# sourceMappingURL=area.js.map