This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
224 lines (186 loc) · 6.87 KB
/
index.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'use strict';
//
// This is a function to generate a chart using Nightmare, Data-Forge and C3.
// Please see README.md for examples.
//
// Parameters:
//
// inputFilePath - The input CSV file to load.
// chartTemplateFile - Path to a C3 spec file (either json or js).
// outputFilePath - Path that specifies where to save the chart's output image.
// options - Options to control rendering.
// show Set to true to show the browser window that renderers the chart.
// export Path to export the chart to (can be used to inspect interactive chart later).
// template Optionaly wet the template used to render the chart.
// verbose Set to true for debug output.
//
const Nightmare = require('nightmare');
const dataForge = require('data-forge');
const path = require('path');
const assert = require('chai').assert;
const fs = require('fs-extra');
const Sugar = require('sugar');
function composeChartTemplate (chartTemplateInputPath, chartTemplateOutputPath, chartDefinition) {
fs.copySync(chartTemplateInputPath, chartTemplateOutputPath);
let chartJson = JSON.stringify(chartDefinition, null, 4);
chartJson = chartJson.split("\n").map(line => " " + line).join("\n");
fs.writeFileSync(
path.join(chartTemplateOutputPath, "index.js"),
"$(function () {\n" +
" c3.generate(\n" +
chartJson + "\n" +
" );\n" +
"});"
);
};
module.exports = function (inputData, chartDefinition, outputFilePath, options, nightmare) {
assert.isString(outputFilePath, "c3-chart-maker: Expected parameter outputFilePath to be a string.");
options = options || {};
function verbose (msg) {
if (options.verbose) {
console.log(msg);
}
};
verbose("Rendering chart to " + outputFilePath);
var data;
if (Sugar.Object.isString(inputData)) {
if (inputData.endsWith(".csv")) {
verbose("<< Input from CSV file " + inputData);
data = dataForge.readFileSync(inputData)
.parseCSV()
.toArray();
}
else { // Assume JSON file.
verbose("<< Input from JSON file " + inputData);
data = JSON.parse(fs.readFileSync(inputData));
}
}
else if (Sugar.Object.isArray(inputData)) {
verbose("<< Input from JavaScript array.");
data = inputData;
}
else {
verbose("<< Input from DataForge DataFrame.");
data = inputData.toArray(); // Assume DataFrame.
}
var chart = null;
if (Sugar.Object.isString(chartDefinition)) {
if (chartDefinition.endsWith(".json")) {
verbose("<< Chart definition from JSON file " + chartDefinition);
// Load json file.
chart = JSON.parse(fs.readFileSync(chartDefinition, 'utf-8'));
}
else if (chartDefinition.endsWith(".js")) {
verbose("<< Chart definition from JavaScript file " + chartDefinition);
// Load Node.js module.
var fullPath = path.resolve(chartDefinition);
chart = require(fullPath)(data, options);
}
else {
throw new Error("Unable to determine type of input file " + chartDefinition + ", expected a .json or .js file." );
}
}
else {
chart = chartDefinition;
}
if (!chart.data) {
chart.data = {};
}
if (chart.series) { // THIS SECTION IS DEPRECATED.
console.error("Usage of deprecated field: 'series'.");
if (!chart.data.columns) {
chart.data.columns = [];
}
var series = Object.keys(chart.series);
var dataFrame = new dataForge.DataFrame(data);
series.forEach(seriesName => {
var dataSeries = chart.series[seriesName];
if (Sugar.Object.isString(inputData) && seriesName !== "x") {
dataFrame = dataFrame.parseFloats(dataSeries).bake();
}
chart.data.columns.push(
[seriesName].concat(
dataFrame.getSeries(dataSeries)
.select(v => v === undefined ? null : v)
.toArray()
)
)
});
}
else if (!chart.data.columns && !chart.data.json) {
chart.data.json = data;
}
if (options.dumpChart) {
console.log(JSON.stringify(chart, null, 4));
}
const chartTemplateInputPath = options.template && path.resolve(options.template) || path.join(__dirname, "template");
verbose("<< Using chart template " + chartTemplateInputPath);
const chartTemplateOutputPath = options.export && path.resolve(options.export) || path.join(__dirname, "chart-tmp");
verbose(">> Outputing interactive chart to " + chartTemplateOutputPath);
verbose(">> Outputing rendered chart to " + outputFilePath);
composeChartTemplate(
chartTemplateInputPath,
chartTemplateOutputPath,
chart
);
var filePath = path.join(chartTemplateOutputPath, 'index.html');
var url = 'file://' + filePath;
var selector = 'svg';
var ownNightmare = false;
if (!nightmare) {
ownNightmare = true;
nightmare = new Nightmare({
frame: false,
show: options.show,
});
}
nightmare.on('console', function (type, message) {
if (type === 'log') {
console.log('LOG: ' + message);
return; // Don't bother with logs.
}
if (type === 'warn') {
console.warn('LOG: ' + message);
return;
}
if (type === 'error') {
throw new Error("Browser JavaScript error: " + message);
}
});
nightmare.goto(url);
return nightmare.wait(selector)
.evaluate(selector => {
const body = document.querySelector('body');
const element = document.querySelector(selector);
const rect = element.getBoundingClientRect();
return {
bodyWidth: body.scrollWidth,
bodyHeight: body.scrollHeight,
x: rect.left,
y: rect.top,
height: rect.bottom - rect.top,
width: rect.right - rect.left,
};
}, selector)
.then(rect => {
return nightmare.viewport(rect.bodyWidth, rect.bodyHeight)
.screenshot(outputFilePath, rect);
})
//.then(() => nightmare.screenshot("whole-page.png"))
.then(() => {
if (ownNightmare) {
return nightmare.end();
}
})
.catch(err => {
if (ownNightmare) {
return nightmare.end()
.then(() => {
throw err
});
}
else {
throw err
}
});
};