forked from StataBS/indikatoren
-
Notifications
You must be signed in to change notification settings - Fork 3
/
exportChartImages.js
118 lines (100 loc) · 3.95 KB
/
exportChartImages.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const exporter = require('highcharts-export-server');
//var execfile = require("execfile");
var fs = require('fs');
var path = require('path');
/*
//Hack to re-use existing web js code from within node.js, see http://stackoverflow.com/a/8808162
var vm = require("vm");
var execute = function(path, context) {
context = context || {};
var data = fs.readFileSync(path);
var result = vm.runInNewContext(data, context, path);
return {context: context, result: result};
};
*/
/*
var keysFilePath = path.join(__dirname, '../metadata/all/kuerzelById.json');
var ctx = execute(keysFilePath);
var kuerzelById = ctx.kuerzelById;
*/
var kuerzelById = JSON.parse(fs.readFileSync(path.join(__dirname, '../metadata/all/kuerzelById.json'), 'utf8'));
var chartDetails = [];
console.log('Deleting previous svg files...');
var rimraf = require("rimraf");
rimraf('images/indikatorenset/*', function(error) {
if (error) { throw error; }
rimraf('images/portal/*', function(error) {
if (error) { throw error; }
go();
});
});
function go(){
var views = [true, false];
views.forEach(function(view){
console.log('Creating array entries for indikatorensetView=' + view);
Object.keys(kuerzelById).forEach(function(chartId) {
chartDetails.push(createPathArray(chartId, view));
});
});
exporter.initPool(
{
maxWorkers: 1,
initialWorkers: 1,
workLimit: 10
});
createSvgImages(chartDetails);
}
function createPathArray(chartId, view){
var imagePath = (view) ? 'images/indikatorenset/' : 'images/portal/';
var configPath = (view) ? 'charts/configs/indikatorenset/' : 'charts/configs/portal/';
var infilePath = path.join(__dirname, '../' + configPath + chartId + '.json');
var outfilePath = path.join(__dirname, '../' + imagePath + chartId + '.svg');
var configFile = fs.readFileSync(infilePath, 'utf8');
var config = deserialize(configFile);
//decide if stockchart, map, or chart
var constr = config.isStock ? 'StockChart': (config.chart.type === 'map' ? 'Map' : 'Chart');
return {
config: config,
infilePath: infilePath,
outfilePath: outfilePath,
constr: constr
};
}
//alternatively use looping style used here: https://github.com/highcharts/node-export-server/issues/41
function createSvgImages(chartDetails){
if (chartDetails.length > 0){
var chartEntry = chartDetails.pop();
//console.log('Current infile: ' + chart.infile);
//if (chartEntry.infilePath.indexOf('6009') > 0 ){
var exportSettings = {
type: 'svg',
infile: chartEntry.infilePath,
constr: chartEntry.constr,
outfile: chartEntry.outfilePath,
//define empty mappie here to satisfy export server, same as in options001.js
customCode: "function(){Highcharts.seriesType('mappie', 'pie', {}, {});}()",
//add proj4 and jQuery to export server's dependencies
resources: {
files: "node_modules/proj4/dist/proj4.js,node_modules/jquery/dist/jquery.min.js"
}
};
exporter.export(exportSettings, function (err, res) {
if (err) {throw err}
//The export result is now in res.
//If the output is not PDF or SVG, it will be base64 encoded (res.data).
//If the output is a PDF or SVG, it will contain a filename (res.filename).
console.log('File created: ' + res.filename + ', ' + chartDetails.length + ' to go...');
createSvgImages(chartDetails);
});
}
else {
console.log('...done!');
exporter.killPool();
process.exit();
}
//}
}
//from https://github.com/yahoo/serialize-javascript
function deserialize(serializedJavascript){
return eval('(' + serializedJavascript + ')');
}