echart保存图片的两种实现方式

  1. echart本身自带有toolbox的saveAsImage用以实现保存图片,具体的option设置如下:
var option = {
        title: {
            text: data.name,
            x: 'center',
            y: '83%',
            textStyle: {color: "#333"}
        },
        color:['#5f67ea', '#f97363', '#008aff','#ff9000', '#00d4c3', '#00b1bc'],
        legend: {
            type: 'scroll',
            icon: 'circle',
            orient: 'vertical',
            x: 'center',
            y:'top',
            itemWidth: 5,
            itemHeight: 5,
            align: 'left',
            itemGap: 5,
            textStyle: {
                fontSize:12,
                color: '#222'
            },
            data: data.legend
        },
        tooltip: {
            trigger: 'item',
            formatter: "{b}<br/>{c}({d}%)"
        },
        toolbox: {show: 1, x: 'center', y: 'bottom', feature: {saveAsImage: {show: false, title: '保存截图', type: 'png'}}},
        series: [{
            name: data.name,
            type: 'pie',
            radius: ['26%','50%'],
            center: ['50%', '57%'],
            data: data.data,
            itemStyle: {
                normal: {
                    label: {show: true, formatter: "{d}%"},
                    labelLine: {show: true}
                }
            },
            labelLine:{
                normal:{
                    length:5
                }
            },
        }]
    }
    var countPieDwlx = echarts.init(document.getElementById("countPieDwlx"));
    countPieDwlx.setOption(option,true);
  1. 但是由于echart自带的图片下载我没有找到如何将下载图标去掉,直接使用自定义图片用以实现图标保存,因此我放弃了echart本身自带的图片保存方法,自定义了一个span添加的点击事件,实现将echart进行图片保存。

html定义代码段

<div class="pie-body-div">
   <div id="countPieDwlx" class="pie-content"></div>
   <span class="pie-save-span" onclick="savePieImg('dwlx')">保存截图</span>
</div>

echart初始化部分同【1】方法相同,只是去掉echart自带的toolbox部分就可以了,下面为自定义的图片保存方法,使用起来比较灵活。

function savePieImg(type) {
  var picInfo= countPieDwlx.getDataURL({
       type: 'png',
       pixelRatio: 1.5,  //放大两倍下载,之后压缩到同等大小展示。解决生成图片在移动端模糊问题
       backgroundColor: '#fff'
   });//获取到的是一串base64信息
   const elink = document.createElement('a');
   elink.download = "统计分析";
   elink.style.display = 'none';
   elink.href = picInfo;
   document.body.appendChild(elink);
   elink.click();
   URL.revokeObjectURL(elink.href); // 释放URL 对象
   document.body.removeChild(elink)
}
  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值