utils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.generateHierarchyRoot = exports.initializeData = exports.valueof = exports.field = exports.visualMark = exports.p = exports.createBandOffset = exports.bandWidth = exports.basePostInference = exports.basePreInference = exports.baseAnnotationChannels = exports.tooltipXd = exports.tooltip1d = exports.tooltip2d = exports.baseGeometryChannels = exports.baseChannels = void 0;
  4. const d3_hierarchy_1 = require("d3-hierarchy");
  5. function baseChannels(options = {}) {
  6. const { shapes } = options;
  7. return [
  8. { name: 'color' },
  9. { name: 'opacity' },
  10. { name: 'shape', range: shapes },
  11. { name: 'enterType' },
  12. { name: 'enterDelay', scaleKey: 'enter' },
  13. { name: 'enterDuration', scaleKey: 'enter' },
  14. { name: 'enterEasing' },
  15. { name: 'key', scale: 'identity' },
  16. { name: 'groupKey', scale: 'identity' },
  17. { name: 'label', scale: 'identity' },
  18. ];
  19. }
  20. exports.baseChannels = baseChannels;
  21. function baseGeometryChannels(options = {}) {
  22. return [...baseChannels(options), { name: 'title', scale: 'identity' }];
  23. }
  24. exports.baseGeometryChannels = baseGeometryChannels;
  25. function tooltip2d() {
  26. return [
  27. { type: 'maybeTitle', channel: 'color' },
  28. { type: 'maybeTooltip', channel: ['x', 'y'] },
  29. ];
  30. }
  31. exports.tooltip2d = tooltip2d;
  32. function tooltip1d() {
  33. return [
  34. { type: 'maybeTitle', channel: 'x' },
  35. { type: 'maybeTooltip', channel: ['y'] },
  36. ];
  37. }
  38. exports.tooltip1d = tooltip1d;
  39. function tooltipXd() {
  40. return [
  41. { type: 'maybeTitle', channel: 'color' },
  42. { type: 'maybeTooltip', channel: ['position'] },
  43. ];
  44. }
  45. exports.tooltipXd = tooltipXd;
  46. function baseAnnotationChannels(options = {}) {
  47. return baseChannels(options);
  48. }
  49. exports.baseAnnotationChannels = baseAnnotationChannels;
  50. function basePreInference() {
  51. return [{ type: 'maybeKey' }];
  52. }
  53. exports.basePreInference = basePreInference;
  54. function basePostInference() {
  55. return [];
  56. }
  57. exports.basePostInference = basePostInference;
  58. function bandWidth(scale, x) {
  59. return scale.getBandWidth(scale.invert(x));
  60. }
  61. exports.bandWidth = bandWidth;
  62. function createBandOffset(scale, value, options = {}) {
  63. const { x: X, y: Y, series: S } = value;
  64. const { x, y, series } = scale;
  65. const { style: { bandOffset = series ? 0 : 0.5, bandOffsetX = bandOffset, bandOffsetY = bandOffset, } = {}, } = options;
  66. const isBandX = !!(x === null || x === void 0 ? void 0 : x.getBandWidth);
  67. const isBandY = !!(y === null || y === void 0 ? void 0 : y.getBandWidth);
  68. const isSeries = !!(series === null || series === void 0 ? void 0 : series.getBandWidth);
  69. if (!isBandX && !isBandY)
  70. return (d) => d;
  71. return (d, i) => {
  72. const widthX = isBandX ? bandWidth(x, X[i]) : 0;
  73. const widthY = isBandY ? bandWidth(y, Y[i]) : 0;
  74. const f = () => (bandWidth(series, S[i]) / 2 + +S[i]) * widthX;
  75. const offset = isSeries && S ? f() : 0;
  76. const [x0, y0] = d;
  77. return [x0 + bandOffsetX * widthX + offset, y0 + bandOffsetY * widthY];
  78. };
  79. }
  80. exports.createBandOffset = createBandOffset;
  81. function p(d) {
  82. return parseFloat(d) / 100;
  83. }
  84. exports.p = p;
  85. function visualMark(index, scale, value, coordinate) {
  86. const { x: X, y: Y } = value;
  87. const { width, height } = coordinate.getOptions();
  88. const P = Array.from(index, (i) => {
  89. const x0 = X[i];
  90. const y0 = Y[i];
  91. const x = typeof x0 === 'string' ? p(x0) * width : +x0;
  92. const y = typeof y0 === 'string' ? p(y0) * height : +y0;
  93. return [[x, y]];
  94. });
  95. return [index, P];
  96. }
  97. exports.visualMark = visualMark;
  98. function field(encode) {
  99. return typeof encode === 'function' ? encode : (d) => d[encode];
  100. }
  101. exports.field = field;
  102. function valueof(data, encode) {
  103. return Array.from(data, field(encode));
  104. }
  105. exports.valueof = valueof;
  106. function initializeData(data, encode) {
  107. const { source = (d) => d.source, target = (d) => d.target, value = (d) => d.value, } = encode;
  108. const { links, nodes } = data;
  109. const LS = valueof(links, source);
  110. const LT = valueof(links, target);
  111. const LV = valueof(links, value);
  112. return {
  113. links: links.map((_, i) => ({
  114. target: LT[i],
  115. source: LS[i],
  116. value: LV[i],
  117. })),
  118. nodes: nodes || Array.from(new Set([...LS, ...LT]), (key) => ({ key })),
  119. };
  120. }
  121. exports.initializeData = initializeData;
  122. /**
  123. * @description Path need when the data is a flat json structure,
  124. * and the tree object structure do not need.
  125. */
  126. function generateHierarchyRoot(data, path) {
  127. if (Array.isArray(data)) {
  128. return typeof path === 'function'
  129. ? (0, d3_hierarchy_1.stratify)().path(path)(data)
  130. : (0, d3_hierarchy_1.stratify)()(data);
  131. }
  132. return (0, d3_hierarchy_1.hierarchy)(data);
  133. }
  134. exports.generateHierarchyRoot = generateHierarchyRoot;
  135. //# sourceMappingURL=utils.js.map