utils.js 3.9 KB

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