vue g6 hello world

本文介绍了如何在Vue项目中使用G6图可视化框架,详细展示了从安装到创建流程图的全过程,包括代码示例和输出效果。

在下当前所在项目需要弄一个流程图,前端框架用的vue,所以研究一下g6在vue下的使用。

g6介绍

G6 是蚂蚁金服旗下一个开源图可视化框架。它提供了一套图可视化的基础设置,能帮助开发者搭建属于自己的图 图分析 应用或是 图编辑器 应用。

个人感觉这个图可视化框架比 GoJS更优秀更好用些。

项目地址:https://github.com/antvis/g6

准备

通过 npm 安装

npm install @antv/g6 --save

安装成功后,可以通过import进行引用。

import G6 from '@antv/g6';

const graph = new G6.Graph({
  container: 'mountNode',
  width: 600,
  height: 300
});

完整代码

<template>
    <div id="mountNode"></div>
</template>

<script>
    import G6 from '@antv/g6';
    export default {
        name: "start",
        created() { //不能在created方法中初始化
            //this.initG6()
        },
        mounted(){
            this.initG6()
        },
        methods: {
            initG6(){
                const data = {
                    nodes: [{
                        id: 'node1',
                        x: 100,
                        y: 200
                    },{
                        id: 'node2',
                        x: 300,
                        y: 200
                    }],
                    edges: [{
                        target: 'node2',
                        source: 'node1'
                    }]
                };
                const graph = new G6.Graph({
                    container: 'mountNode',
                    width: 500,
                    height: 500,
                    nodeStyle: {
                        default: {
                            fill: '#40a9ff',
                            stroke: '#096dd9'
                        }
                    },
                    edgeStyle: {
                        default: { stroke: '#A3B1BF' }
                    }
                });
                graph.read(data);
            }
        }
    }
</script>

输出效果

在这里插入图片描述


参考:

"vue-flowchart": "^0.1.1", <template> <div class="flow-container"> <h3>vue-flowchart 流程图</h3> <flow-chart :data="chartData" :options="flowOptions" style="width: 1000px; height: 600px; border: 1px solid #ccc;" ></flow-chart> </div> </template> <script> import FlowChart from 'vue-flowchart' import 'vue-flowchart/dist/vue-flowchart.css' export default { name: "HelloWorld", components: { FlowChart }, data() { return { // 初始化为包含空数组的对象,避免undefined chartData: { nodes: [], edges: [] }, flowOptions: { draggable: false, connector: ['StateMachine', { cornerRadius: 5 }], paintStyle: { stroke: '#666', strokeWidth: 2 }, endpointStyle: { radius: 6, fill: '#42b983' }, anchors: ['Right', 'Left'] } }; }, mounted() { // 加载数据(模拟后端请求) this.fetchFlowData(); }, methods: { fetchFlowData() { // 模拟后端数据(确保nodes和edges是数组) const mockData = { nodes: [ { id: 'node1', label: '开始', x: 100, y: 50, width: 120, height: 60 }, { id: 'node2', label: '部门审批', x: 300, y: 200, width: 120, height: 60 }, { id: 'node3', label: 'HR审批', x: 600, y: 200, width: 120, height: 60 }, { id: 'node4', label: '结束', x: 450, y: 350, width: 120, height: 60 } ], edges: [ { source: 'node1', target: 'node2' }, { source: 'node2', target: 'node3' }, { source: 'node3', target: 'node4' } ] }; // 赋值时验证并兜底(防止后端返回格式错误) this.chartData = { nodes: Array.isArray(mockData.nodes) ? mockData.nodes : [], edges: Array.isArray(mockData.edges) ? mockData.edges : [] }; } } }; </script> 表格连接线没有展示
10-26
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值