用Echarts画力导向图,节点文字随鼠标缩放而缩放

项目场景:

用Echarts画力导向图,节点文字随鼠标缩放而缩放

解决方案:

用原生函数getZr()或_zr()实现,例子如下:

this.nodes=[],//节点数据
this.links=[];//边数据
let fontSize=12;//初始化字体大小
let myForce = echarts.init(document.getElementById('myForce'));
let option={
	 title:{
	     show : true,
	          text:'路径图'
	      }
	 },
	  series: [
	  	{
             type: 'graph',
             layout: 'force',//力引导布局
             symbolSize: 50,
             roam: true,//开启鼠标缩放和平移
             edgeSymbol: ['circle', 'arrow'],// 边两端的标记类型
             edgeSymbolSize: [4, 15],
             force: {
                 repulsion: 1500,//节点之间的距离
                 edgeLength: [10, 40],
                 layoutAnimation:true
             },
             draggable: true,
             itemStyle: {
                 normal: {
                     color: '#4b565b'
                 }
             },
             lineStyle: {
                 width: 2,
             },
             edgeLabel: {//关系边的公用线条
                 normal: {
                     show: true,
                     textStyle: {
                         fontSize: fontSize,
                         color:'black',
                     },
                     formatter: function (x) {
                         return x.data.relation;
                     }
                 }
             },
             label: {  //节点圈里的字体
                 normal: {
                     show: true,
                     position:'bottom',
                     distance : 15,
                     fontSize :fontSize,
                     color:'black'
                 }
             },
             data: this.nodes,
             links:this.links
         }
     ]
     	 myForce.clear();
         myForce.setOption(option,true);
         //鼠标点击节点位置不跳动
         myForce.on('mouseup', function (params) {
             var option = myForce.getOption();
             //自己定义位置
             option.series[0].data[params.dataIndex].x = params.data.x;
             option.series[0].data[params.dataIndex].y = params.data.y;
             option.series[0].data[params.dataIndex].fixed = true;
             //设置文字
             option.series[0].label.fontSize = fontSize;
             // option.series[0].label.lineHeight=fontSize * 1.25;
             option.series[0].edgeLabel.textStyle.fontSize = fontSize;
             myForce.setOption(option);
         });
         let preZoom = 1;
         //设置文字(缩放文字)
         myForce.getZr().on("mousewheel", function () {
             var option = myForce.getOption();
             var zoom = option.series[0].zoom;
             if (zoom !== preZoom) {
                 preZoom = zoom;
                 fontSize = 12 * zoom;
                 option.series[0].label.fontSize = fontSize;
                 option.series[0].label.lineHeight = fontSize * 1.25;
                 option.series[0].edgeLabel.textStyle.fontSize = fontSize;
                 myForce.setOption(option);
             }
         });
         window.onresize = function() {
             myForce.resize();
		}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值