vue2 eCharts实现自定义节点图标 graph关系图

先展示最后效果图

在查阅eCharts官网的配置项手册会发现提供了自定义节点图标的方法:

代码如下,可直接复制:

(注释已标记)

 <div class="container">
    <div class="title">
      <span class="circle"></span>
      <span>网络健康状态拓扑图</span>
    </div>
    <div class="chart" ref="chart"></div>
  </div>
import * as echarts from "echarts";
import platform from "@/assets/img/vedio/platform.png"
import platform1 from "@/assets/img/vedio/platform1.png"
import device from "@/assets/img/vedio/device.png"
export default {
  data() {
    return {
      chart: null,
      chartData: [],
      symbolWidth:160,  //节点图片的宽度
      symbolHeight:97,  //节点图片的高度
      linksData:[],   //节点之间的连接关系
    };
  },
  methods: {
    initChart() {
      let chartsData = {
        nodes: [],
      };
      for (var j = 0; j < this.chartData.length; j++) {
        const { x, y, nodeName, iconPath,name } = this.chartData[j];
        var node = {
          nodeName,
          value: [x, y],
          symbolSize: [this.symbolWidth,this.symbolHeight],
          symbol: "image://" + iconPath,   //自定义节点的图标
          itemStyle: {
            color: "orange",
          },
          name:name
        };
        chartsData.nodes.push(node);
      }
      this.chart = echarts.init(this.$refs.chart);
      this.chart.setOption({
        grid:{
          left:0,
          right:0,
          top:0,
          bottom:0
        },
        xAxis: {
          min: 0,
          max: this.$refs.chart.clientWidth,
          show: false,
          type: "value",
        },
        yAxis: {
          min: 0,
          max: this.$refs.chart.clientHeight,
          show: false,
          type: "value",
        },
        series: [
          {
            type: "graph",  //指定图表类型为关系图
            coordinateSystem: "cartesian2d",
            label: {
              show: true,
              position: "bottom",
              color: "#5D5D5D",
              formatter: function(item) {
                return item.data.nodeName;
              },
            },
            data: chartsData.nodes,  //图表数据
            links: this.linksData   //各个节点指向关系
          },
        ],
      });
    },
    setData1(arr,obj,level){
      level++
      if(!obj['level' + level]){
        obj['level' + level] = []
      }
      obj['level' + level] = obj['level' + level].concat(arr)
      arr.map((item)=>{
        if(item.children && item.children.length != 0){
          this.setData1(item.children,obj,level)
        }
        if(item.parentId){
          let isPush = true
          this.linksData.map((item1)=>{
            if(item1.source === item.id && item1.target === item.parentId){
              isPush = false
            }
          })
          if(isPush){
            this.linksData.push({
              source: item.id,   //当前节点
              target: item.parentId,  //连接的目标节点
              lineStyle: {  //连接线的样式
                width: 4,
                color: item.isOnLine?"#04D036":"#FF3C3C",
              }
            })
          }
        }
      })
      return obj
    },
    setData(obj,arr1,width,height){ //根据类型不同显示不同的节点图片
      let keys = Object.keys(obj)
      keys.map((parent,level)=>{
        obj[parent].map((item,index)=>{
          let iconPath;
          if(item.type === 1){
            iconPath = platform
          }else if(item.type === 2){
            iconPath = platform1
          }else if(item.type === 3){
            iconPath = device
          }
          let width1 = (width / obj[parent].length)
          width1 = (width1 / 2) + index * width1
          let height1 = (height / keys.length)
          height1 = (height1 - this.symbolHeight) * (level + 1)
          height1 = height - height1
          if(level === 0){
            height1 = height - this.symbolHeight
          }
          if(level === keys.length - 1){
            height1 = 50 + this.symbolHeight
          }
          arr1.push({
            iconPath:iconPath,
            // iconPath:'',
            nodeName:item.name,
            x:width1,
            y:height1,
            name:item.id
          })
        })
      })
      return arr1
    },
  },
  mounted() {
    let data = [
      {
        name:"平台",
        type:1,
        id:"n1",
        children:[
          {
            id:"n11",
            parentId:"n1",
            isOnLine:true,
            name:"平台1",
            type:2,
            children:[
              {
                id:"n111",
                parentId:"n11",
                isOnLine:true,
                name:"设备1",
                type:3,
                children:[]
              },
              {
                id:"n112",
                parentId:"n11",
                isOnLine:true,
                name:"平台11",
                type:2,
                children:[]
              },
            ]
          },
          {
            id:"n12",
            parentId:"n1",
            isOnLine:true,
            name:"平台2",
            type:2,
            children:[
              {
                id:"n121",
                parentId:"n12",
                isOnLine:true,
                name:"设备2",
                type:3,
                children:[]
              },
              {
                id:"n122",
                parentId:"n12",
                isOnLine:true,
                name:"平台22",
                type:2,
                children:[]
              },
            ]
          },
          {
            id:"n13",
            parentId:"n1",
            isOnLine:false,
            name:"平台3",
            type:2,
            children:[
              {
                id:"n131",
                parentId:"n13",
                isOnLine:false,
                name:"设备3",
                type:3,
                children:[]
              },
              {
                id:"n132",
                parentId:"n13",
                isOnLine:false,
                name:"平台33",
                type:2,
                children:[]
              },
            ]
          },
          {
            id:"n14",
            parentId:"n1",
            isOnLine:false,
            name:"平台4",
            type:2,
            children:[
              {
                id:"n141",
                parentId:"n14",
                isOnLine:true,
                name:"设备4",
                type:3,
                children:[]
              },
              {
                id:"n142",
                parentId:"n14",
                isOnLine:true,
                name:"平台44",
                type:2,
                children:[]
              },
            ]
          },
        ]
      },
    ]
    let chartObj = this.$refs.chart
    data = this.setData1(data,{},-1)
    let data1 = this.setData(data,[],chartObj.clientWidth,chartObj.clientHeight,-1)
    this.chartData = data1
    this.initChart();
  },
};
.container {
  width: 100%;
  height: 100%;
  background: #ffffff;
  border-radius: 0.06rem;
  padding: 0.24rem;
  position: relative;
  background-image: url(~@/assets/img/vedio/background.png);
  background-size: cover;
  .title{
    position: absolute;
    top: 20px;
    left: 20px;
    display: flex;
    align-items: center;
    .circle{
      width: 8px;
      height: 8px;
      background: #1D39C4;
      border: 1px solid #B4C0DA;
      margin-right: 5px;
    }
  }
  .chart {
    width: 100%;
    height: 100%;
    border-radius: 0.06rem;
    border: 0.01rem solid #ffffff;
  }
}

