Article directory
demand analysis
In the actual project development process, there is often a chart display function. At the same time, in order to meet the needs of users, a chart export function is attached, and the main form is to save it as a picture. This configuration item is provided in Echarts itself, which is simple and convenient to use. However, demand analysts require that there must be a chart export function button in order to match the overall style, for this requirement. . .
Development preparation
By checking the Echarts website, you can quickly find its own toolbox
configuration items. feature
There is one in it saveAsImage
, and the effect is roughly as follows:
Part of the code:
option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
toolbox: {
show: true,
feature: {
saveAsImage: {
}
}
},
series: [{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line'
}]
};
In addition, Echarts provides an interface getDataURL
through which chart images can be exported and return a base64 URL. Then there is an idea.
Implementation ideas
- By
getDataURL()
obtaining the base64 encoded characters of the image; - Characters are decoded and converted into Blob objects;
- Create
URL
an object through the Blob object; - Utilize
<a>
trigger download operations;
Specific code:
<!DOCTYPE html>
<html style="height: 100%">
<head>
<meta charset="utf-8">
<title>JS实现Echarts的图表保存为图片功能</title>
</head>
<body style="height: 100%; margin: 0">
<input type="button" value="导出" onclick="saveAsImage()"/>
<div id="container" style="height: 100%"></div>
<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
<script type="text/javascript">
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {
};
option = null;
option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
// toolbox: {
// show: true,
// feature: {
// saveAsImage: {}
// }
// },
series: [{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line'
}]
};
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
console.log(myChart.getDataURL());
//base64转blob
function base64ToBlob(code) {
let parts = code.split(';base64,');
let contentType = parts[0].split(':')[1];
let raw = window.atob(parts[1]);
let rawLength = raw.length;
let uInt8Array = new Uint8Array(rawLength);
for (let i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
return new Blob([uInt8Array], {
type: contentType });
}
function saveAsImage() {
let content = myChart.getDataURL();
let aLink = document.createElement('a');
let blob = this.base64ToBlob(content);
let evt = document.createEvent("HTMLEvents");
evt.initEvent("click", true, true);
aLink.download = "line.png";
aLink.href = URL.createObjectURL(blob);
aLink.dispatchEvent(new MouseEvent('click', {
bubbles: true, cancelable: true, view: window }));
}
</script>
</body>
</html>