把echarts生成的图表转成图片保存
时间:2023-03-29 14:32:50
PHP
一:echarts官网:https://echarts.apache.org/zh...echarts.js地址:https://cdn.jsdelivr.net/npm/...二:将echarts生成的图表转成一个图片示例1:html
2:生成echarts图表(js),这里以直方图为例varchart=echarts.init(document.getElementById("chart"));varoption={animation:false,title:{text:'statistics',padding:[10,320,0,320]},xAxis:{type:'category',数据:['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],name:'Month',},tooltip:{trigger:'item',formatter:"{a}:{c}"},yAxis:{type:'value',name:'quantity',},series:[{name:'quantity',data:[120,200,150,80,70,110,130],类型:'bar',barWidth:50,//columnchartwidthlabel:{show:true,position:'top'},itemStyle:{color:newecharts.graphic.LinearGradient(0,0,0,1,[{offset:0,color:'#4976C5'},{offset:0.5,color:'#7496D3'},{offset:1,color:'#ECF0F9'},])},}]};图表。设置选项(选项);3:Getthebase64addressofthegeneratedhistogram//获取echarts图表的base64地址varbaseImage=chart.getDataURL("png");4:将生成的base64传递给后端并保存(一):ajax上传$.ajax({url:url,type:'post',data:{image:baseImage},success:function(json){}});(2):后端保存文件(以PHP为例)$image=$request->post('image');if(preg_match('/^(data:\s*image\/(\w+);base64,)/',$image,$result)){$type=$result[2];fullPath='图片保存地址';$fileName='echarts.png';//图片保存名if(file_put_contents($fullPath.$fileName,base64_decode(str_replace($result[1],'',$image)))){return'上传成功';}else{return'图片上传失败!';}}else{return'无效的图像文件!';}按照上面的步骤,echarts图表就可以变成图片了