2019-12-30 01:21:15 +08:00
|
|
|
import G6 from '../../../src';
|
2020-04-24 18:23:07 +08:00
|
|
|
import dataset from './data';
|
|
|
|
|
|
|
|
const data = dataset.data;
|
2020-01-03 19:49:26 +08:00
|
|
|
|
2019-12-30 01:21:15 +08:00
|
|
|
const div = document.createElement('div');
|
|
|
|
div.id = 'graph-spec';
|
|
|
|
document.body.appendChild(div);
|
|
|
|
|
2020-01-03 19:49:26 +08:00
|
|
|
describe('fruchterman', () => {
|
2019-12-30 01:21:15 +08:00
|
|
|
it('new graph with fruchterman layout, without configurations', () => {
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
container: div,
|
|
|
|
layout: { type: 'fruchterman' },
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
2020-01-03 19:49:26 +08:00
|
|
|
defaultNode: {
|
2020-02-14 10:10:54 +08:00
|
|
|
size: 10,
|
|
|
|
},
|
2019-12-30 01:21:15 +08:00
|
|
|
});
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
expect(graph.getNodes()[0].getModel().x).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[0].getModel().y).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[1].getModel().x).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[1].getModel().y).not.toEqual(undefined);
|
|
|
|
graph.destroy();
|
|
|
|
});
|
|
|
|
it('new graph with fruchterman layout, with configurations', () => {
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
container: div,
|
|
|
|
layout: {
|
|
|
|
type: 'fruchterman',
|
|
|
|
center: [100, 100],
|
2020-01-03 19:49:26 +08:00
|
|
|
maxIteration: 5000,
|
2019-12-30 01:21:15 +08:00
|
|
|
},
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
|
|
|
});
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
expect(graph.getNodes()[0].getModel().x).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[0].getModel().y).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[1].getModel().x).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[1].getModel().y).not.toEqual(undefined);
|
|
|
|
graph.destroy();
|
|
|
|
});
|
2020-01-03 19:49:26 +08:00
|
|
|
it('fruchterman layout with no node', () => {
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
container: div,
|
|
|
|
layout: {
|
2020-02-14 10:10:54 +08:00
|
|
|
type: 'fruchterman',
|
2020-01-03 19:49:26 +08:00
|
|
|
},
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
|
|
|
});
|
|
|
|
graph.data({
|
2020-02-14 10:10:54 +08:00
|
|
|
nodes: [],
|
2020-01-03 19:49:26 +08:00
|
|
|
});
|
|
|
|
graph.render();
|
|
|
|
graph.destroy();
|
|
|
|
});
|
2020-02-14 17:25:43 +08:00
|
|
|
it('fruchterman layout with undefined nodes in data', () => {
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
container: div,
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
|
|
|
});
|
|
|
|
const Layout = new G6.Layout['fruchterman']();
|
|
|
|
Layout.init({});
|
|
|
|
Layout.execute();
|
|
|
|
graph.render();
|
|
|
|
graph.destroy();
|
|
|
|
});
|
2020-01-03 19:49:26 +08:00
|
|
|
it('fruchterman layout with one node', () => {
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
container: div,
|
|
|
|
layout: {
|
2020-02-14 10:10:54 +08:00
|
|
|
type: 'fruchterman',
|
2020-01-03 19:49:26 +08:00
|
|
|
},
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
|
|
|
});
|
|
|
|
graph.data({
|
2020-02-14 10:10:54 +08:00
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node',
|
|
|
|
},
|
|
|
|
],
|
2020-01-03 19:49:26 +08:00
|
|
|
});
|
|
|
|
graph.render();
|
|
|
|
const nodeModel = graph.getNodes()[0].getModel();
|
|
|
|
expect(nodeModel.x).toEqual(250);
|
|
|
|
expect(nodeModel.y).toEqual(250);
|
|
|
|
graph.destroy();
|
|
|
|
});
|
|
|
|
it('fruchterman layout with clustering and no clusterGravity', () => {
|
2020-02-14 10:10:54 +08:00
|
|
|
const colors = ['#f00', '#0f0', '#00f', '#ff0'];
|
2020-08-17 14:51:02 +08:00
|
|
|
data.nodes.forEach((node) => {
|
2020-01-03 19:49:26 +08:00
|
|
|
node.size = 10;
|
2020-02-14 10:10:54 +08:00
|
|
|
node.cluster = Math.ceil((Math.random() / 3) * 10);
|
2020-01-03 19:49:26 +08:00
|
|
|
node.style = {
|
2020-02-14 10:10:54 +08:00
|
|
|
fill: colors[node.cluster],
|
2020-01-03 19:49:26 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
container: div,
|
|
|
|
layout: {
|
|
|
|
type: 'fruchterman',
|
|
|
|
clustering: true,
|
|
|
|
maxIteration: 3000,
|
2020-02-14 10:10:54 +08:00
|
|
|
clusterGravity: null,
|
2020-01-03 19:49:26 +08:00
|
|
|
},
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
|
|
|
});
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
const node0 = data.nodes[0];
|
|
|
|
expect(node0.x).not.toEqual(NaN);
|
|
|
|
expect(node0.y).not.toEqual(NaN);
|
|
|
|
graph.destroy();
|
|
|
|
});
|
|
|
|
it('fruchterman layout with overlapped nodes and loop edge', () => {
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
container: div,
|
|
|
|
layout: {
|
|
|
|
type: 'fruchterman',
|
|
|
|
clustering: true,
|
|
|
|
maxIteration: 3000,
|
2020-02-14 10:10:54 +08:00
|
|
|
clusterGravity: null,
|
2020-01-03 19:49:26 +08:00
|
|
|
},
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
|
|
|
});
|
|
|
|
const tmpData = {
|
2020-02-14 10:10:54 +08:00
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'node0',
|
|
|
|
x: 100,
|
|
|
|
y: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node1',
|
|
|
|
x: 100,
|
|
|
|
y: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'node3',
|
|
|
|
x: 150,
|
|
|
|
y: 120,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
source: 'node3',
|
|
|
|
target: 'node3',
|
|
|
|
},
|
|
|
|
],
|
2020-01-03 19:49:26 +08:00
|
|
|
};
|
|
|
|
graph.data(tmpData);
|
|
|
|
graph.render();
|
|
|
|
const node0 = tmpData.nodes[0];
|
|
|
|
const node1 = tmpData.nodes[1];
|
|
|
|
expect(node0.x).not.toEqual(node1.x);
|
|
|
|
expect(node0.y).not.toEqual(node1.y);
|
|
|
|
graph.destroy();
|
|
|
|
});
|
2019-12-30 01:21:15 +08:00
|
|
|
it('update fructherman layout configurations', () => {
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
container: div,
|
|
|
|
layout: {
|
|
|
|
type: 'fruchterman',
|
|
|
|
},
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
|
|
|
});
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
graph.updateLayout({
|
|
|
|
center: [100, 100],
|
|
|
|
gravity: 50,
|
|
|
|
});
|
|
|
|
expect(graph.getNodes()[0].getModel().x).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[0].getModel().y).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[1].getModel().x).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[1].getModel().y).not.toEqual(undefined);
|
|
|
|
graph.destroy();
|
|
|
|
});
|
2020-01-06 11:38:00 +08:00
|
|
|
|
|
|
|
it('instantiate layout', () => {
|
|
|
|
const fruchtermanLayout = new G6.Layout['fruchterman']({
|
2020-02-14 10:10:54 +08:00
|
|
|
center: [250, 250],
|
2020-01-06 11:38:00 +08:00
|
|
|
});
|
|
|
|
fruchtermanLayout.init(data);
|
|
|
|
fruchtermanLayout.execute();
|
|
|
|
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
width: 500,
|
|
|
|
height: 500,
|
2020-02-14 10:10:54 +08:00
|
|
|
container: div,
|
2020-01-06 11:38:00 +08:00
|
|
|
});
|
|
|
|
graph.data(data);
|
|
|
|
graph.render();
|
|
|
|
|
|
|
|
expect(graph.getNodes()[0].getModel().x).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[0].getModel().y).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[1].getModel().x).not.toEqual(undefined);
|
|
|
|
expect(graph.getNodes()[1].getModel().y).not.toEqual(undefined);
|
|
|
|
graph.destroy();
|
|
|
|
});
|
2019-12-30 01:21:15 +08:00
|
|
|
});
|