Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed loop edges positioning and labels for loop edges #388

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/graphin/src/utils/__tests__/processEdges.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { IUserEdge } from '../../index';
import processEdges from '../processEdges';

describe('processEdges', () => {
describe('Should process edges as loops', () => {
let edges: IUserEdge[];

beforeEach(() => {
edges = [
{
source: '23a07720-b446-11e9-8592-cdfa43d74b9b',
target: '23a07720-b446-11e9-8592-cdfa43d74b9b',
data: {},
style: {
label: {
value: 'a',
},
},
},
{
source: '23a07720-b446-11e9-8592-cdfa43d74b9b',
target: '23a07720-b446-11e9-8592-cdfa43d74b9b',
data: {},
style: {
label: {
value: 'b',
},
},
},
{
source: '23a07720-b446-11e9-8592-cdfa43d74b9b',
target: '23a07720-b446-11e9-8592-cdfa43d74b9b',
data: {},
style: {
label: {
value: 'c',
},
},
},
];
});

it('With default values', () => {
const newEdges = processEdges(edges);

expect(newEdges[0].style?.label?.offset).toEqual([0, -30]);
expect(newEdges[0].style?.keyshape?.type).toEqual('loop');
expect(newEdges[0].style?.keyshape?.loop?.distance).toEqual(0);

expect(newEdges[1].style?.label?.offset).toEqual([0, -50]);
expect(newEdges[1].style?.keyshape?.type).toEqual('loop');
expect(newEdges[1].style?.keyshape?.loop?.distance).toEqual(10);

expect(newEdges[2].style?.label?.offset).toEqual([0, -70]);
expect(newEdges[2].style?.keyshape?.type).toEqual('loop');
expect(newEdges[2].style?.keyshape?.loop?.distance).toEqual(20);
});

it('With custom loop definition', () => {
const newEdges = processEdges(edges, { loop: 20 });

expect(newEdges[0].style?.label?.offset).toEqual([0, -30]);
expect(newEdges[0].style?.keyshape?.type).toEqual('loop');
expect(newEdges[0].style?.keyshape?.loop?.distance).toEqual(0);

expect(newEdges[1].style?.label?.offset).toEqual([0, -70]);
expect(newEdges[1].style?.keyshape?.type).toEqual('loop');
expect(newEdges[1].style?.keyshape?.loop?.distance).toEqual(20);

expect(newEdges[2].style?.label?.offset).toEqual([0, -110]);
expect(newEdges[2].style?.keyshape?.type).toEqual('loop');
expect(newEdges[2].style?.keyshape?.loop?.distance).toEqual(40);
});

it('With custom loop label position', () => {
const newEdges = processEdges(edges, { loopLabelPosition: 0.5 });

expect(newEdges[0].style?.label?.offset).toEqual([0, -15]);
expect(newEdges[0].style?.keyshape?.type).toEqual('loop');
expect(newEdges[0].style?.keyshape?.loop?.distance).toEqual(0);

expect(newEdges[1].style?.label?.offset).toEqual([0, -35]);
expect(newEdges[1].style?.keyshape?.type).toEqual('loop');
expect(newEdges[1].style?.keyshape?.loop?.distance).toEqual(10);

expect(newEdges[2].style?.label?.offset).toEqual([0, -55]);
expect(newEdges[2].style?.keyshape?.type).toEqual('loop');
expect(newEdges[2].style?.keyshape?.loop?.distance).toEqual(20);
});
});
});
59 changes: 37 additions & 22 deletions packages/graphin/src/utils/processEdges.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IUserEdge } from '../index';
import { IUserEdge, EdgeStyle } from '../index';
import { deepMix } from '@antv/util';

function isEven(number: number) {
return number % 2 === 0;
Expand All @@ -8,6 +9,10 @@ function isOdd(number: number) {
return !isEven(number);
}

const POLY_DEFAULT = 30;
const LOOP_DEFAULT = 10;
const LOOP_LABEL_POSITION_DEFAULT = 1;

/**
*
* @param edges 边的集合
Expand All @@ -16,16 +21,20 @@ function isOdd(number: number) {
const processEdges = (
edges: IUserEdge[],
{
poly = 30,
loop = 10,
poly = POLY_DEFAULT,
loop = LOOP_DEFAULT,
loopLabelPosition = LOOP_LABEL_POSITION_DEFAULT,
}: {
/** poly distance */
poly: number;
poly?: number;
/** loop distance */
loop: number;
loop?: number;
/** loop label position based on loop edge */
loopLabelPosition?: number;
} = {
poly: 30,
loop: 10,
poly: POLY_DEFAULT,
loop: LOOP_DEFAULT,
loopLabelPosition: LOOP_LABEL_POSITION_DEFAULT,
},
) => {
const edgesMap: { [edgeId: string]: IUserEdge[] } = {};
Expand Down Expand Up @@ -77,30 +86,36 @@ const processEdges = (
delete edge.revert;
}

let keyshapeStyle = {
type: 'poly',
poly: {
distance: resultDistance,
},
};

let keyshapeStyle: EdgeStyle['keyshape'];
if (isLoop) {
const distance = index * loop;

keyshapeStyle = {
type: 'loop',
// @ts-ignore
loop: {
distance: index * loop,
distance,
},
};

if (edge.style?.label) {
const offsetX = 0;
const offsetY = -(POLY_DEFAULT * loopLabelPosition + distance * 2);
edge.style.label.offset = [offsetX, offsetY];
}
} else {
keyshapeStyle = {
type: 'poly',
poly: {
distance: resultDistance,
},
};
}

edge.style = {
...edge.style,
keyshape: {
...edge.style?.keyshape,
...keyshapeStyle,
deepMix(edge, {
style: {
keyshape: keyshapeStyle,
},
};
});
edge.isMultiple = true;
newEdges.push(edge);
});
Expand Down