echarts树图修改连线样式颜色,树的形状曲线和折线,树图边的曲度

最终实现样式:

鼠标移入任意子节点展示:

 

 实现代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- 引入 jquery.js -->
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <!--直接cdn引入的echarts-->
    <script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
    <style>
        *{padding: 0;margin: 0;}
    </style>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div class="tree-container">
    <div id="main" style="width: 1000px;height:600px;"></div>
</div>
 
<script type="text/javascript">
    // 基于准备好的dom,初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));
    var data = {
        "id": "0",
        "name": "外部网络",
        "type":"Internet",
        "symbol": 'image://image/internet.png',
        "children": [
            {
                "id": "1",
                "name": "交换机",
                "type":"switch",
                "IP":"192.168.30.126",
                "MAC":"b0:98:6e:bf:6r:4c",
                "deviceType":"交换机",
                "deviceNum":"HUAWEI",
                "symbol": 'image://image/switch.png',
                "children": [
                    {
                        "id": "2",
                        "name": "笔记本",
                        "type":"switch",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "symbol": 'image://image/network.png',
                    },
                    {
                        "id": "3",
                        "name": "计算机",
                        "type":"computer",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "symbol": 'image://image/computer.png',
                        "children": [
                            {
                                "id": "4",
                                "name": "计算机1",
                                "type":"computer",
                                "IP":"192.168.30.126",
                                "MAC":"b0:98:6e:bf:6r:4c",
                                "symbol": 'image://image/computer.png',
                            },
                            {
                                "id": "5",
                                "name": "计算机2",
                                "type":"computer",
                                "IP":"192.168.30.126",
                                "MAC":"b0:98:6e:bf:6r:4c",
                                "symbol": 'image://image/computer.png',
                            },
                            {
                                "id": "6",
                                "name": "计算机3",
                                "type":"computer",
                                "IP":"192.168.30.126",
                                "MAC":"b0:98:6e:bf:6r:4c",
                                "symbol": 'image://image/computer.png',
                            },
                            {
                                "id": "7",
                                "name": "计算机4",
                                "type":"computer",
                                "IP":"192.168.30.126",
                                "MAC":"b0:98:6e:bf:6r:4c",
                                "lastLoginTime":"2020-8-26",
                                "symbol": 'image://image/computer.png',
                            }
                        ]
                    },
                    {
                        "id": "8",
                        "name": "路由器",
                        "type":"rooter",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"路由器",
                        "symbol": 'image://image/rooter.png',
                    },
                    {
                        "id": "9",
                        "name": "服务器",
                        "type":"service",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"服务器",
                        "symbol": 'image://image/service.png',
                    },
                    {
                        "id": "10",
                        "name": "打印机",
                        "type":"print",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"打印机",
                        "symbol": 'image://image/print.png',
                    },
                    {
                        "id": "11",
                        "name": "计算机",
                        "type":"computer",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "lastLoginTime":"2020-8-26",
                        "symbol": 'image://image/computer.png',
                    }
                ]
            },
            {
                "id": "12",
                "name": "无线交换机",
                "type":"switch",
                "IP":"192.168.30.126",
                "MAC":"b0:98:6e:bf:6r:4c",
                "deviceType":"交换机",
                "deviceNum":"HUAWEI",
                "symbol": 'image://image/switch.png',
                "children": [
                    {
                        "id": "13",
                        "name": "手机",
                        "type":"phone",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"手机",
                        "symbol": 'image://image/phone.png',
                    },
                    {
                        "id": "14",
                        "name": "平板",
                        "type":"phone",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"平板",
                        "symbol": 'image://image/phone.png',
                    }
                ]
            },
            {
                "id": "15",
                "name": "hub",
                "type":"hub",
                "symbol": 'image://image/hub.png',
                "children": [
                    {
                        "id": "16",
                        "name": "计算机",
                        "type":"computer",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"计算机",
                        "symbol": 'image://image/computer.png',
                    },
                    {
                        "id": "17",
                        "name": "笔记本",
                        "type":"phone",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"手机",
                        "symbol": 'image://image/phone.png',
                    },
                    {
                        "id": "18",
                        "name": "打印机",
                        "type":"print",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"打印机",
                        "symbol": 'image://image/print.png',
                    },
                    {
                        "id": "19",
                        "name": "路由器",
                        "type":"rooter",
                        "IP":"192.168.30.126",
                        "MAC":"b0:98:6e:bf:6r:4c",
                        "deviceType":"路由器",
                        "symbol": 'image://image/rooter.png',
                    }
                ]
            }
        ]
    };
    //		页面初始化的时候,隐藏奇数位的子节点
    		// echarts.util.each(data.children, function (datum, index) {
    	    //     index % 2 === 0 && (datum.collapsed = true);
    	    // });
            // console.log('data',data);
    var option = {
        tooltip: {
            trigger: 'item',
            triggerOn: 'mousemove',
            enterable:true,//鼠标是否可进入提示框浮层中
            formatter:formatterHover,//修改鼠标悬停显示的内容
        },
        toolbox: {
            show: true,
            top:20,
            left:20,
            feature: {
                restore: {
                    title:'刷新'//刷新echarts图标
                },
                saveAsImage: {
                    title:'下载图片',//鼠标悬停在下载图标上时,显示的文字
                    name:'network-topology'//下载图片的文件名为network-topology.png
                }
            }
        },
        series: [
            {
                type: 'tree',
                data: [data],
                top: '1%',
                left: '7%',
                bottom: '1%',
                right: '20%',
                // edgeShape: 'polyline', // 修改树的形状为折线
                label: {
                    position: 'left',
                    verticalAlign: 'middle',
                    align: 'right',
                    color: '#666',
                    fontSize: 9
                },
                lineStyle: {
                    color: '#2E8B57',
                    curveness: 1, // 修改树的曲度
                },
                orient: 'LR',
                leaves: {
                    label: {
                        position: 'right',
                        verticalAlign: 'middle',
                        align: 'left'
                    }
                },
                symbolSize: [30, 30],
                edgeForkPosition: "72%",
                emphasis: { // 高亮
                    focus: 'descendant'
                },
                initialTreeDepth:2, // 树图初始展开层级
                roam:true,//鼠标缩放,拖拽整颗树
                expandAndCollapse: true,//无关的子树折叠收起
                animationDuration: 550,
                animationDurationUpdate: 750,
                width: "50%"//组件宽度
            }
        ]
    };
    /**
     * 鼠标悬停时显示详情
     */
    function formatterHover(params){
        // console.log(params.collapsed);
        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="padding:0 5px;font-size: 14px;">'+ params.data.name+'</span>';
        }  if (deviceType === 'switch'){
            return "<img src='"+imgPathSrc+" ' width='30px' height='30px'>" + '<span style="padding: 0 5px;font-size: 14px;">设备类型:'+ params.data.name+'</span>'+ '<br>'
                + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;font-size: 14px;">IP:'+ params.data.IP+'</span>'+ '<br>'
                + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;font-size: 14px;">MAC:'+ params.data.MAC+'</span>'+ '<br>';
                // +'<button style="padding:2px 5px;border:none;outline:none;color:#ffffff;border-radius: 5px;background:rgba(0,0,0,0.5);"><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> IP列表</button>'
                // +'<button style="padding:2px 5px;border:none;outline:none;color:#ffffff;border-radius: 5px;background:rgba(0,0,0,0.5);margin-left: 10px;"><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> MAC列表</button>';
        }else{
            return "<img src='"+imgPathSrc+" ' width='30px' height='30px'>" + '<span style="padding: 0 5px;font-size: 14px;">设备类型:'+ params.data.name+'</span>'+ '<br>'
                + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;font-size: 14px;">IP:'+ params.data.IP+'</span>'+ '<br>'
                + '<span style="padding-left:5px;height:30px;line-height:30px;display: inline-block;font-size: 14px;">MAC:'+ params.data.MAC+'</span>'+ '<br>';
        }
    }
    /**
     * 解决echarts图片首次加载不显示的问题
     */
    setTimeout(function(){
        $(myChart).resize();
    },200);
    /**
     * 解决点击父节点合并或展开后子节点图片不显示的问题
     */
    $(window).on('click',function(){
        $(myChart).resize();
    });
    /**
     * 遍历数节点
     */
    function readNodes (nodes) {
        for (let item of nodes) { // js遍历树形数组结构
            console.log('item', item)
            if (item.children && item.children.length){
                readNodes(item.children)
            }
            if (item.id == 1 || item.id == 14) {
                // 修改连线颜色
                item.lineStyle = {
                    color: '#2E8B57'
                }
            } else {
                item.lineStyle = {
                    color: '#ff0000'
                }
            }
        }
    }
    readNodes([data]); // 调用时,要给data加[],因为原本的data不是一个正常的数组结构
    myChart.setOption(option); // 重新设置一遍树图,不然不起效果
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值