g6/plugins/template.maxSpanningForest/index.js

78 lines
1.7 KiB
JavaScript

/**
* @fileOverview 图分析模版
* @author huangtonger@aliyun.com
* read only:
* edge.isTreeEdge
* read && write:
* node.rank - node rank the max rank would be the root node
* edge.weight - edge weight
*/
const G6 = require('@antv/g6');
const maxSpanningForest = require('./maxSpanningForest');
const Layout = require('../layout.forceAtlas2/layout');
const Util = G6.Util;
class Plugin {
constructor(options) {
Util.mix(this, {
layout: {
auto: 'once', // true false once
processer: new Layout({
kr: 120,
kg: 8.0,
mode: 'common',
prev_overlapping: true,
dissuade_hubs: false,
max_iteration: 1000,
barnes_hut: true,
ks: 0.1,
ksmax: 10,
tao: 0.1,
...options
})
},
...options
});
}
init() {
const graph = this.graph;
graph.on('beforeinit', () => {
const layout = graph.get('layout');
if (!layout) {
graph.set('layout', this.layout);
}
});
graph.on('beforerender', () => {
const data = graph.getSource();
let {
nodes,
edges
} = data;
nodes = nodes.map((node, index) => {
return {
...node,
index
};
});
edges = edges.map((edge, index) => {
return {
...edge,
index
};
});
const forest = maxSpanningForest(nodes, edges);
forest.edges.forEach(edge => {
const { index } = edge;
data.edges[index].isTreeEdge = true;
});
graph.addFilter(item => {
return !item.isEdge || item.getModel().isTreeEdge;
});
});
}
}
G6.Plugins['template.maxSpanningForest'] = Plugin;
module.exports = Plugin;