Echart自适应图表大小和字体大小

思路:监听window的resize事件,获取屏幕宽度,计算出设计图下的字体大小,通过通过echart实例的setOption()和resize()方法就可以。其中,setOption方法是用来重新设置字体大小等参数和数据,echart会自动合并这些参数,resize是用来在容器发生改变时该表图表尺寸的。

主要代码如下:

window.addEventListener('resize', () => {
			//新的配置参数,会和老的配置参数合并
            var newOption ={
                series: [
                    {
                        name: '能源消耗',
                        type: 'pie',
                        radius: '65%',
                        center: ["45%", "50%"],
                        data: data,
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        },
                        itemStyle: {
                            borderColor: '#fff',
                            borderWidth: 3
                        },
                        label: {
                            normal: {
                                textStyle: {
                                    color: '#444',
                                    fontSize: 16 * pxRatio//pxRatio是根据window的resize动态计算的
                                },
                                formatter: '{center|{b}}\n{d}吨',
                                padding: 10 * pxRatio,
                                borderWidth: 1,
                                borderColor: '#333',
                                borderRadius: 10,
                                rich: {
                                    center: {
                                        align: 'center',
                                        padding: [0, 0, 8, 0],
                                        fontSize: 16 * pxRatio,
                                        fontWeight: 'bolder'
                                    }
                                }
                            }
                        }
                    },
                    {
                        name: '能源消耗',
                        type: 'pie',//显示百分比的pie图
                        radius: '65%',
                        data: data,
                        center: ["45%", "50%"],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        },
                        itemStyle: {
                            borderColor: '#fff',
                            borderWidth: 3
                        },
                        label: {
                            normal: {
                                show: true,
                                position: 'inside',
                                formatter: '{d}%',
                                textStyle: {
                                    color: "#fff",
                                    fontSize: 16 * pxRatio
                                }
                            }
    
                        }
                    }
                ]
            }
            myChart.setOption(newOption)
            myChart.resize()
        })
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值