美文网首页
echarts关系图graph绘制拓扑图, 使用过程中遇到的坑.

echarts关系图graph绘制拓扑图, 使用过程中遇到的坑.

作者: 柒枼 | 来源:发表于2023-02-02 16:15 被阅读0次

用echarts绘制topo图,研究了下最终决定使用其中的graph关系图,由于项目中数据比较复杂,在数据处理和使用echarts绘制时,遇到了几个坑, 因此决定在此记录一下下...

一、项目情况

1.框架angular8
2.ng-zorro7.5x
3.echarts4.2.1~

二、需求

1.需求:使用echarts绘制设备间的拓扑图
选择示例:echarts 关系图graph悲惨世界人物关系图/力引导布局

三、基础代码片段

var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom, 'dark');
var option;
option = {
    title: {
      text: 'Les Miserables',
      subtext: 'Default layout',
      top: 'bottom',
      left: 'right'
    },
    tooltip: {},
    series: [
          {
            name: 'Les Miserables',
            type: 'graph',
            layout: 'force',
            data: graph.nodes,
            links: graph.links,
            categories: graph.categories,
          }
    ]
  };
  
   myChart.setOption(option);

四、数据构造

var graph = {
    "nodes": [
      {
        "id": "0",
        "name": "Myriel",
        "draggable": true
      },
      {
        "id": "1",
        "name": "Napoleon",
        "draggable": true
      },
      {
        "id": "2",
        "name": "MlleBaptistine",
        "draggable": true
      },
      {
        "id": "3",
        "name": "MmeMagloire",
        "draggable": true
      },
      {
        "id": "4",
        "name": "CountessDeLo",
        "draggable": true
      },
    ],
   "links": [
      {
        "source": "1",
        "target": "0",
        "relation": {name: 'http'},
      },
      {
        "source": "2",
        "target": "3",
        "relation": {name: 'modbus'}
      },
      {
        "source": "3",
        "target": "0",
        "relation": {name: 'mqtt'}
      },
      {
        "source": "3",
        "target": "2",
         "relation": {name: 'snmp'}
      }
    ],
    "categories": [
      {
        "name": "http",
         "itemStyle":{color:'#91e8e1'},
      },
      {
        "name": "modbus",
         "itemStyle":{color:'#2b908f'},
        
      },
      {
        "name": "mqtt",
         "itemStyle":{color:'#e4d354'},
      },
      {
        "name": "snmp",
         "itemStyle":{color:'#f15c80'},
      }
    ]
  };

五、结果如图所示

image.png

此时展示的图形们有几个问题,第一节点过小,第二不知道这些节点分别是谁,因此首先要增加节点样式

5.1 设置节点样式

节点样式可以在nodes数据中分别设置,也可以在series中直接设置,本文采用在series中统一设置的形式:

series: [
          {
            name: 'Les Miserables',
            type: 'graph',
            layout: 'force',
            data: graph.nodes,
            links: graph.links,
            categories: graph.categories,
            label: { //节点显示名称
              show: true,
              position: "bottom",
              distance: 5,
              fontSize: 18,
              align: "center",
              
            },
            // symbol: 'image:///assets/images/devices/device.png', // 设置节点图片或形状,
//circle,'rect'等,具体见官网api示例

            symbolSize: 30, // 节点大小
          }
    ]

image.png
5.2 配置series

此时我们看图可以看到,所有的节点都挨着,所以说明节点间没有排斥力,同时关系间的线的长度都没有设置,节点之间的关系也没有显示,因此直接在series增加相应的设置即可,最终的series如下:

 series: [
      {
        name: 'Les Miserables',
        type: 'graph',
        layout: 'force',
        data: graph.nodes,
        links: graph.links,
        categories: graph.categories,
        label: {
              show: true,
              position: "bottom",
              distance: 5,
              fontSize: 18,
              align: "center",
              
            },
        symbolSize: 30,
        roam: true,
        edgeSymbol: ["circle", "arrow"],
        force: { // 节点排斥力设置
            repulsion: 200,
            gravity: 0.01,
            edgeLength: 200
        },
        lineStyle: { 
          color: 'source',
          curveness: 0.3 // 线的曲率
        },
        emphasis: { //高亮状态的图形样式,v5.0版本及以上
          focus: 'adjacency',
          lineStyle: {
            width: 10
          }
        },
        edgeLabel: {//边的设置(节点之间的关系)
              show: true,
              position: "middle",
              fontSize: 12,
              formatter: (params) => {
                return params.data.relation.name;
          },
        },
      }
    ]

