echarts修改鼠标悬停在节点上时显示的内容,自定义鼠标悬停显示内容

首先看一下效果,如下图所示:

效果图

代码部分:

1.在option对象下的tooltip对象中添加formatter函数,代码如下:

var option = {
    tooltip: {
	 trigger: 'item',
	 triggerOn: 'mousemove',
         enterable:true,//鼠标是否可进入提示框浮层中
          formatter:formatterHover,//修改鼠标悬停显示的内容
    },
}

2.编写formatter函数,根据自己的需求编写,代码如下:

/**
* 鼠标悬停时显示详情
 */
function formatterHover(params){
     // console.log(params);
     var deviceType = params.data.type;
     var imgPath = params.data.symbol;
     //图片地址截取,因为echarts修改图片的时候有一个------image://---前缀,前缀后面的才是图片真正的地址
    var imgPathSrc = imgPath.split("image://")[1];
    // console.log('str',imgPathSrc);
    if (deviceType === 'Internet' || deviceType === 'hub'){
        return "<img src='"+imgPathSrc+" ' width='30px' height='30px'>" + '<span style="position: relative;top: -10px;padding:0 5px;">'+ params.data.name+'</span>';
    }  if (deviceType === 'switch'){
        return "<img src='"+imgPathSrc+" ' width='30px' height='30px'>" + '<span style="position: relative;top: -10px;padding: 0 5px;">设备类型:'+ params.data.name+'</span>'+ '<br>'
         + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">IP:'+ params.data.IP+'</span>'+ '<br>'
         + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">MAC:'+ params.data.MAC+'</span>'+ '<br>'
         + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">设备型号:'+ params.data.deviceNum+'</span>';
    }else{
        return "<img src='"+imgPathSrc+" ' width='30px' height='30px'>" + '<span style="position: relative;top: -10px;padding: 0 5px;">设备类型:'+ params.data.name+'</span>'+ '<br>'
        + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">IP:'+ params.data.IP+'</span>'+ '<br>'
        + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">MAC:'+ params.data.MAC+'</span>'+ '<br>'
        + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">账号:'+ params.data.account+'</span>'+ '<br>'
        + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">所在位置:'+ params.data.location+'</span>'+ '<br>'
        + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">最后登录时间:'+ params.data.lastLoginTime+'</span>';
        }
}

3.完整代码如下:

注:

1.代码中的图片是本地image文件夹下的图片,直接运行图片应该会报错,放到idea或Visual Studio Code下运行不会报错

2.image下的图片要换成自己文件夹下的图片,如果不想换,可参考我另一篇文章,用服务器图片:

https://blog.csdn.net/qq_36509946/article/details/108219598

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- 引入 echarts.js -->
	<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="js/echars.js"></script>
</head>
<body>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 1000px;height:500px;"></div>
    <div class="right-click-menu" style="display: none;padding: 5px;border: 1px solid #CCCCCC;width: 200px;">右键操作菜单</div>
    <script type="text/javascript">
        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
		var data = {
		  "name": "外部网络",
          "type":"Internet",
          "symbol": 'image://image/internet.png',
		  "children": [
		    {
                  "name": "交换机",
                  "type":"switch",
                  "IP":"192.168.30.126",
                  "MAC":"b0:98:6e:bf:6r:4c",
                  "deviceType":"交换机",
                  "deviceNum":"HUAWEI",
                  "symbol": 'image://image/switch.png',
		      "children": [
		        {
		            "name": "笔记本",
                    "type":"network",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"笔记本",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/network.png',
		        },
		        {
		            "name": "计算机",
                    "type":"computer",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"计算机",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/computer.png',
		          "children": [
			        {
			            "name": "计算机1",
                        "type":"computer",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"计算机1",
                        "account":"xiaox",
                        "location":"204",
                        "lastLoginTime":"2020-8-26",
                        "symbol": 'image://image/computer.png',
			        },
			        {
			            "name": "计算机2",
                        "type":"computer",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"计算机2",
                        "account":"xiaox",
                        "location":"204",
                        "lastLoginTime":"2020-8-26",
                        "symbol": 'image://image/computer.png',
			        },
			        {
			            "name": "计算机3",
                        "type":"computer",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"计算机3",
                        "account":"xiaox",
                        "location":"204",
                        "lastLoginTime":"2020-8-26",
                        "symbol": 'image://image/computer.png',
			        },
			        {
			            "name": "计算机4",
                        "type":"computer",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"计算机4",
                        "account":"xiaox",
                        "location":"204",
                        "lastLoginTime":"2020-8-26",
                        "symbol": 'image://image/computer.png',
			        }
			      ]
		        },
		        {
		            "name": "路由器",
                    "type":"rooter",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"路由器",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/rooter.png',
		        },
		        {
		            "name": "服务器",
                    "type":"service",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"服务器",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/service.png',
		        },
		        {
		            "name": "打印机",
                    "type":"print",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"打印机",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/print.png',
		        },
		        {
		            "name": "计算机",
                    "type":"computer",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"计算机",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/computer.png',
		        }
		      ]
		    },
		    {
		      "name": "无线交换机",
              "type":"switch",
              "IP":"192.168.30.126",
              "MAC":"b0:98:6e:bf:6r:4c",
              "deviceType":"交换机",
              "deviceNum":"HUAWEI",
              "symbol": 'image://image/switch.png',
		      "children": [
		        {
		            "name": "手机",
                    "type":"phone",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"手机",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/phone.png',
		        },
		        {
		            "name": "平板",
                    "type":"phone",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"平板",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/phone.png',
		        }
		      ]
		    },
		    {
		      "name": "hub",
              "type":"hub",
              "symbol": 'image://image/hub.png',
		      "children": [
		        {
		            "name": "计算机",
                    "type":"computer",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"计算机",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/computer.png',
		        },
		        {
		            "name": "笔记本",
                    "type":"phone",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"手机",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/phone.png',
		        },
		        {
		            "name": "打印机",
                    "type":"print",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"打印机",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/print.png',
		        },
		        {
		            "name": "路由器",
                    "type":"rooter",
                    "IP":"192.168.30.126",
                    "MAC":"b0:98:6e:bf:6r:4c",
                    "deviceType":"路由器",
                    "account":"xiaox",
                    "location":"204",
                    "lastLoginTime":"2020-8-26",
                    "symbol": 'image://image/rooter.png',
		        }
		      ]
		    }
		  ]
		};
