有向图

作者: 幻城之雪 | 来源:发表于2023-05-28 22:40 被阅读0次

写在前面

最近的项目有一个需求,是关于有向图的, 第一个对应的功能主要是数据渲染,第二个则是添加了节点自定义的功能;
经过领导指导以及调查资料,发现有一个专门的js库--dagre
dagre 是专注于有向图布局的 javascript 库,由于 dagre 仅仅专注于图形布局,需要使用其他方案根据 dagre 的布局信息来实际渲染图形,而 dagre-d3 就是 dagre 基于 D3 的渲染方案

有向图的绘制

写一个小demo,效果图如下:


引入d3 以及 dagre-d3

1.准备数据nodes(节点)和edges(边)

nodes =  {
  0: {
    label: 'ReactComponent',
    style: 'fill: rgb(80, 194, 138);'
  },
  1: {
    label: 'props',
    style: 'fill: rgb(204, 230, 255);'
  },
  2: {
    label: 'context',
    style: 'fill: rgb(204, 230, 255);'
  },
  3: {
    label: 'end',
    style: 'fill: rgb(204, 230, 255);'
  },
  4: {
    label: 'end',
    style: 'fill: rgb(204, 230, 255);'
  },
  2235: {
    label: "<button>Button</button>",
    labelType: "html",
    style: 'fill: rgb(204, 230, 255);'
  }
}
edges: [
    [0, 1, {
      style: 'stroke: rgb(214, 214, 214); fill: none',
      curve: d3.curveBasis
    }],
    [0, 2, {
      style: 'stroke: rgb(214, 214, 214); fill: none',
      curve: d3.curveBasis
    }],
    [0, 3, {
      style: 'stroke: rgb(214, 214, 214); fill: none',
      curve: d3.curveBasis
    }],
    [1, 4, {
      style: 'stroke: rgb(214, 214, 214); fill: none',
      curve: d3.curveBasis
    }],
    [2, 4, {
      style: 'stroke: rgb(214, 214, 214); fill: none',
      curve: d3.curveBasis
    }]
]
 <template>
  <svg class='dagre-svg'>
    <g/>
  </svg>
 </template>
 data() {
    return {
    //创建Graph对象
      g: new dagreD3.graphlib.Graph().setGraph({
        rankdir: 'LR',
        marginx: 20,
        marginy: 20
      })
    }
 }
 
 mounted() {
    this.svg = d3.select('svg');
    this.inner = d3.select('g');
    this.renderDag(this.nodes, this.edges);
 }
 
 methods: {
    renderDag(nodes, edges) {
      for (let [id, node] of Object.entries(nodes))
        this.g.setNode(id, node);
      for (let edge of edges) {
        this.g.setEdge(edge[0], edge[1], edge[2]);
      }
    }
 }
  
  
data() {
  return {
    renderGraph: new dagreD3.render()
  }
}
methods: {
  renderDag(nodes, edges) {
    //选择绘图容器
    let inner = this.inner;
    ...添加节点和边
    //在绘图容器上运行渲染器生成流程图
    this.renderGraph(inner, this.g);
  }
}
methods: {
  renderDag(nodes, edges) {
    let svg = this.svg;
    let zoom = d3.zoom().on("zoom",
          () => inner.attr("transform", d3.event.transform));
    svg.call(zoom);
  }
}

2.使用 dagre-d3 创建 Graph 对象,并添加节点和边

<template>
<svg class='dagre-svg'>
<g/>
</svg>
</template>
data() {
return {
//创建Graph对象
g: new dagreD3.graphlib.Graph().setGraph({
rankdir: 'LR',
marginx: 20,
marginy: 20
})
}
}

mounted() {
this.svg = d3.select('svg');
this.inner = d3.select('g');
this.renderDag(this.nodes, this.edges);
}

methods: {
renderDag(nodes, edges) {
for (let [id, node] of Object.entries(nodes))
this.g.setNode(id, node);
for (let edge of edges) {
this.g.setEdge(edge[0], edge[1], edge[2]);
}
}
}

3.创建渲染器

data() {
return {
renderGraph: new dagreD3.render()
}
}
methods: {
renderDag(nodes, edges) {
//选择绘图容器
let inner = this.inner;
...添加节点和边
//在绘图容器上运行渲染器生成流程图
this.renderGraph(inner, this.g);
}
}
4.交互
methods: {
renderDag(nodes, edges) {
let svg = this.svg;
let zoom = d3.zoom().on("zoom",
() => inner.attr("transform", d3.event.transform));
svg.call(zoom);
}
}
###基本概念及基本配置
![](https://img.haomeiwen.com/i28984750/ceb1dbd5b0b21ec0.jpg)

相关文章

  • 有向图和最短路径Dijkstra、Bellman-Ford、Fl

    本篇开始讨论关于有向图的算法,无向图是特殊的有向图。内容概要: 有向图的实现 最短路径经典算法实现 有向图的实现 ...

  • 任务调度-DAG和Oozie基础

    本文主要内容 有向无环图 拓扑排序 Oozie 有向无环图 什么是有向无环图 有向无环图(Directed Acy...

  • 《算法》-图[有向图]

    有向图 简单的来说有向图就是连接带方向的图。有向图的例子在现实生活中也很多,比如在一段时间内银行间的现金流动,或者...

  • 数据结构-图

    数据结构 - 图 目录: 基本概念无向图有向图 储存结构邻接矩阵邻接表十字链表(有向图)邻接多重表(无向图) 图的...

  • 图的表示

    图的概念 无向图无向图 有向图有向图 带权图带权图 顶点:图中的元素。 边:图中的一个顶点可以与任意其他顶点建立连...

  • 数据结构--图的定义及存储结构

    一、 图的定义和术语1、 图按照无方向和有方向分为“无向图”和“有向图” 无向图是由顶点和边构成,有向图是由顶...

  • 图的定义与术语 1、图按照有无方向分为无向图和有向图。无向图由顶点和边构成,有向图由顶点和弧构成。弧有弧尾和弧头之...

  • (19)监督学-标注问题-隐马尔科夫模型

    图模型主要分为2种;有向图和无向图。 图模型——1有向图——贝叶斯网(静态、动态——HMM)——生成式模型 ...

  • DirectedAcyclicGraph

    有向无环图,所有的树都是有向无环图

  • 有向图环检测、拓扑排序和有向欧拉图

    内容概要: DAG图及有向图环检测 拓扑排序与环检测 有向欧拉图的欧拉回路Hierholzer算法 有向图环检测 ...

网友评论

    本文标题:有向图

    本文链接:https://www.haomeiwen.com/subject/owadedtx.html