echarts 括扑图(graph 与 lines实现)

目的

要实现一个由几条线串起来的设备,线是动态的,如下
在这里插入图片描述

相关技术

vue,echarts

难点

因为用到了两种图,要保持坐标系一致性,graph设置coordinateSystem: ‘cartesian2d’,后不能使用x,y要使用value,(这一点官网没有说)

代码


<script setup>
import { ref } from 'vue'
import MyEcharts from '@/components/myEcharts/index.vue'
import symbol_img_1 from '../../assets/image/test/配电柜.svg'

const opt = ref(null)
opt.value = {
  title: {
    text: '巡检图'
  },
  xAxis: {
    show: false,
    type: 'value',
    min: 0,
    max: 100
  },
  yAxis: {
    show: false,
    type: 'value',
    min: 0,
    max: 100
  },
  tooltip: {},
  animationDurationUpdate: 1500, // 数据更新动画的时长,单位 ms,默认 300
  animationEasingUpdate: 'quinticInOut', // 数据更新动画的缓动效果,默认 'cubicOut'
  series: [
    {
      type: 'graph',
      coordinateSystem: 'cartesian2d',
      layout: 'none', // 图的布局 none:不采用任何布局
      symbol: `image://${symbol_img_1}`, // 节点标记的图形
      symbolSize: 100, // 节点大小,可以设置数组表示宽高
      roam: false, // 是否可以平移缩放
      label: {
        show: true //是否显示标签
      },
      edgeSymbol: ['circle', 'arrow'], // 边两端的类型
      edgeSymbolSize: [4, 10], // 边两端的大小,可以设置数组表示宽高

      edgeLabel: {
        // 边的文本标签
        fontSize: 20
      },
      data: [
        {
          name: 'Node 1',
          value: [20, 50]
        },
        {
          name: 'Node 2',
          value: [50, 50]
        },
        {
          name: 'Node 3',
          value: [80, 50]
        },
        {
          name: 'Node 4',
          value: [80, 80]
        }
      ],
      links: [
        // {
        //   source: 'Node 1',
        //   target: 'Node 2',
        //   symbolSize: [10, 20],
        //   label: {
        //     show: false
        //   },
        //   lineStyle: {
        //     width: 5,
        //     color: 'rgba(15, 117, 148, 1)',
        //     type: 'dashed',
        //     dashOffset: 5, // 虚线偏移量
        //     curveness: 0 // 0-1 之间的数值,表示曲度,值越大曲度越大
        //   }
        // }
      ]
    },
    {
      type: 'lines',
      polyline: true, // 是否为多线段
      coordinateSystem: 'cartesian2d', // 坐标系类型

      lineStyle: {
        type: 'solid',
        width: 8,
        color: 'rgba(5, 98, 96, 1)'
      },
      effect: {
        period: 3, // 动画时间
        show: true,
        trailLength: 0, // 尾迹长度
        symbol: 'rect',
        color: 'rgba(0, 217,163, 1)',
        loop: true,
        symbolSize: [3, 10]
      },
      data: [
        {
          coords: [
            [20, 50],
            [50, 50]
          ]
        },
        {
          coords: [
            [50, 50],
            [80, 50]
          ]
        },
        {
          coords: [
            [80, 50],
            [80, 80]
          ]
        }
      ]
    }
  ]
}
</script>
可以使用 ECharts 的 tree 和 graph 组件来实现主机关联和详细数据的。下面是一个简单的示例: HTML 代码: ```html <div id="main" style="width: 100%; height: 800px;"></div> ``` JavaScript 代码: ```javascript // 初始化 echarts 实例 var myChart = echarts.init(document.getElementById('main')); // 配置项 var option = { title: { text: '主机关联和详细数据' }, tooltip: {}, animationDurationUpdate: 1500, animationEasingUpdate: 'quinticInOut', series: [ { type: 'graph', layout: 'none', symbolSize: 50, roam: true, label: { show: true }, edgeSymbol: ['circle', 'arrow'], edgeSymbolSize: [4, 10], edgeLabel: { fontSize: 20 }, data: [ { name: '主机1', x: 300, y: 300, itemStyle: { color: '#FF6600' } }, { name: '主机2', x: 800, y: 300, itemStyle: { color: '#FF6600' } }, { name: '详细数据1', x: 300, y: 500, itemStyle: { color: '#3399CC' } }, { name: '详细数据2', x: 800, y: 500, itemStyle: { color: '#3399CC' } } ], // 主机之间的连线 links: [ { source: '主机1', target: '主机2', symbolSize: [5, 20], label: { show: true }, lineStyle: { width: 5, curveness: 0.2, opacity: 0.5 } } ], lineStyle: { opacity: 0.9, width: 3, curveness: 0 } }, // 详细数据之间的关系 { type: 'tree', data: [ { name: '详细数据1', children: [ { name: '详细数据1-1' }, { name: '详细数据1-2' }, { name: '详细数据1-3' } ] }, { name: '详细数据2', children: [ { name: '详细数据2-1' }, { name: '详细数据2-2' } ] } ], top: '60%', bottom: '5%', left: '20%', right: '20%', symbolSize: 50, symbol: 'circle', orient: 'LR', label: { position: 'top', verticalAlign: 'middle', align: 'center', fontSize: 16 }, edgeShape: 'polyline', edgeForkPosition: '63%', initialTreeDepth: 2 } ] }; // 使用刚指定的配置项和数据显示表。 myChart.setOption(option); ``` 在这个示例中,我们使用了两个组件:`graph` 和 `tree`。`graph` 组件用于表示主机之间的关系,而 `tree` 组件用于表示详细数据之间的关系。 在 `graph` 组件中,我们定义了四个节点,分别表示两台主机和两个详细数据。我们还定义了一条主机之间的连线。我们使用了 `lineStyle` 属性来设置连线的样式。 在 `tree` 组件中,我们定义了两个根节点,分别表示两个详细数据,每个节点下面再分别定义了几个子节点。我们使用了 `edgeShape` 属性来设置连线的样式。 最后,我们将 `option` 对象传递给 `setOption` 方法,用于显示表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值