Skip to content

Commit

Permalink
refactor(transform): replace some utils with d3-array
Browse files Browse the repository at this point in the history
  • Loading branch information
visiky committed Mar 27, 2022
1 parent a8f0f6b commit 943ae3a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 52 deletions.
37 changes: 0 additions & 37 deletions src/transform/d3-sankey/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,3 @@ export function constant(x: any) {
return x;
};
}

export function sumBy(arr, func) {
let r = 0;
for (let i = 0; i < arr.length; i++) {
r += func(arr[i]);
}

return r;
}

/**
* 计算最大值
* @param arr
* @param func
*/
export function maxValueBy(arr, func): number {
let r = -Infinity;
for (let i = 0; i < arr.length; i++) {
r = Math.max(func(arr[i]), r);
}

return r;
}

/**
* 计算最小值
* @param arr
* @param func
*/
export function minValueBy(arr, func): number {
let r = Infinity;
for (let i = 0; i < arr.length; i++) {
r = Math.min(func(arr[i]), r);
}

return r;
}
7 changes: 3 additions & 4 deletions src/transform/d3-sankey/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* 桑基图布局代码,Fork from https://github.com/d3/d3-sankey/tree/master/src
* 主要修改:
* 1. 删除 d3-array 依赖
* 2. 修改一些 set map 的遍历
* 3. 数组创建出 [empty] 导致出错
* 4. 通过 align 方法实现 depth 自定义
* 1. modify some set and map traverse
* 2. fix some error caused by [empty] array
* 3. support `nodeDepth` through align method
*/
import { Sankey as sankey } from './sankey';
export { center, left, right, justify } from './align';
Expand Down
22 changes: 11 additions & 11 deletions src/transform/d3-sankey/sankey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { sum, max, min } from 'd3-array';
import { justify } from './align';
import { constant, sumBy, maxValueBy, minValueBy } from './helper';
import { constant } from './helper';

function ascendingSourceBreadth(a, b) {
return ascendingBreadth(a.source, b.source) || a.index - b.index;
Expand Down Expand Up @@ -187,10 +188,7 @@ export function Sankey() {
for (const node of nodes) {
node.value =
node.fixedValue === undefined
? Math.max(
sumBy(node.sourceLinks, value),
sumBy(node.targetLinks, value),
)
? Math.max(sum(node.sourceLinks, value), sum(node.targetLinks, value))
: node.fixedValue;
}
}
Expand All @@ -214,7 +212,10 @@ export function Sankey() {

// 如果配置了 depth,则设置自定义 depth
if (depth) {
const maxDepth = Math.max(maxValueBy(nodes, (d: any) => d.depth) + 1, 0);
const maxDepth = Math.max(
max(nodes, (d: { depth: number }) => d.depth) + 1,
0,
);

let node;
for (let i = 0; i < nodes.length; i++) {
Expand Down Expand Up @@ -243,7 +244,7 @@ export function Sankey() {
}

function computeNodeLayers({ nodes }) {
const x = Math.max(maxValueBy(nodes, (d: any) => d.depth) + 1, 0);
const x = Math.max(max(nodes, (d: { depth: number }) => d.depth) + 1, 0);
const kx = (x1 - x0 - dx) / (x - 1);
const columns = new Array(x).fill(0).map(() => []);
for (const node of nodes) {
Expand All @@ -265,9 +266,9 @@ export function Sankey() {
}

function initializeNodeBreadths(columns) {
const ky = minValueBy(
const ky = min(
columns,
(c: any[]) => (y1 - y0 - (c.length - 1) * py) / sumBy(c, value),
(c: any[]) => (y1 - y0 - (c.length - 1) * py) / sum(c, value),
) as any as number;
for (const nodes of columns) {
let y = y0;
Expand All @@ -293,8 +294,7 @@ export function Sankey() {
const columns = computeNodeLayers(graph);
py = Math.min(
dy,
(y1 - y0) /
((maxValueBy(columns, (c: any[]) => c.length) as any as number) - 1),
(y1 - y0) / ((max(columns, (c: any[]) => c.length) as any as number) - 1),
);
initializeNodeBreadths(columns);
for (let i = 0; i < iterations; ++i) {
Expand Down

0 comments on commit 943ae3a

Please sign in to comment.