//		页面初始化的时候,隐藏奇数位的子节点
//		echarts.util.each(data.children, function (datum, index) {
//	        index % 2 === 0 && (datum.collapsed = true);
//	    });

		var option = {
			tooltip: {
	            trigger: 'item',
	            triggerOn: 'mousemove',
                enterable:true,//鼠标是否可进入提示框浮层中
                formatter:formatterHover,//修改鼠标悬停显示的内容
	       },
	        series: [
	            {
	                type: 'tree',
	                data: [data],
	                top: '1%',
	                left: '7%',
	                bottom: '1%',
	                right: '20%',
	                label: {
	                    position: 'left',
	                    verticalAlign: 'middle',
	                    align: 'right',
	                    fontSize: 9
	                },
	                leaves: {
	                    label: {
	                        position: 'right',
	                        verticalAlign: 'middle',
	                        align: 'left'
	                    }
	                },
                        symbolSize: [30, 30],
	                edgeForkPosition: "72%",
	                roam:true,//鼠标缩放,拖拽整颗树
	                expandAndCollapse: true,//无关的子树折叠收起
	                animationDuration: 550,
	                animationDurationUpdate: 750,
	                width: "50%"//组件宽度
	            }
	        ]
		}
        /**
         * 鼠标悬停时显示详情
         */
		function formatterHover(params){
		    // console.log(params);
		    var deviceType = params.data.type;
		    var imgPath = params.data.symbol;
		    //图片地址截取,因为echarts修改图片的时候有一个------image://---前缀,前缀后面的才是图片真正的地址
            var imgPathSrc = imgPath.split("image://")[1];
            // console.log('str',imgPathSrc);
		    if (deviceType === 'Internet' || deviceType === 'hub'){
		        return "<img src='"+imgPathSrc+" ' width='30px' height='30px'>" + '<span style="position: relative;top: -10px;padding:0 5px;">'+ params.data.name+'</span>';
                // return firstParams.name + '  ' + firstParams.seriesName + '<br>' + '装机:' + firstParams.data + ' 亿千瓦<br>增长率:' + sndParams.data +' %';
            }  if (deviceType === 'switch'){
                return "<img src='"+imgPathSrc+" ' width='30px' height='30px'>" + '<span style="position: relative;top: -10px;padding: 0 5px;">设备类型:'+ params.data.name+'</span>'+ '<br>'
                    + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">IP:'+ params.data.IP+'</span>'+ '<br>'
                    + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">MAC:'+ params.data.MAC+'</span>'+ '<br>'
                    + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">设备型号:'+ params.data.deviceNum+'</span>';
            }else{
                return "<img src='"+imgPathSrc+" ' width='30px' height='30px'>" + '<span style="position: relative;top: -10px;padding: 0 5px;">设备类型:'+ params.data.name+'</span>'+ '<br>'
                    + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">IP:'+ params.data.IP+'</span>'+ '<br>'
                    + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">MAC:'+ params.data.MAC+'</span>'+ '<br>'
                    + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">账号:'+ params.data.account+'</span>'+ '<br>'
                    + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">所在位置:'+ params.data.location+'</span>'+ '<br>'
                    + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;">最后登录时间:'+ params.data.lastLoginTime+'</span>';
            }

        }
		/**
		 * 解决echarts图片首次加载不显示的问题
		 */
		setTimeout(function(){
		     $(myChart).resize();     
		},200)
		/**
		 * 解决点击父节点合并或展开后子节点图片不显示的问题
		 */
		$(window).on('click',function(){
			$(myChart).resize();
		})
	    myChart.setOption(option);
    </script>
</body>
</html>

 

  • 4
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值