vue使用antvG6笔记

本文档展示了如何在Vue项目中安装并使用AntV G6库来创建一个简单的图谱。首先通过npm安装G6,然后在组件中导入并初始化图谱,设置容器、宽高,加载数据并渲染。示例代码包括了节点和边的数据结构,以及模板和脚本部分的实现。
摘要由CSDN通过智能技术生成
  • 安装
 npm install --save @antv/g6
  • 引入
import G6 from "@antv/g6";
  • 代码
<template>
  <div id="home"></div>
</template>

<script>
import G6 from "@antv/g6";
export default {
  name: "Home",
  data() {
    return {
      data: {
        // 点集
        nodes: [
          {
            id: "node1", // String,该节点存在则必须,节点的唯一标识
            x: 100, // Number,可选,节点位置的 x 值
            y: 200 // Number,可选,节点位置的 y 值
          },
          {
            id: "node2", // String,该节点存在则必须,节点的唯一标识
            x: 300, // Number,可选,节点位置的 x 值
            y: 200 // Number,可选,节点位置的 y 值
          }
        ],
        // 边集
        edges: [
          {
            source: "node1", // String,必须,起始点 id
            target: "node2" // String,必须,目标点 id
          }
        ]
      },
      gaph: null //实例
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    //初始化图谱方法
    init() {
      this.graph = new G6.Graph({
        container: "home", // 指定图画布的容器 id,与第 9 行的容器对应
        // 画布宽高
        width: 800,
        height: 500
      });
      // 读取数据
      this.graph.data(this.data);
      // 渲染图
      this.graph.render();
    }
  }
};
</script>
<style>
.home {
  width: 100%;
  height: 500px;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. 安装antvG6 ```bash npm install @antv/g6 --save ``` 2. 在Vue组件中引入antvG6 ```vue <template> <div id="container"></div> </template> <script> import G6 from '@antv/g6'; export default { name: 'CustomLine', mounted() { this.initGraph(); }, methods: { initGraph() { const data = { nodes: [ { id: 'node1', x: 100, y: 100 }, { id: 'node2', x: 200, y: 200 }, { id: 'node3', x: 300, y: 300 }, ], edges: [ { source: 'node1', target: 'node2', type: 'custom-line' }, { source: 'node2', target: 'node3', type: 'custom-line' }, ], }; G6.registerEdge('custom-line', { draw(cfg, group) { const startPoint = cfg.startPoint; const endPoint = cfg.endPoint; const shape = group.addShape('path', { attrs: { stroke: '#333', lineWidth: 2, path: [ ['M', startPoint.x, startPoint.y], ['L', endPoint.x, endPoint.y], ], }, name: 'path-shape', }); return shape; }, }); const graph = new G6.Graph({ container: 'container', width: 500, height: 500, defaultEdge: { type: 'custom-line', }, }); graph.data(data); graph.render(); }, }, }; </script> ``` 3. 在注册Edge时,通过draw方法自定义折线的绘制方式。在draw方法中,可以获取到边的起点和终点,然后通过group.addShape('path', {...})方法绘制出折线。最后将折线作为返回值即可。 4. 在Graph实例中,可以通过defaultEdge配置项来指定所有边的类型,也可以通过edges配置项来指定单个边的类型。通过type属性来指定边的类型为custom-line即可。 5. 在上面的示例中,我们仅仅是绘制了一条直线。如果需要绘制更复杂的折线,可以在path数组中添加更多的点来控制折线的形状。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

正函数 

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值