image.png
5.3 配置legend

此时我们可以看到图形中是没有小标题legend的,需在option中设置,最终的option如下:

option = {
    title: {
      text: 'Les Miserables',
      subtext: 'Default layout',
      top: 'bottom',
      left: 'right'
    },
    tooltip: {},
    legend: [
      {
        // selectedMode: 'single',
        data: graph.categories.map(function (a) {
          return a.name;
        })
      }
    ],
    animationDuration: 1500,
    animationEasingUpdate: 'quinticInOut',
    series:[...] // 如上已展示,在此不再具体写出
  };
image.png
5.4 legend默认点击事件不生效问题

此时可以看出legend与图形节点之间并没有一一对应,legend的默认点击不显示事件也并未生效,观察官网示例数据发现,nodes数据每条需要一个category值与categories的索引值对应。
nodes数据更改如下:

"nodes": [
      {
        "id": "0",
        "name": "Myriel",
        "draggable": true,
        "category":0
      },
      {
        "id": "1",
        "name": "Napoleon",
        "draggable": true,
        "category": 1
      },
      {
        "id": "2",
        "name": "MlleBaptistine",
         "draggable": true,
        "category": 2
      },
      {
        "id": "3",
        "name": "MmeMagloire",
        "draggable": true,
        "category": 3
      },
      {
        "id": "4",
        "name": "CountessDeLo",
        "draggable": true,
        "category": 4
      }
    ],
image.png
5.5 legend与节点间的一一对应处理

设置好nodes里的每一条category值之后,观察图片,会发现,我们有一个节点消失了,nodes有5条数据,而图中只显示了4个节点,这是为什么呢???
观察nodes的category值与categories数组发现,categories数组有四组数据,而nodes有5组数据,当ndoes的category值按照索引设置后,最大为4,此时无法与categories数组对应,因此一条数据就无法显示了,更改nodes索引值与categories一一对应就可以完美解决此问题啦,nodes最终数据为:

"nodes": [
      {
        "id": "0",
        "name": "Myriel",
        "draggable": true,
        "category":0
      },
      {
        "id": "1",
        "name": "Napoleon",
        "draggable": true,
        "category": 1
      },
      {
        "id": "2",
        "name": "MlleBaptistine",
         "draggable": true,
        "category": 2
      },
      {
        "id": "3",
        "name": "MmeMagloire",
        "draggable": true,
        "category": 3
      },
      {
        "id": "4",
        "name": "CountessDeLo",
        "draggable": true,
        "category": 3
      }
    ],
image.png

最后附echarts官网示例链接 ,以供参考:
https://echarts.apache.org/examples/zh/editor.html?c=graph

相关文章

  • echarts-graph取消节点之间线的联动效果

    安利插件: echarts v4.0,本次讲解Graph即关系图有关的小技巧。如果有朋友还不知道echarts这款...

  • echarts3绘制拓扑图

    配置: 传送门:https://github.com/Youzi-wr/echarts-topology/tree...

  • Echarts 图表中设置背景图片

    在项目开发过程中,遇到在Echarts图表上添加背景图的需求,通过查找Echarts官网的配置项,发现 graph...

  • Echarts绘制关系图(一)

    关系图 何为关系图,从字面上可以看出,为关系的图形,既然为关系,那么就需要有点以及关系,用来表示点与点之间的联系。...

  • echarts绘制条形图-添加总数

    echarts绘制条形图-添加总数 前言 本文的代码是基于react的。本文仅用于记录我在echarts绘制条形图...

  • Echarts关系图demo

    以上市公司的调研数据为例,对echarts的关系图绘制和各属性进行解释;echarts3没有force了,而是把f...

  • echarts自适应

    一、图表变形 使用Echarts绘制图表时,可能会遇到变形的问题。如下图:【变形图】其原因是Echarts在初始化...

  • Echarts图表变形与自适应解决方案

    一、图表变形 使用Echarts绘制图表时,可能会遇到变形的问题。如下图:【变形图】其原因是Echarts在初始化...

  • Echarts学习笔记

    介绍和安装 介绍 Echarts是一个用于绘制可视化图表的工具,包括但不限于多种类型的统计图、树图、关系图、地图等...

  • 2018-09-20

    分工 (1)讨论系统功能(2)搜集文字素材:马才楷(3)绘制功能模块图:蒋婷(4)绘制网络拓扑图:文伟(5)设计界...

网友评论

      本文标题:echarts关系图graph绘制拓扑图, 使用过程中遇到的坑.

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