首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用VueJS设计网络拓扑图

用VueJS设计网络拓扑图
EN

Stack Overflow用户
提问于 2022-06-09 10:32:42
回答 1查看 434关注 0票数 1

我正在用vueJS和v-网络图库设计一个网络拓扑图.

有了这个,我可以实现下图。

我的要求是,在每个节点之间的链接上,我要显示图标,如下图所示。

有人能帮忙吗,我怎么能做到这一点?或者除了v-网络图之外,我还能查到的其他图书馆。

谢谢你的回答。

EN

回答 1

Stack Overflow用户

发布于 2022-07-05 13:08:37

我是v-network-graph的作者。

利用"edge-overlay"v-network-graph插槽可以将任意物体叠加在边缘上。这是在v0.6.1中实现的特性,因此需要使用v0.6.1或更高版本。

下面是一个例子。

代码语言:javascript
运行
复制
<!-- App.vue -->
<script setup lang="ts">
import { defineConfigs } from "v-network-graph";
import data from "./data";

const WIRELESS_ICON = "&#xe63e";

const configs = defineConfigs({
  node: {
    selectable: true,
  },
});
</script>

<template>
  <v-network-graph
    :nodes="data.nodes"
    :edges="data.edges"
    :layouts="data.layouts"
    :configs="configs"
  >
    <!-- Use CSS to define references to external fonts.
         To use CSS within SVG, use <defs>. -->
    <defs>
      <!-- Cannot use <style> directly due to restrictions of Vue. -->
      <!-- eslint-disable-next-line vue/require-component-is -->
      <component is="style">
        <!-- eslint-disable prettier/prettier -->
        @font-face { font-family: 'Material Icons'; font-style: normal; font-weight: 400; src:
        url(https://fonts.gstatic.com/s/materialicons/v97/flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2)
        format('woff2'); }
        .edge-icon { pointer-events: none; }
        <!-- eslint-enable -->
      </component>
    </defs>
    <template #edge-overlay="{ edge, scale, length, pointAtLength }">
      <!-- source side -->
      <g v-if="edge.isSourceWireless" class="edge-icon">
        <!-- pointAtLength():Calculate the coordinates advanced
             the specified length from the source side. -->
        <circle
          :cx="pointAtLength(30 * scale).x"
          :cy="pointAtLength(30 * scale).y"
          :r="10 * scale"
          stroke="#444"
          :stroke-width="1 * scale"
          fill="#fff"
        />
        <text
          v-bind="pointAtLength(30 * scale)"
          font-family="Material Icons"
          text-anchor="middle"
          dominant-baseline="central"
          :font-size="16 * scale"
          v-html="WIRELESS_ICON"
        />
      </g>
      <!-- target side -->
      <g v-if="edge.isTargetWireless" class="edge-icon">
        <!-- length: The total length of the edge. -->
        <circle
          :cx="pointAtLength(length - 30 * scale).x"
          :cy="pointAtLength(length - 30 * scale).y"
          :r="10 * scale"
          stroke="#444"
          :stroke-width="1 * scale"
          fill="#fff"
        />
        <text
          v-bind="pointAtLength(length - 30 * scale)"
          font-family="Material Icons"
          text-anchor="middle"
          dominant-baseline="central"
          :font-size="16 * scale"
          v-html="WIRELESS_ICON"
        />
      </g>
    </template>
  </v-network-graph>
</template>
代码语言:javascript
运行
复制
// data.ts
import { Nodes, Edges, Layouts } from "v-network-graph";

const nodes: Nodes = {
  node1: { name: "N1" },
  node2: { name: "N2" },
  node3: { name: "N3" },
  node4: { name: "N4" },
  node5: { name: "N5" },
  node6: { name: "N6" },
};

const edges: Edges = {
  edge1: { source: "node1", target: "node2", isSourceWireless: true },
  edge2: { source: "node2", target: "node3", isTargetWireless: true },
  edge3: { source: "node2", target: "node4", isTargetWireless: true },
  edge4: { source: "node4", target: "node5", isTargetWireless: true },
  edge5: { source: "node4", target: "node6", isTargetWireless: true },
};

const layouts: Layouts = {
  nodes: {
    node1: { x: 0, y: 0 },
    node2: { x: 80, y: 60 },
    node3: { x: 0, y: 160 },
    node4: { x: 200, y: 160 },
    node5: { x: 320, y: 0 },
    node6: { x: 320, y: 160 },
  },
};

export default {
  nodes,
  edges,
  layouts,
};

这里是显示上述代码的图像。

请注意,如果您需要"edge-overlay"插槽的其他示例,可以在v-网络图的“示例”站点的下一页找到它。

https://dash14.github.io/v-network-graph/examples/appearance.html#place-any-object-on-the-edge

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72558793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档