vue3使用AntV X6 (图可视化引擎)历程[二]

通过h函数动态展示自定义节点内容

一、案例效果

二、案例代码

  • 父组件. BloodTopology.vue

    <template>
    <TopologyCompact> <template #main-board-box> <TopologyDependent domId="featureBloodContainer" :nodeData="originalNodeData" /> </template>
    复制代码
        <template #right-drawer-box>
          <RightDrawer :width="350">
            <template #rightContent> rightContent </template>
          </RightDrawer>
        </template>
      </TopologyCompact>
    </div>
    </template> <script lang="ts" setup> import RightDrawer from '@/common/components/topologyToolKit/RightDrawer.vue'; import TopologyCompact from '@/common/components/topologyToolKit/TopologyCompact.vue'; import TopologyDependent from '@/common/components/topologyToolKit/TopologyDependent.vue'; import { onMounted, ref } from 'vue';

    const originalNodeData = ref<any>({
    nodes: [],
    edges: [],
    });

    const edges = [
    ['1', '2'],
    ['2', '3'],
    ['2', '4'],
    ['4', '5'],
    ['4', '6'],
    ['4', '7'],
    ['4', '8'],
    ['5', '9'],
    ['6', '10'],
    ['7', '11'],
    ['8', '12'],
    ];
    const initNodeData = () => {
    for (let i = 1; i <= 12; i++) {
    originalNodeData.value.nodes!.push({
    id: ${i},
    shape: 'vue-shape',
    width: 32,
    height: 32,
    label: 我是第${i}个,
    attrs: {
    body: {
    fill: '#5F95FF',
    stroke: 'transparent',
    },
    label: {
    fill: '#ffffff',
    },
    },
    });
    }

    复制代码
    edges.forEach((edge: [string, string]) => {
      originalNodeData.value.edges!.push({
        source: edge[0],
        target: edge[1],
        attrs: {
          line: {
            stroke: '#A2B1C3',
            strokeWidth: 2,
          },
        },
      });
    });

    };
    onMounted(() => {
    initNodeData();
    });
    </script>

  • h 函数动态渲染内容展示 attrConfig.ts

    import NodeElement from '@/views/featureManage/featureList/topologyToolKit/NodeHtml.vue';
    import { h } from 'vue';
    /**

    • 自定义注册节点配置
      */
      export const registerOption = {
      shape: 'vue-shape',
      width: 100,
      height: 100,
      component: ({ node }: { node: any }) => {
      // 将 node 数据传递给 NodeElement 组件
      return h(NodeElement, { nodeElementItem: node });
      },
      };
  • 自定义节点内容展示 NodeHtml.vue

    <template>
    {{ nodeInfo?.label }}
    </template> <script lang="ts" setup> import { onMounted, ref } from 'vue';

    const props = defineProps({
    nodeElementItem: {
    type: Object,
    required: true,
    },
    });

    const nodeInfo = ref();

    const handleClick = () => {
    console.log('---handleClick');
    };

    const initNodeData = () => {
    nodeInfo.value = props.nodeElementItem.store.data;
    console.log(' nodeInfo.value ', nodeInfo.value);
    };
    onMounted(() => {
    initNodeData();
    });
    </script>

    <style lang="less" scoped> .status-node { height: 50px; width: 100px; border-radius: 10px; display: flex; align-items: center; justify-content: center; border: 1px solid #8f8f8fa1; box-shadow: 0 0 6px rgba(0, 0, 0, 0.1); z-index: 999; }

    .content {
    color: #000;
    font-size: 14px;
    }
    </style>

三、控制台打印信息

相关推荐
JELEE.5 小时前
Django登录注册完整代码(图片、邮箱验证、加密)
前端·javascript·后端·python·django·bootstrap·jquery
毕设十刻10 小时前
基于Vue的学分预警系统98k51(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
牧杉-惊蛰11 小时前
纯flex布局来写瀑布流
前端·javascript·css
王同学要变强14 小时前
【深入学习Vue丨第二篇】构建动态Web应用的基础
前端·vue.js·学习
社恐的下水道蟑螂14 小时前
从字符串到像素:深度解析 HTML/CSS/JS 的页面渲染全过程
javascript·css·html
程序定小飞14 小时前
基于springboot的web的音乐网站开发与设计
java·前端·数据库·vue.js·spring boot·后端·spring
武昌库里写JAVA14 小时前
element-ui 2.x 及 vxe-table 2.x 使用 css 定制主题
java·vue.js·spring boot·sql·学习
行走的陀螺仪15 小时前
uni-app + Vue3 实现折叠文本(超出省略 + 展开收起)
前端·javascript·css·uni-app·vue3
冴羽15 小时前
JavaScript 异步循环踩坑指南
前端·javascript·node.js
Mr.Jessy15 小时前
Web APIs 学习第四天:DOM事件进阶
开发语言·前端·javascript·学习·ecmascript