如有问题欢迎留言!!

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Vue中使用echarts实现折线,你需要按照以下步骤进行操作: 1. 首先,安装echarts库。你可以使用npm或yarn来安装echarts,具体命令如下: ``` npm install echarts ``` 或 ``` yarn add echarts ``` 2. 在Vue组件中引入echarts库。你可以在组件的`<script>`标签中使用`import`语句引入echarts库,如下所示: ```javascript import echarts from 'echarts'; ``` 3. 在Vue组件的模板中准备一个具备大小的容器div,用于显示折线。你可以给这个div设置一个id,如`main`,并设置宽度和高度,如下所示: ```html <template> <div id="app"> <!--为echarts准备一个具备大小的容器dom--> <div id="main" style="width: 600px; height: 400px"></div> </div> </template> ``` 4. 在Vue组件的`mounted`钩子函数中编写代码来绘制折线。你可以使用`echarts.init`方法初始化一个echarts实例,并传入容器的id,如`main`。然后,使用`setOption`方法设置折线的配置项,如x轴、y轴、数据等,如下所示: ```javascript export default { name: 'YourComponentName', mounted() { this.$nextTick(function () { let charts = echarts.init(document.getElementById('main')); charts.setOption({ // 设置折线的配置项 // ... }); }); }, }; ``` 通过以上步骤,你就可以在Vue中使用echarts实现折线了。记得根据你的需求修改折线的配置项,以及在`setOption`方法中设置相应的数据。 #### 引用[.reference_title] - *1* *3* [vue中如何使用echarts——以折线为例](https://blog.csdn.net/weixin_61032994/article/details/124023735)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [在Vue中使用Echarts](https://blog.csdn.net/qq_36538012/article/details/109571270)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值