line.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Line = void 0;
  4. const d3_array_1 = require("d3-array");
  5. const coordinate_1 = require("../utils/coordinate");
  6. const utils_1 = require("./utils");
  7. const line = (index, scale, value, coordinate) => {
  8. var _a, _b;
  9. const { series: S, x: X, y: Y } = value;
  10. const { x, y } = scale;
  11. // Because x and y channel is not strictly required in Line.props,
  12. // it should throw error with empty x or y channels.
  13. if (X === undefined || Y === undefined) {
  14. throw new Error('Missing encode for x or y channel.');
  15. }
  16. // Group data into series.
  17. // There is only one series without specified series encode.
  18. const series = S ? Array.from((0, d3_array_1.group)(index, (i) => S[i]).values()) : [index];
  19. const I = series.map((group) => group[0]).filter((i) => i !== undefined);
  20. // A group of data corresponds to one line.
  21. const xoffset = (((_a = x === null || x === void 0 ? void 0 : x.getBandWidth) === null || _a === void 0 ? void 0 : _a.call(x)) || 0) / 2;
  22. const yoffset = (((_b = y === null || y === void 0 ? void 0 : y.getBandWidth) === null || _b === void 0 ? void 0 : _b.call(y)) || 0) / 2;
  23. const P = Array.from(series, (I) => {
  24. return I.map((i) => coordinate.map([+X[i] + xoffset, +Y[i] + yoffset]));
  25. });
  26. return [I, P, series];
  27. };
  28. const parallel = (index, scale, value, coordinate) => {
  29. // Extract all value for position[number] channels.
  30. const PV = Object.entries(value)
  31. .filter(([key]) => key.startsWith('position'))
  32. .map(([, value]) => value);
  33. // Because position channel is not strictly required in Line.props,
  34. // it should throw error with empty position values.
  35. if (PV.length === 0) {
  36. throw new Error('Missing encode for position channel.');
  37. }
  38. // One data corresponds to one line.
  39. const P = Array.from(index, (i) => {
  40. // Transform high dimension vector to a list of two-dimension vectors.
  41. // [a, b, c] -> [d, e, f, g, h, i]
  42. const vector = PV.map((pv) => +pv[i]);
  43. const vectors = coordinate.map(vector);
  44. // Two-dimension vectors are stored in a flat array, so extract them.
  45. // [d, e, f, g, h, i] -> [d, e], [f, g], [h, i]
  46. const points = [];
  47. for (let i = 0; i < vectors.length; i += 2) {
  48. points.push([vectors[i], vectors[i + 1]]);
  49. }
  50. return points;
  51. });
  52. return [index, P];
  53. };
  54. /**
  55. * Convert value for each channel to line shapes.
  56. */
  57. const Line = () => {
  58. return (index, scale, value, coordinate) => {
  59. const mark = (0, coordinate_1.isParallel)(coordinate) ? parallel : line;
  60. return mark(index, scale, value, coordinate);
  61. };
  62. };
  63. exports.Line = Line;
  64. const shapes = ['line', 'smooth'];
  65. exports.Line.props = {
  66. defaultShape: 'line',
  67. defaultLabelShape: 'label',
  68. composite: false,
  69. channels: [
  70. ...(0, utils_1.baseGeometryChannels)({ shapes }),
  71. { name: 'x' },
  72. { name: 'y' },
  73. { name: 'position', independent: true },
  74. { name: 'size' },
  75. { name: 'series', scale: 'identity' },
  76. ],
  77. preInference: [
  78. ...(0, utils_1.basePreInference)(),
  79. { type: 'maybeSeries' },
  80. { type: 'maybeGradient' },
  81. ],
  82. postInference: [...(0, utils_1.basePostInference)(), ...(0, utils_1.tooltip1d)(), ...(0, utils_1.tooltipXd)()],
  83. interaction: {
  84. shareTooltip: true,
  85. seriesTooltip: true,
  86. crosshairs: true,
  87. },
  88. };
  89. //# sourceMappingURL=line.js.map