echarts 字体自适应的方法(使用rem)

采用rem为单位,根据屏幕的宽度调整html的font-size。

获取屏幕宽度并计算比例:

function fontSize(res){
	let docEl = document.documentElement,
		clientWidth = window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth;
	if (!clientWidth) return;
	let fontSize = 100 * (clientWidth / 1920);
	return res*fontSize;
}

在需要设置字体的地方可以这样写,
如在1920屏宽下字体设置为12px,就可以传入0.12给fontSize fontSize(0.12)

tooltip : {
	trigger: 'axis',
	textStyle:{
		fontSize: fontSize(0.12),
	}
},

你可能感兴趣的:(echart)