G6的外掛系統
G6的外掛系統做的相當完善, 可惜文件沒有具體說到. 這裡整理一下g6的外掛.
外掛大致分為四種類型:
- behaviour 行為, 可以理解為事件處理
- node, edge的外掛, 就是node和edge的樣式, 同樣是外掛
- layout外掛, node的佈局之類, 這部分涉及的演算法比較多
- Util外掛, 就是自定義工具函式, 將其內建G6.Util中
這四種外掛都有各自的寫法以及api, 但是文件中沒有提到, 這裡簡單介紹一下. 一下都以官方外掛為例.
behaviour 行為
寫完發現其實官方有這部分的文件:ofollow,noindex">www.yuque.com/antv/g6/cus…
請看下面程式碼, 這部分是註冊一個右鍵拖動的行為:
// g6/plugins/behaviour.analysis/index.js function panCanvas(graph, button = 'left', panBlank = false) { let lastPoint; if (button === 'right') { graph.behaviourOn('contextmenu', ev => { ev.domEvent.preventDefault(); }); } graph.behaviourOn('mousedown', ev => { if (button === 'left' && ev.domEvent.button === 0 || button === 'right' && ev.domEvent.button === 2) { if (panBlank) { if (!ev.shape) { lastPoint = { x: ev.domX, y: ev.domY }; } } else { lastPoint = { x: ev.domX, y: ev.domY }; } } }); // 滑鼠右鍵拖拽畫布空白處平移畫布互動 G6.registerBehaviour('rightPanBlank', graph => { panCanvas(graph, 'right', true); }) 複製程式碼
然後在例項化graph的時候在modes中引入:
new Graph({ modes: { default: ['panCanvas'] } }) 複製程式碼
其實到這裡我們已經知道了, 只要是在一些內建事件中註冊一下自定義事件再引入我們就可以稱之為一個行為外掛. 但是我們還需要再深入一點, 看到底是不是這樣的.
// g6/src/mixin/mode.js behaviourOn(type, fn) { const eventCache = this._eventCache; if (!eventCache[type]) { eventCache[type] = []; } eventCache[type].push(fn); this.on(type, fn); }, 複製程式碼
照老虎畫貓我們最終可以實現一個自己的行為外掛:
// 未經過驗證 function test(graph) { graph.behaviourOn('mousedown' () => alert(1) ) } // 滑鼠右鍵拖拽畫布空白處平移畫布互動 G6.registerBehaviour('test', graph => { test(graph); }) new Graph({ modes: { default: ['test'] } }) 複製程式碼
node, edge的外掛
關於node, edge的外掛的外掛其實官方文件上面的自定義形狀和自定義邊.
// g6/plugins/edge.polyline/index.js G6.registerEdge('polyline', { offset: 10, getPath(item) { const points = item.getPoints(); const source = item.getSource(); const target = item.getTarget(); return this.getPathByPoints(points, source, target); }, getPathByPoints(points, source, target) { const polylinePoints = getPolylinePoints(points[0], points[points.length - 1], source, target, this.offset); // FIXME default return Util.pointsToPolygon(polylinePoints); } }); G6.registerEdge('polyline-round', { borderRadius: 9, getPathByPoints(points, source, target) { const polylinePoints = simplifyPolyline( getPolylinePoints(points[0], points[points.length - 1], source, target, this.offset) ); // FIXME default return getPathWithBorderRadiusByPolyline(polylinePoints, this.borderRadius); } }, 'polyline'); 複製程式碼
這部分那麼多程式碼其實最重要的還是上面的部分, 註冊一個自定義邊, 直接引入就可以在shape中使用了, 具體就不展開了.自定義邊自定義節點
layout外掛
layout在初始化的時候即可以在layout
欄位中初始化也可以在plugins
中.
const graph = new G6.Graph({ container: 'mountNode', layout: dagre }) /* ---- */ const graph = new G6.Graph({ container: 'mountNode', plugins: [ dagre ] }) 複製程式碼
原因在於寫外掛的時候同時也把佈局註冊為一個外掛了:
// g6/plugins/layout.dagre/index.js class Plugin { constructor(options) { this.options = options; } init() { const graph = this.graph; graph.on('beforeinit', () => { const layout = new Layout(this.options); graph.set('layout', layout); }); } } G6.Plugins['layout.dagre'] = Plugin; 複製程式碼
通過檢視原始碼我們可以知道自定義佈局的核心方法就是<span data-type="color" style="color:rgb(111, 66, 193)"><span data-type="background" style="background-color:rgba(255, 192, 120, 0.5)">execute</span></span>
, 再具體一點就是我們需要在每個佈局外掛中都有<span data-type="color" style="color:rgb(111, 66, 193)"><span data-type="background" style="background-color:rgba(255, 192, 120, 0.5)">execute</span></span>
方法:
// g6/plugins/layout.dagre/layout.js // 執行佈局 execute() { const nodes = this.nodes; const edges = this.edges; const nodeMap = {}; const g = new dagre.graphlib.Graph(); const useEdgeControlPoint = this.useEdgeControlPoint; g.setGraph({ rankdir: this.getValue('rankdir'), align: this.getValue('align'), nodesep: this.getValue('nodesep'), edgesep: this.getValue('edgesep'), ranksep: this.getValue('ranksep'), marginx: this.getValue('marginx'), marginy: this.getValue('marginy'), acyclicer: this.getValue('acyclicer'), ranker: this.getValue('ranker') }); g.setDefaultEdgeLabel(function() { return {}; }); nodes.forEach(node => { g.setNode(node.id, { width: node.width, height: node.height }); nodeMap[node.id] = node; }); edges.forEach(edge => { g.setEdge(edge.source, edge.target); }); dagre.layout(g); g.nodes().forEach(v => { const node = g.node(v); nodeMap[v].x = node.x; nodeMap[v].y = node.y; }); g.edges().forEach((e, i) => { const edge = g.edge(e); if (useEdgeControlPoint) { edges[i].controlPoints = edge.points.slice(1, edge.points.length - 1); } }); } 複製程式碼
上面是官方外掛有向圖的核心程式碼, 用到了dagre演算法, 再簡化一點其實可以理解為就是利用某種演算法確定節點和邊的位置.
最終執行佈局的地方:
// g6/src/controller/layout.js graph._executeLayout(processor, nodes, edges, groups) 複製程式碼
Util外掛
這類外掛相對簡單許多, 就是將函式內建到Util中. 最後直接在G6.Util
中使用即可
比如一個生成模擬資料的:
// g6/plugins/util.randomData/index.js const G6 = require('@antv/g6'); const Util = G6.Util; const randomData = { // generate chain graph data createChainData(num) { const nodes = []; const edges = []; for (let index = 0; index < num; index++) { nodes.push({ id: index }); } nodes.forEach((node, index) => { const next = nodes[index + 1]; if (next) { edges.push({ source: node.id, target: next.id }); } }); return { nodes, edges }; }, // generate cyclic graph data createCyclicData(num) { const data = randomData.createChainData(num); const { nodes, edges } = data; const l = nodes.length; edges.push({ source: data.nodes[l - 1].id, target: nodes[0].id }); return data; }, // generate num * num nodes without edges createNodesData(num) { const nodes = []; for (let index = 0; index < num * num; index++) { nodes.push({ id: index }); } return { nodes }; } }; Util.mix(Util, randomData); 複製程式碼