VUE和Antv G6實(shí)現(xiàn)在線拓?fù)鋱D編輯操作
效果圖如下:

我使用的是G6 2.0,也可以使用 G6 3.0,3.0的拓?fù)鋱D單獨(dú)作為一個(gè)編輯器使用,使用更加方便。不過(guò)2.0的比較簡(jiǎn)單,容易上手。
1.首先在Antv官網(wǎng)上找到螞蟻Antv G6插件,引入插件。
也可以npm 方式引入。
2.寫(xiě)組件
export default {
name: “index”,
components: {},
mounted() {
this.initG6();
},
data() {
return {
action: ‘',
name: ‘',
func: ‘',
account: ‘',
workflow: ‘',
nodeType: 0,
color: ‘',
net: ‘',
Util: ‘',
workflowName: ‘',
activation: ‘', //當(dāng)前激活的節(jié)點(diǎn)
isNode: false, //當(dāng)前是節(jié)點(diǎn)
isBlank: true, //當(dāng)前是空白區(qū)
checked: true, //網(wǎng)格對(duì)齊
infoTitle: ‘畫(huà)布',//屬性標(biāo)題
oldColor: ‘', //獲取節(jié)點(diǎn)本身顏色
type: ‘', //有值為編輯狀態(tài)
actionList:[],
nodeTypeList: [
{id: 0, label: ‘普通節(jié)點(diǎn)'}]
}
},
methods: {
initG6() {
let self = this;
self.Util = G6.Util;
let grid;
if (self.checked) {
grid = {
forceAlign: true, // 是否支持網(wǎng)格對(duì)齊
cell: 25, // 網(wǎng)格大小
};
} else {
grid = null;
}
// 生成度量
. . . . . .
// 工具方法
. . . . . .
var sourcesData={ }; //后臺(tái)返回的數(shù)據(jù)
var trainScale = function(dim, scale){
var max = -Infinity;
var min = Infinity;
sourcesData.source.nodes.map(function(node){
max =30;
min =25;
});
scale.domain([min, max]);
};
var colors = ['#007AE7', ‘#40BCD2', ‘#81D6C3', ‘#C1E4BC', ‘#FFDD9B', ‘#FEAC4C', ‘#FF7C01', ‘#C4201D'];
. . . . . .
// 生成圖
http://self.net = new http://G6.Net({
id: ‘knowledge', // 容器ID
height: canvasHeight, // 畫(huà)布高
mode: ‘edit'
});
G6.Global.nodeLabelStyle = {
fill: ‘#fff',
textAlign: ‘left',
textBaseline: ‘bottom',
fontSize:24
};
self.net.tooltip(true);
self.net.node()
.size(function(model){
return sizeScale(model.weight)*2;
})
;
self.net.source(sourcesData.source.nodes, sourcesData.source.edges);
self.net.removeBehaviour([‘dragCanvas', ‘dragHideEdges', ‘dragHideTexts']);
self.net.addBehaviour([‘dragBlank']);
self.net.read(sourcesData);
self.net.render();
self.net.zoomAt(graphCenterX, graphCenterY, 0.7);
// 生成布局
var layoutNodes = sourcesData.source.nodes;
var layoutEdges = Util.clone(sourcesData.source.edges);
var ticked = function(){
self.net.updateNodesPosition();
};
…
/**
點(diǎn)擊空白處
/
self.net.on(‘click', (ev) => {
if (!self.Util.isNull(ev.item)) {
self.isBlank = false
} else {
self.isBlank = true;
self.infoTitle = ‘畫(huà)布'
}
});
/
點(diǎn)擊節(jié)點(diǎn)
/
self.net.on(‘itemclick', function (ev) {
self.isNode = self.Util.isNode(ev.item); //是否為Node
self.activation = ev.item;
if (self.isNode) {
/ 激活節(jié)點(diǎn)后節(jié)點(diǎn)名稱input聚焦/
self.KaTeX parse error: Expected '}', got 'EOF' at end of input: …ick(()=>{ self.refs.inputFocus.$el.querySelector(‘input').focus();
});
self.infoTitle = ‘節(jié)點(diǎn)';
self.name = ev.item.get(‘model').label;
self.func = ev.item.get(‘model').func;
self.account = ev.item.get(‘model').account || [];
self.workflow = ev.item.get(‘model').workflow;
} else {
self.infoTitle = ‘邊';
self.action = ev.item.get(‘model').action;
}
self.color = self.oldColor;
});
/**
鼠標(biāo)移入移出事件改變顏色
/
self.net.on(‘itemmouseenter', ev => {
const item = ev.item;
self.oldColor = item.get(‘model').color; //獲取節(jié)點(diǎn)顏色
self.net.update(item, {
color: ‘#108EE9',
});
self.net.refresh();
});
self.net.on(‘itemmouseleave', ev => {
const item = ev.item;
self.net.update(item, {
color: self.oldColor
});
self.net.refresh();
});
/*
提示信息
*/
self.net.tooltip({
title: ‘信息', // @type {String} 標(biāo)題
split: ‘:', // @type {String} 分割符號(hào)
dx: 0, // @type {Number} 水平偏移
dy: 0 // @type {Number} 豎直偏移
});
self.net.edge().tooltip() .size(‘val', function(val){
return 3;
});
/**
渲染
*/
/self.net.source(self.nodes, self.edges);/ //加載資源數(shù)據(jù)
// self.net.render();
},
addCircle() {
},//添加起始節(jié)點(diǎn)
addRect() {
},//添加常規(guī)節(jié)點(diǎn)
addRhombus() {
}, //添加條件節(jié)點(diǎn)
addLine() {
}, //添加直線
addSmooth() {
}, //添加曲線
addArrowSmooth() {
}, //添加箭頭曲線
addArrowLine() {
}, //添加箭頭直線
addPolyLine() {
}, //添加折線
changeMode(mode) {
}, //拖拽與編輯模式的切換
del() {
this.net.del()
},//刪除
save() {
/* 驗(yàn)證流圖名稱*/
if (this.workflowName !== ‘') {
let data = this.net.save();
if (data.source.nodes.length === 0) {
this.KaTeX parse error: Expected 'EOF', got '}' at position 61: …; return false }̲ /* 驗(yàn)證節(jié)點(diǎn)名稱*/ fo…message({type: ‘error', message: ‘節(jié)點(diǎn)名稱不能為空'});
return false
}
}
data.source[‘name'] = this.workflowName;
/let json = JSON.stringify(data, null, 2);/
this.KaTeX parse error: Expected 'EOF', got '}' at position 43: …e, this.type); }̲ else { this.message({type: ‘error', message: ‘拓?fù)涿Q不能為空'})
}
/console.log(saveData, json);/
},//保存
update() {
}, //更新節(jié)點(diǎn)
clearView() {
this.type = ‘';
this.workflowName = ‘';
this.net.changeData()
}, //清空視圖
source(nodes, edges, name, type) {
this.type = type;
this.workflowName = name;
this.net.changeData(nodes, edges)
}, //更新數(shù)據(jù)
},
watch: {
/**
監(jiān)聽(tīng)輸入框
/
action: function () {
this.update()
},
name: function () {
this.update()
},
func: function () {
this.update()
},
account: function () {
this.update()
},
workflow: function () {
this.update()
},
nodeType: function () {
this.update()
},
color: function () {
this.update()
},
/*
網(wǎng)格切換
*/
checked: function () {
let _saveData = this.net.save();
this.net.destroy(); //銷(xiāo)毀畫(huà)布
this.initG6();
this.net.read(_saveData);
this.net.render()
}
}
}
3.注意:
在實(shí)現(xiàn)過(guò)程中,我采用了度量的生成方法使節(jié)點(diǎn)均勻分布,否則需要指定節(jié)點(diǎn)的位置。不指定位置頁(yè)面不會(huì)顯示任何東西。
補(bǔ)充知識(shí):antv G6關(guān)系樹(shù),拓?fù)鋱D 不同層級(jí)不同顏色
前端菜雞… 近期遇到一個(gè)需求,要求關(guān)系圖每個(gè)層級(jí)不同顏色展示,位置還得隨機(jī),目前echart實(shí)現(xiàn)后都不太滿意…em 于是G6 …
廢話不多說(shuō) 看代碼(效果)//
1、引入G6 相關(guān) (基礎(chǔ)數(shù)據(jù)是官方的)
import G6 from '@antv/g6'
fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/algorithm-category.json')
.then(res => res.json())
.then(data => {
console.log(data)
const width = document.getElementById('map').scrollWidth;
const height = document.getElementById('map').scrollHeight || 500;
const graph= new G6.TreeGraph({
container: 'map',
width,
height,
pixelRatio: 2,
modes: {
default: [{
type: 'collapse-expand',
onChange: function onChange(item, collapsed) {
console.log(item)
const data = item.get('model').data;
data.collapsed = collapsed;
return true;
}
}, 'drag-canvas', 'zoom-canvas']
},
defaultNode: {
size: [200, 50],
shape: 'rect',
style: {
fill: '#C6E5FF',
stroke: '#5B8FF9'
}
},
defaultEdge: {
shape: 'cubic-horizontal',
style: {
stroke: '#A3B1BF'
}
},
layout: {
type: 'mindmap',
direction: 'H',
getHeight: () => {
return 40;
},
getWidth: () => {
return 160;
},
getVGap: () => {
return 10;
},
getHGap: () => {
return 100;
}
}
});
let centerX = 0;
// 以下重點(diǎn)
graph.node(function (node) {
// depth 類(lèi)似節(jié)點(diǎn)標(biāo)識(shí)
if(node.depth == 0){
console.log(node)
return {
size:[100,60],
style:{
fill:'red',
// stroke:''
},
label:node.id
}
}
if(node.depth == 1){
console.log(node)
return {
size:[100,60],
style:{
fill:'blue',
},
label:node.id
}
}
return {
label: node.id, // 設(shè)置顯示名稱
labelCfg: {
// position: node.children && node.children.length > 0 ? 'left' : node.x > centerX ? 'right' : 'left', // 設(shè)置顯示左右
offset: 5
}
};
});
graph.data(data);
graph.render();
graph.fitView();
});
這樣效果就出來(lái)了. 不同級(jí)不同顏色 看圖

以上這篇VUE和Antv G6實(shí)現(xiàn)在線拓?fù)鋱D編輯操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?長(zhǎng)列表數(shù)據(jù)刷新的實(shí)現(xiàn)及思考
這篇文章主要為大家介紹了vue?長(zhǎng)列表數(shù)據(jù)刷新的實(shí)現(xiàn)及思考,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Vue+Router+Element實(shí)現(xiàn)簡(jiǎn)易導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了Vue+Router+Element實(shí)現(xiàn)簡(jiǎn)易導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue引入elementUi后打開(kāi)頁(yè)面報(bào)錯(cuò)Uncaught?TypeError的解決方式
這篇文章主要給大家介紹了關(guān)于vue引入elementUi后打開(kāi)頁(yè)面報(bào)錯(cuò)Uncaught?TypeError:?Cannot?read?properties?of?undefined(reading?‘prototype‘)的解決方式,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Vue路由組件的緩存keep-alive和include屬性的具體使用
:瀏覽器頁(yè)面在進(jìn)行切換時(shí),原有的路由組件會(huì)被銷(xiāo)毀,通過(guò)緩存可以保存被切換的路由組件,本文主要介紹了Vue路由組件的緩存keep-alive和include屬性的具體使用,感興趣的可以了解一下2023-11-11
使用Vue組件實(shí)現(xiàn)一個(gè)簡(jiǎn)單彈窗效果
這篇文章主要介紹了使用Vue組件實(shí)現(xiàn)一個(gè)簡(jiǎn)單彈窗效果,本文主要內(nèi)容會(huì)涉及到彈窗遮罩的實(shí)現(xiàn), slot 插槽的使用方式,props 、 $emit 傳參,具體組件代碼也傳上去了。需要的朋友可以參考下2018-04-04
Vue Element前端應(yīng)用開(kāi)發(fā)之echarts圖表
在我們做應(yīng)用系統(tǒng)的時(shí)候,往往都會(huì)涉及圖表的展示,綜合的圖表展示能夠給客戶帶來(lái)視覺(jué)的享受和數(shù)據(jù)直觀體驗(yàn),同時(shí)也是增強(qiáng)客戶認(rèn)同感的舉措之一2021-05-05
vue使用ElementUI時(shí)導(dǎo)航欄默認(rèn)展開(kāi)功能的實(shí)現(xiàn)
這篇文章主要介紹了vue使用ElementUI時(shí)導(dǎo)航欄默認(rèn)展開(kāi)功能的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07

