VUE和Antv G6实现在线拓扑图编辑

VUE和Antv G6实现在线拓扑图编辑

效果图如下:
在这里插入图片描述
我使用的是G6 2.0,也可以使用 G6 3.0,3.0的拓扑图单独作为一个编辑器使用,使用更加方便。不过2.0的比较简单,容易上手。
1.首先在Antv官网上找到蚂蚁Antv G6插件,引入插件。

也可以npm 方式引入。

2.写组件

export default {
name: “index”,
components: {},
mounted() {
this.initG6();
},
data() {
return {
action: ‘’,
name: ‘’,
func: ‘’,
account: ‘’,
workflow: ‘’,
nodeType: 0,
color: ‘’,
net: ‘’,
Util: ‘’,
workflowName: ‘’,
activation: ‘’, //当前激活的节点
isNode: false, //当前是节点
isBlank: true, //当前是空白区
checked: true, //网格对齐
infoTitle: ‘画布’,//属性标题
oldColor: ‘’, //获取节点本身颜色
type: ‘’, //有值为编辑状态
actionList:[],
nodeTypeList: [
{id: 0, label: ‘普通节点’}]
}
},
methods: {
initG6() {
let self = this;
self.Util = G6.Util;
let grid;
if (self.checked) {
grid = {
forceAlign: true, // 是否支持网格对齐
cell: 25, // 网格大小
};
} else {
grid = null;
}

// 生成度量
. . . . . .
// 工具方法
. . . . . .

var sourcesData={ }; //后台返回的数据

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, // 画布高
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();
};

/**
点击空白处
/
self.net.on(‘click’, (ev) => {
if (!self.Util.isNull(ev.item)) {
self.isBlank = false
} else {
self.isBlank = true;
self.infoTitle = ‘画布’
}
});
/

点击节点
/
self.net.on(‘itemclick’, function (ev) {
self.isNode = self.Util.isNode(ev.item); //是否为Node
self.activation = ev.item;
if (self.isNode) {
/
激活节点后节点名称input聚焦
/
self.KaTeX parse error: Expected '}', got 'EOF' at end of input: …ick(()=>{ self.refs.inputFocus.$el.querySelector(‘input’).focus();
});
self.infoTitle = ‘节点’;
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;
});
/**

  • 鼠标移入移出事件改变颜色
    /
    self.net.on(‘itemmouseenter’, ev => {
    const item = ev.item;
    self.oldColor = item.get(‘model’).color; //获取节点颜色
    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} 标题
    split: ‘:’, // @type {String} 分割符号
    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);/ //加载资源数据
// self.net.render();
},
addCircle() {

},//添加起始节点
addRect() {

},//添加常规节点
addRhombus() {

}, //添加条件节点
addLine() {

}, //添加直线
addSmooth() {

}, //添加曲线
addArrowSmooth() {

}, //添加箭头曲线
addArrowLine() {

}, //添加箭头直线
addPolyLine() {

}, //添加折线
changeMode(mode) {

}, //拖拽与编辑模式的切换
del() {
this.net.del()
},//删除
save() {
/* 验证流图名称*/
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 }̲ /* 验证节点名称*/ fo…message({type: ‘error’, message: ‘节点名称不能为空’});
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: ‘拓扑名称不能为空’})
}
/console.log(saveData, json);/
},//保存
update() {

}, //更新节点
clearView() {
this.type = ‘’;
this.workflowName = ‘’;
this.net.changeData()
}, //清空视图
source(nodes, edges, name, type) {
this.type = type;
this.workflowName = name;
this.net.changeData(nodes, edges)
}, //更新数据
},
watch: {
/**

  • 监听输入框
    /
    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()
    },
    /
    *
  • 网格切换
    */
    checked: function () {
    let _saveData = this.net.save();
    this.net.destroy(); //销毁画布
    this.initG6();
    this.net.read(_saveData);
    this.net.render()
    }
    }
    }

3.注意:
在实现过程中,我采用了度量的生成方法使节点均匀分布,否则需要指定节点的位置。不指定位置页面不会显示任何东西。
以上代码只写了重要的部分,需要理解后在写,如果有不明白的可以关注我的博客或者知乎文章。

谢谢观看!

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值