使用html快速基于antv G6实现可视化

本文提供了一个Antv G6在Vue环境下的基础使用模板,包括HTML、CSS和JS代码示例。示例展示了如何创建一个Radial布局的图,并通过更新配置动态改变布局效果。适合初学者参考,实现数据可视化。
摘要由CSDN通过智能技术生成

因为Antv G6涉及到组件挂载,其可视化实现需要基于Vue或React等前端框架,无法在纯html上使用。

而网上能够迅速实现Antv G6的代码参考太少,因此在这里留一个模板供大家快速实现可视化。

注:
需要本地存在@antv依赖包,vue可以用线上依赖链接

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Antv G6 模板html</title>
    <style>::-webkit-scrollbar{display:none;}html,body{overflow:hidden;margin:0;}</style>
  </head>
  <body>
    <div id="app">
      {{message}}
    </div>
    <div id="container" style="height: 1000px;"></div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>/*Fixing iframe window.innerHeight 0 issue in Safari*/document.body.clientHeight;</script>
    <script src="G6依赖路径\@antv\g6\dist\g6.min.js"></script>
    <script>
    var app = new Vue({
      el:"#app",

      data:{
        message:"Radial辐射图"
      },

      created(){
        console.log("created完成");
      },
      mounted(){
        console.log("mounted进行");

      // 后续代码可直接从Antv G6样例中cv
        const data = {
          nodes: [
            {
              id: '0',
              label: '0',
            },
            {
              id: '2',
              label: '2',
            },
            {
              id: '3',
              label: '3',
            },
            {
              id: '4',
              label: '4',
            },
            {
              id: '5',
              label: '5',
            },
            {
              id: '6',
              label: '6',
            },
            {
              id: '9',
              label: '9',
            },
            {
              id: '10',
              label: '10',
            },
          ],
          edges: [
            {
              source: '0',
              target: '2',
            },
            {
              source: '0',
              target: '3',
            },
            {
              source: '0',
              target: '4',
            },
            {
              source: '0',
              target: '5',
            },
            {
              source: '0',
              target: '9',
            },
            {
              source: '0',
              target: '10',
            },
            {
              source: '2',
              target: '3',
            },
            {
              source: '4',
              target: '5',
            },
            {
              source: '6',
              target: '4',
            },
            {
              source: '5',
              target: '6',
            },
            {
              source: '9',
              target: '10',
            },
          ],
        };

        const descriptionDiv = document.createElement('div');
        descriptionDiv.innerHTML =
          'Radial layout, focusNode =  = 0, unitRadius = 50, preventOverlap: false';
        const container = document.getElementById('container');
        container.appendChild(descriptionDiv);

        const width = container.scrollWidth;
        const height = (container.scrollHeight || 500) - 20;

        const graph = new G6.Graph({
          container: 'container',
          width,
          height,
          modes: {
            default: ['drag-canvas', 'drag-node'],
          },
          layout: {
            type: 'radial',
            unitRadius: 50,
          },
          animate: true,
          defaultNode: {
            size: 20,
          },
          defaultEdge: {
            style: {
              endArrow: {
                path: 'M 0,0 L 8,4 L 8,-4 Z',
                fill: '#e2e2e2',
              },
            },
          },
        });
        graph.data(data);
        graph.render();

        if (typeof window !== 'undefined')
          window.onresize = () => {
            if (!graph || graph.get('destroyed')) return;
            if (!container || !container.scrollWidth || !container.scrollHeight) return;
            graph.changeSize(container.scrollWidth, container.scrollHeight - 20);
          };

        layoutConfigTranslation();

        setInterval(function () {
          layoutConfigTranslation();
        }, 7000);

        function layoutConfigTranslation() {
          setTimeout(function () {
            descriptionDiv.innerHTML =
              'Radial layout, focusNode =  = 0, unitRadius = 50, preventOverlap: true';
            graph.updateLayout({
              preventOverlap: true,
              nodeSize: 20,
            });
          }, 1000);

          setTimeout(function () {
            descriptionDiv.innerHTML =
              'Radial layout, focusNode =  = 0, unitRadius = 80, preventOverlap: true';
            graph.updateLayout({
              unitRadius: 80,
            });
          }, 2500);

          setTimeout(function () {
            descriptionDiv.innerHTML =
              'Radial layout, focusNode =  = 10, unitRadius = 80, preventOverlap: true';
            graph.updateLayout({
              focusNode: '10',
            });
          }, 4000);

          setTimeout(function () {
            descriptionDiv.innerHTML =
              'Radial layout, focusNode =  = 20, unitRadius = 80, preventOverlap: true';
            graph.updateLayout({
              focusNode: '20',
            });
          }, 5500);

          setTimeout(function () {
            descriptionDiv.innerHTML =
              'Radial layout, focusNode =  = 0, unitRadius = 50, preventOverlap: false';
            graph.updateLayout({
              focusNode: '0',
              preventOverlap: false,
              unitRadius: 50,
            });
          }, 5500);
        }
      }
    })  
    </script>
  </body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

达分柒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值