save PNG chart preview

This commit is contained in:
Jan Prochazka
2021-11-11 13:39:53 +01:00
parent 0fd4084fac
commit 4faaaa1eb7
3 changed files with 14 additions and 4 deletions

View File

@@ -118,10 +118,19 @@ module.exports = {
},
exportChart_meta: 'post',
async exportChart({ title, config }) {
const html = getChartExport(title, config);
async exportChart({ title, config, image }) {
const fileName = `${uuidv1()}.html`;
const imageFile = `${fileName}.png`;
const html = getChartExport(title, config, imageFile);
await fs.writeFile(path.join(uploadsdir(), fileName), html);
if (image) {
const index = image.indexOf('base64,');
if (index > 0) {
const data = image.substr(index + 'base64,'.length);
const buf = Buffer.from(data, 'base64');
await fs.writeFile(path.join(uploadsdir(), imageFile), buf);
}
}
return fileName;
},
};

View File

@@ -1,4 +1,4 @@
const getChartExport = (title, config) => {
const getChartExport = (title, config, imageFile) => {
return `<html>
<meta charset='utf-8'>
@@ -25,7 +25,7 @@ const getChartExport = (title, config) => {
</html>
<body onload="showChart()">
<img src="img1.png" id="myImage" />
<img src="${imageFile}" id="myImage" />
<div>
<canvas id="myChart"></canvas>