echart拓扑图实现过程

本文详细介绍了如何使用ECharts库在div上创建一个具有定制提示的图形化拓扑图,包括canvas转div的注意事项,tooltip配置,以及hover事件的自定义 formatterHover 方法。遇到的问题和解决方案也一并分享,适合初学者和进阶者参考。
摘要由CSDN通过智能技术生成
看效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
链接地址:官方地址echart
实现源码:

首先说一下,实现这个过程很简单,不过坑还是有一些的,今天主要就是说一下这些坑,上面的图是我实现的结果

let topology = res.data.items
                        let X = 200
                        let Y = 200
                        let dataSource = [{
							                name: '节点1',
							                x: 300,
							                y: 300
							            }, {
							                name: '节点2',
							                x: 800,
							                y: 300
							            }, {
							                name: '节点3',
							                x: 550,
							                y: 100
							            }, {
							                name: '节点4',
							                x: 550,
							                y: 500
							            }],
						let links = [{
							                source: 0,
							                target: 1,
							                symbolSize: [5, 20],
							                label: {
							                    show: true
							                },
							                lineStyle: {
							                    width: 5,
							                    curveness: 0.2
							                }
							            }, {
							                source: '节点2',
							                target: '节点1',
							                label: {
							                    show: true
							                },
							                lineStyle: {
							                    curveness: 0.2
							                }
							            }, {
							                source: '节点1',
							                target: '节点3'
							            }, {
							                source: '节点2',
							                target: '节点3'
							            }, {
							                source: '节点2',
							                target: '节点4'
							            }, {
							                source: '节点1',
							                target: '节点4'
							            }],

                        let option = {
                            title: {
                                text: this.title
                            },
                            tooltip: {
                                //show: true,
                                //enterable: true,//鼠标是否可进入提示框浮层中
                                formatter: formatterHover,//修改鼠标悬停显示的内容
                            },
                            legend: [{
                                data: dataSource.map(function (a) {
                                    return a.name;
                                })
                            }],
                            series: [
                                {
                                    type: 'graph',
                                    layout: 'none', //none' 不采用任何布局,使用节点中提供的 x, y 作为节点的位置.'circular' 采用环形布局,'force' 采用力引导布局。
                                    symbolSize: 50,
                                    //categories: dataSource, //是否显示图例
                                    focusNodeAdjacency: true,  //鼠标放置上去 显示当前的连接
                                    draggable: true, //是否支持拖拽   只有layout 为 force 的时候才可以用
                                    roam: true, //是否支持拖动和缩放
                                    animation: true, //是否需要加载
                                    animationDurationUpdate: 5000,//加载动画时间
                                    animationEasingUpdate: 'backIn',//动画类型
                                    label: {
                                        normal: {
                                            show: true,//是否显示标签。
                                            position: 'bottom',//标签的位置。['50%', '50%'] [x,y]   'inside'
                                            textStyle: { //标签的字体样式
                                                color: '#3489ff', //字体颜色
                                                fontStyle: 'normal',//文字字体的风格 'normal'标准 'italic'斜体 'oblique' 倾斜
                                                fontWeight: 'bolder',//'normal'标准'bold'粗的'bolder'更粗的'lighter'更细的或100 | 200 | 300 | 400...
                                                fontFamily: 'sans-serif', //文字的字体系列
                                                fontSize: 12, //字体大小
                                            }
                                        },
                                    },
                                    itemStyle: {
                                        borderColor: '#fff',
                                        borderWidth: 1,
                                        shadowBlur: 10,
                                        shadowColor: 'rgba(0, 0, 0, 0.3)'
                                    },
                                    edgeSymbol: ['none', 'arrow'],
                                    data: dataSource,
                                    links: links,
                                    lineStyle: {
                                        opacity: 0.9,
                                        width: 2,
                                        curveness: 0,
                                        color: '#000000'
                                    },
                                    emphasis: {
                                        lineStyle: {
                                            width: 6
                                        }
                                    }
                                }
                            ],
                            markPoint: {},
                        };
                        function formatterHover(params) {
                            let currData = params.data
                            if (params.dataType === 'node') {
                            //鼠标到节点
                                return currData.name
                            } else {
                            //鼠标到线条
                                return currData.name
                            }
                        }
                        currTopology.setOption(option)

注意的点1:
如果使用的是canvas进行绘制的,那么我们的tooltip不显示,解决方案是将canvas改为div就可以了

<!--<canvas width="1920" height="600" id="canvas"
                         style="background-color: #FFFFFF" ref="topology"></canvas>-->
<div id="canvas"
                         style="background-color: #FFFFFF;width:1920px ;height:600px" ref="topology"></div>

注意的点2:
formatterHover 该函数需要写到我们的方法中,function定义,然后进行html的构建。

注意的点3:具体怎么引入的echart,我之前的博客中有写到,npm install就可以了。

具体说我一个月没有更新博客了,也没啥解释的了,都是我的问题,关于这个拓扑图实现过程中有什么问题,可以私信我,或者下面评论!看到了我都会回复的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是悟能了

阅读就是对我最大的支持,感谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值