-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplementation.js
38 lines (36 loc) · 1.12 KB
/
implementation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function render_chart(params) {
const { chartType, options, title, data } = params;
const dataString = JSON.stringify(data)
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
const optionsString = JSON.stringify(options)
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
const htmlString = `
<!DOCTYPE html>
<html>
<head>
<title>${title}</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="generated-chart" style="width: 100%; height: 90vh;"></canvas>
<script>
var ctx = document.getElementById('generated-chart').getContext('2d');
var chart = new Chart(ctx, {
type: '${chartType}',
data: JSON.parse("${dataString}"),
options: JSON.parse("${optionsString}")
});
</script>
</body>
</html>
`;
return htmlString;
}