2020-02-14 10:10:54 +08:00
|
|
|
import Graph from '../../../../src/graph/graph';
|
|
|
|
import '../../../../src/shape/node';
|
|
|
|
import '../../../../src/shape/nodes';
|
|
|
|
import '../../../../src/shape/edge';
|
|
|
|
import '../../../../src/shape/edges';
|
|
|
|
import '../../../../src/behavior';
|
2019-12-12 21:41:51 +08:00
|
|
|
|
2019-12-18 21:05:01 +08:00
|
|
|
const div = document.createElement('div');
|
|
|
|
div.id = 'graph-spec';
|
|
|
|
document.body.appendChild(div);
|
2019-12-12 21:41:51 +08:00
|
|
|
|
|
|
|
describe('polyline e test', () => {
|
|
|
|
describe('default polyline test', () => {
|
2019-12-18 21:05:01 +08:00
|
|
|
const graph = new Graph({
|
|
|
|
container: div,
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
2020-03-04 22:24:07 +08:00
|
|
|
defaultEdge: {
|
2020-02-14 10:10:54 +08:00
|
|
|
type: 'polyline',
|
|
|
|
},
|
2020-03-04 22:24:07 +08:00
|
|
|
defaultNode: {
|
|
|
|
type: 'rect',
|
|
|
|
size: [100, 50],
|
|
|
|
style: {
|
2020-08-17 14:51:02 +08:00
|
|
|
opacity: 0.1,
|
|
|
|
},
|
|
|
|
},
|
2019-12-18 21:05:01 +08:00
|
|
|
});
|
2020-03-05 18:24:40 +08:00
|
|
|
it('default polyline config', () => {
|
2019-12-18 21:05:01 +08:00
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 200,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 200,
|
2019-12-18 21:05:01 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node2',
|
|
|
|
x: 150,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 100,
|
|
|
|
},
|
2019-12-18 21:05:01 +08:00
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node1',
|
|
|
|
target: 'node2',
|
2020-02-14 10:10:54 +08:00
|
|
|
type: 'polyline',
|
|
|
|
},
|
|
|
|
],
|
2019-12-18 21:05:01 +08:00
|
|
|
};
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
2019-12-12 21:41:51 +08:00
|
|
|
|
2019-12-18 21:05:01 +08:00
|
|
|
const edges = graph.getEdges();
|
|
|
|
expect(edges.length).toEqual(1);
|
|
|
|
const edge = edges[0];
|
|
|
|
const keyShape = edge.getKeyShape();
|
|
|
|
expect(keyShape.attr('lineWidth')).toEqual(1);
|
2020-11-12 19:42:59 +08:00
|
|
|
expect(keyShape.attr('stroke')).toEqual('rgb(224, 224, 224)');
|
2019-12-12 21:41:51 +08:00
|
|
|
});
|
|
|
|
|
2020-01-02 14:47:41 +08:00
|
|
|
it('polyline radius and default offset', () => {
|
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 200,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 200,
|
2020-01-02 14:47:41 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node2',
|
|
|
|
x: 150,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 100,
|
|
|
|
},
|
2020-01-02 14:47:41 +08:00
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node1',
|
|
|
|
target: 'node2',
|
2020-01-16 18:11:35 +08:00
|
|
|
type: 'polyline',
|
2020-01-02 14:47:41 +08:00
|
|
|
style: {
|
|
|
|
radius: 10,
|
2020-02-14 10:10:54 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-01-02 14:47:41 +08:00
|
|
|
};
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
|
|
|
|
const edges = graph.getEdges();
|
|
|
|
expect(edges.length).toEqual(1);
|
|
|
|
const edge = edges[0];
|
|
|
|
const keyShape = edge.getKeyShape();
|
|
|
|
expect(keyShape.attr('lineWidth')).toEqual(1);
|
2020-11-12 19:42:59 +08:00
|
|
|
expect(keyShape.attr('stroke')).toEqual('rgb(224, 224, 224)');
|
2020-01-02 14:47:41 +08:00
|
|
|
});
|
2020-08-28 09:59:13 +08:00
|
|
|
|
2020-01-02 14:47:41 +08:00
|
|
|
it('polyline radius and no offset', () => {
|
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 200,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 200,
|
2020-01-02 14:47:41 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node2',
|
|
|
|
x: 150,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 100,
|
|
|
|
},
|
2020-01-02 14:47:41 +08:00
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node1',
|
|
|
|
target: 'node2',
|
2020-01-16 18:11:35 +08:00
|
|
|
type: 'polyline',
|
2020-01-02 14:47:41 +08:00
|
|
|
style: {
|
|
|
|
radius: 10,
|
2020-02-14 10:10:54 +08:00
|
|
|
offset: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-01-02 14:47:41 +08:00
|
|
|
};
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
|
|
|
|
const edges = graph.getEdges();
|
|
|
|
expect(edges.length).toEqual(1);
|
|
|
|
const edge = edges[0];
|
|
|
|
const keyShape = edge.getKeyShape();
|
|
|
|
expect(keyShape.attr('lineWidth')).toEqual(1);
|
2020-11-12 19:42:59 +08:00
|
|
|
expect(keyShape.attr('stroke')).toEqual('rgb(224, 224, 224)');
|
2020-01-02 14:47:41 +08:00
|
|
|
});
|
2019-12-18 21:05:01 +08:00
|
|
|
it('polyline with label', () => {
|
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 200,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 200,
|
2019-12-18 21:05:01 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node2',
|
|
|
|
x: 150,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 100,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node1',
|
|
|
|
target: 'node2',
|
|
|
|
type: 'polyline',
|
|
|
|
label: 'polyline1-2',
|
|
|
|
},
|
|
|
|
],
|
2019-12-18 21:05:01 +08:00
|
|
|
};
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
2019-12-12 21:41:51 +08:00
|
|
|
|
2019-12-18 21:05:01 +08:00
|
|
|
const edges = graph.getEdges();
|
|
|
|
expect(edges.length).toEqual(1);
|
|
|
|
const edge = edges[0];
|
|
|
|
const group = edge.get('group');
|
|
|
|
expect(group.getCount()).toEqual(2);
|
2019-12-12 21:41:51 +08:00
|
|
|
|
2020-08-17 14:51:02 +08:00
|
|
|
const label = group.find((g) => {
|
2019-12-18 21:05:01 +08:00
|
|
|
return g.get('className') === 'edge-label';
|
|
|
|
});
|
|
|
|
expect(label).not.toBe(undefined);
|
2020-11-11 10:48:38 +08:00
|
|
|
expect(label.attr('fill')).toEqual('rgb(0, 0, 0)');
|
2019-12-18 21:05:01 +08:00
|
|
|
const type = label.get('type');
|
|
|
|
expect(type).toEqual('text');
|
|
|
|
});
|
2019-12-12 21:41:51 +08:00
|
|
|
|
2019-12-18 21:05:01 +08:00
|
|
|
it('polyline with controlPoints', () => {
|
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 200,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 200,
|
2019-12-18 21:05:01 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node2',
|
|
|
|
x: 150,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 100,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node1',
|
|
|
|
target: 'node2',
|
|
|
|
type: 'polyline',
|
2020-03-31 13:02:50 +08:00
|
|
|
controlPoints: [{ x: 200, y: 160 }],
|
2020-02-14 10:10:54 +08:00
|
|
|
},
|
|
|
|
],
|
2019-12-18 21:05:01 +08:00
|
|
|
};
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
2019-12-12 21:41:51 +08:00
|
|
|
|
2019-12-18 21:05:01 +08:00
|
|
|
const edges = graph.getEdges();
|
|
|
|
expect(edges.length).toEqual(1);
|
|
|
|
const edge = edges[0];
|
|
|
|
const keyShape = edge.getKeyShape();
|
|
|
|
const path = keyShape.attr('path');
|
|
|
|
expect(path.length).toEqual(3);
|
2020-03-31 13:02:50 +08:00
|
|
|
expect(path[1]).toEqual(['L', 200, 160]);
|
2019-12-18 21:05:01 +08:00
|
|
|
graph.destroy();
|
|
|
|
expect(graph.destroyed).toBe(true);
|
|
|
|
});
|
2020-08-28 09:59:13 +08:00
|
|
|
|
|
|
|
it('polyline using fallbackRoute', () => {
|
|
|
|
const graph = new Graph({
|
|
|
|
container: div,
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
|
|
|
defaultEdge: {
|
|
|
|
type: 'polyline',
|
|
|
|
routeCfg: {
|
|
|
|
maximumLoops: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
defaultNode: {
|
|
|
|
type: 'rect',
|
|
|
|
size: [100, 50],
|
|
|
|
style: {
|
|
|
|
opacity: 0.1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 200,
|
|
|
|
y: 200,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node2',
|
|
|
|
x: 150,
|
|
|
|
y: 100,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node1',
|
|
|
|
target: 'node2',
|
|
|
|
type: 'polyline',
|
|
|
|
style: {
|
|
|
|
radius: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
|
|
|
|
const edges = graph.getEdges();
|
|
|
|
expect(edges.length).toEqual(1);
|
|
|
|
const edge = edges[0];
|
|
|
|
const keyShape = edge.getKeyShape();
|
|
|
|
expect(keyShape.attr('lineWidth')).toEqual(1);
|
2020-11-12 19:42:59 +08:00
|
|
|
expect(keyShape.attr('stroke')).toEqual('rgb(224, 224, 224)');
|
2020-08-28 09:59:13 +08:00
|
|
|
});
|
2019-12-12 21:41:51 +08:00
|
|
|
});
|
2019-12-23 23:15:22 +08:00
|
|
|
|
|
|
|
describe('update', () => {
|
|
|
|
const graph = new Graph({
|
|
|
|
container: div,
|
|
|
|
width: 500,
|
2020-02-14 10:10:54 +08:00
|
|
|
height: 500,
|
2020-08-28 09:59:13 +08:00
|
|
|
modes: {
|
|
|
|
default: ['drag-node'],
|
|
|
|
},
|
2019-12-23 23:15:22 +08:00
|
|
|
});
|
|
|
|
it('styles', () => {
|
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 200,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 200,
|
2019-12-23 23:15:22 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node2',
|
|
|
|
x: 150,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 100,
|
|
|
|
},
|
2019-12-23 23:15:22 +08:00
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node1',
|
|
|
|
target: 'node2',
|
2020-01-16 18:11:35 +08:00
|
|
|
type: 'polyline',
|
2019-12-23 23:15:22 +08:00
|
|
|
style: {
|
2020-02-14 10:10:54 +08:00
|
|
|
stroke: 'red',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2019-12-23 23:15:22 +08:00
|
|
|
};
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
|
|
|
|
const edges = graph.getEdges();
|
|
|
|
expect(edges.length).toEqual(1);
|
|
|
|
const edge = edges[0];
|
|
|
|
const keyShape = edge.getKeyShape();
|
|
|
|
expect(keyShape.attr('lineWidth')).toEqual(1);
|
|
|
|
expect(keyShape.attr('stroke')).toEqual('red');
|
|
|
|
|
|
|
|
edge.update({
|
|
|
|
style: {
|
|
|
|
stroke: 'blue',
|
2020-02-14 10:10:54 +08:00
|
|
|
lineWidth: 3,
|
|
|
|
},
|
|
|
|
});
|
2019-12-23 23:15:22 +08:00
|
|
|
expect(keyShape.attr('lineWidth')).toEqual(3);
|
|
|
|
expect(keyShape.attr('stroke')).toEqual('blue');
|
|
|
|
|
|
|
|
edge.update({
|
|
|
|
style: {
|
|
|
|
shadowColor: 'black',
|
|
|
|
shadowBlur: 5,
|
2020-02-14 10:10:54 +08:00
|
|
|
},
|
|
|
|
});
|
2019-12-23 23:15:22 +08:00
|
|
|
expect(keyShape.attr('lineWidth')).toEqual(3);
|
|
|
|
expect(keyShape.attr('stroke')).toEqual('blue');
|
|
|
|
expect(keyShape.attr('shadowColor')).toEqual('black');
|
|
|
|
expect(keyShape.attr('shadowBlur')).toEqual(5);
|
|
|
|
});
|
2020-03-05 18:24:40 +08:00
|
|
|
it('label', () => {
|
2019-12-23 23:15:22 +08:00
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 200,
|
2020-01-16 01:44:36 +08:00
|
|
|
y: 200,
|
|
|
|
label: 'node',
|
|
|
|
labelCfg: {
|
|
|
|
position: 'right',
|
2020-02-14 10:10:54 +08:00
|
|
|
offset: 20,
|
|
|
|
},
|
2019-12-23 23:15:22 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node2',
|
|
|
|
x: 150,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 100,
|
|
|
|
},
|
2019-12-23 23:15:22 +08:00
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node1',
|
|
|
|
target: 'node2',
|
2020-01-16 18:11:35 +08:00
|
|
|
type: 'polyline',
|
2020-01-16 01:44:36 +08:00
|
|
|
label: 'polyline',
|
|
|
|
labelCfg: {
|
|
|
|
position: 'end',
|
|
|
|
refX: 10,
|
2020-02-14 10:10:54 +08:00
|
|
|
refY: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2019-12-23 23:15:22 +08:00
|
|
|
};
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
|
|
|
|
const edges = graph.getEdges();
|
|
|
|
const edge = edges[0];
|
|
|
|
const group = edge.get('group');
|
2020-08-17 14:51:02 +08:00
|
|
|
const label = group.find((g) => {
|
2019-12-23 23:15:22 +08:00
|
|
|
return g.get('className') === 'edge-label';
|
|
|
|
});
|
|
|
|
expect(label.attr('text')).toEqual('polyline');
|
|
|
|
|
|
|
|
edge.update({
|
|
|
|
label: 'new label',
|
|
|
|
labelCfg: {
|
2020-01-16 01:44:36 +08:00
|
|
|
position: 'start',
|
2019-12-23 23:15:22 +08:00
|
|
|
style: {
|
2020-02-14 10:10:54 +08:00
|
|
|
fill: '#0ff',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2019-12-23 23:15:22 +08:00
|
|
|
expect(label.attr('fill')).toEqual('#0ff');
|
|
|
|
expect(label.attr('text')).toEqual('new label');
|
|
|
|
|
|
|
|
edge.update({
|
|
|
|
labelCfg: {
|
|
|
|
style: {
|
|
|
|
shadowColor: 'black',
|
|
|
|
shadowBlur: 5,
|
2020-02-14 10:10:54 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2019-12-23 23:15:22 +08:00
|
|
|
expect(label.attr('fill')).toEqual('#0ff');
|
|
|
|
expect(label.attr('text')).toEqual('new label');
|
|
|
|
expect(label.attr('shadowColor')).toEqual('black');
|
|
|
|
expect(label.attr('shadowBlur')).toEqual(5);
|
|
|
|
});
|
|
|
|
it('label from none', () => {
|
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 200,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 200,
|
2019-12-23 23:15:22 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node2',
|
|
|
|
x: 150,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 100,
|
|
|
|
},
|
2019-12-23 23:15:22 +08:00
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node1',
|
|
|
|
target: 'node2',
|
2020-01-16 18:11:35 +08:00
|
|
|
type: 'polyline',
|
2020-02-14 10:10:54 +08:00
|
|
|
},
|
|
|
|
],
|
2019-12-23 23:15:22 +08:00
|
|
|
};
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
|
|
|
|
const edges = graph.getEdges();
|
|
|
|
const edge = edges[0];
|
|
|
|
const group = edge.get('group');
|
|
|
|
|
|
|
|
edge.update({
|
|
|
|
label: 'new label',
|
|
|
|
labelCfg: {
|
|
|
|
style: {
|
2020-02-14 10:10:54 +08:00
|
|
|
fill: '#0ff',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-08-17 14:51:02 +08:00
|
|
|
const label = group.find((g) => {
|
2019-12-23 23:15:22 +08:00
|
|
|
return g.get('className') === 'edge-label';
|
|
|
|
});
|
|
|
|
expect(label.attr('fill')).toEqual('#0ff');
|
|
|
|
expect(label.attr('text')).toEqual('new label');
|
|
|
|
|
|
|
|
edge.update({
|
|
|
|
labelCfg: {
|
|
|
|
style: {
|
|
|
|
shadowColor: 'black',
|
|
|
|
shadowBlur: 5,
|
2020-02-14 10:10:54 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2019-12-23 23:15:22 +08:00
|
|
|
expect(label.attr('fill')).toEqual('#0ff');
|
|
|
|
expect(label.attr('text')).toEqual('new label');
|
|
|
|
expect(label.attr('shadowColor')).toEqual('black');
|
|
|
|
expect(label.attr('shadowBlur')).toEqual(5);
|
|
|
|
});
|
|
|
|
it('label position', () => {
|
2019-12-30 12:14:07 +08:00
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 200,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 200,
|
2019-12-30 12:14:07 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node2',
|
|
|
|
x: 150,
|
2020-02-14 10:10:54 +08:00
|
|
|
y: 100,
|
|
|
|
},
|
2019-12-30 12:14:07 +08:00
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node1',
|
|
|
|
target: 'node2',
|
2020-01-16 18:11:35 +08:00
|
|
|
type: 'polyline',
|
2020-02-14 10:10:54 +08:00
|
|
|
},
|
|
|
|
],
|
2019-12-30 12:14:07 +08:00
|
|
|
};
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
2019-12-23 23:15:22 +08:00
|
|
|
|
2019-12-30 12:14:07 +08:00
|
|
|
const edges = graph.getEdges();
|
|
|
|
const edge = edges[0];
|
|
|
|
const group = edge.get('group');
|
2019-12-23 23:15:22 +08:00
|
|
|
|
2019-12-30 12:14:07 +08:00
|
|
|
edge.update({
|
|
|
|
label: 'new label',
|
|
|
|
labelCfg: {
|
|
|
|
position: 'end',
|
|
|
|
style: {
|
2020-02-14 10:10:54 +08:00
|
|
|
fill: '#0ff',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-08-17 14:51:02 +08:00
|
|
|
const label = group.find((g) => {
|
2019-12-30 12:14:07 +08:00
|
|
|
return g.get('className') === 'edge-label';
|
|
|
|
});
|
|
|
|
expect(label.attr('fill')).toEqual('#0ff');
|
|
|
|
expect(label.attr('text')).toEqual('new label');
|
2019-12-23 23:15:22 +08:00
|
|
|
});
|
|
|
|
});
|
2019-12-12 21:41:51 +08:00
|
|
|
});
|