echarts设置背景图片,且能够自适应宽高

  1. 设置option的backgroundcolor属性
	var img = new Image();
	img.src = '图片的url';
	
	option = {
		backgroundColor = {
			image: img ,
			repeat: "no-repeat",
		},
		...
	}

2.自适应图表宽高

    //echarts: 当前图表
    let fullImage = new Image();
	let img = new Image();
	let canvas = document.createElement('canvas');
	let ctx = canvas.getContext('2d');
	canvas.width = echarts.getWidth() * window.devicePixelRatio;
	canvas.height = echarts.getHeight() * window.devicePixelRatio;
	img.onload = () => {
		ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
		fullImage.src = canvas.toDataURL();
		echarts.resize();
	};
	img.src = require("../../../assets/echarts-bg.jpg");
	
	option = {
		backgroundColor = {
			image: fullImage,
			repeat: "no-repeat",
		},
		...
	}
  1. 可能会先加载默认黑色背景再加载图片,页面闪现,解决方法如下:
    fullImage设置为全局变量,初始化时为fullImage赋值
	mounted() {
		this.myOption = this.$echarts.init(this.$el);
			this.fullImage = new Image();
			let img = new Image();
			let canvas = document.createElement('canvas');
			let ctx = canvas.getContext('2d');
			canvas.width = this.myOption.getWidth() * window.devicePixelRatio;
			canvas.height = this.myOption.getHeight() * window.devicePixelRatio;
			img.onload = () => {
				ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
				this.fullImage.src = canvas.toDataURL();
				this.myOption.resize();
			};
			img.src = require("../../../assets/echarts-bg.jpg");
	}

初始化图标时设置定时器

	option = {
			backgroundColor = {
				image: this.fullImage,
				repeat: "no-repeat",
			},
			...
		}
    setTimeout(()=>{
		echarts.setOption(option)
	}, 100)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值