g6/tests/unit/layout/fruchterman-spec.ts

213 lines
5.3 KiB
TypeScript
Raw Normal View History

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;
2019-12-30 01:21:15 +08:00
const div = document.createElement('div');
div.id = 'graph-spec';
document.body.appendChild(div);
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,
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],
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();
});
it('fruchterman layout with no node', () => {
const graph = new G6.Graph({
container: div,
layout: {
2020-02-14 10:10:54 +08:00
type: 'fruchterman',
},
width: 500,
height: 500,
});
graph.data({
2020-02-14 10:10:54 +08:00
nodes: [],
});
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();
});
it('fruchterman layout with one node', () => {
const graph = new G6.Graph({
container: div,
layout: {
2020-02-14 10:10:54 +08:00
type: 'fruchterman',
},
width: 500,
height: 500,
});
graph.data({
2020-02-14 10:10:54 +08:00
nodes: [
{
id: 'node',
},
],
});
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) => {
node.size = 10;
2020-02-14 10:10:54 +08:00
node.cluster = Math.ceil((Math.random() / 3) * 10);
node.style = {
2020-02-14 10:10:54 +08:00
fill: colors[node.cluster],
};
});
const graph = new G6.Graph({
container: div,
layout: {
type: 'fruchterman',
clustering: true,
maxIteration: 3000,
2020-02-14 10:10:54 +08:00
clusterGravity: null,
},
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,
},
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',
},
],
};
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();
});
it('instantiate layout', () => {
const fruchtermanLayout = new G6.Layout['fruchterman']({
2020-02-14 10:10:54 +08:00
center: [250, 250],
});
fruchtermanLayout.init(data);
fruchtermanLayout.execute();
const graph = new G6.Graph({
width: 500,
height: 500,
2020-02-14 10:10:54 +08:00
container: div,
});
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
});