-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
37 lines (32 loc) · 1.07 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
const express = require('express');
const vg = require('vega');
vg.config.load.baseURL = 'https://vega.github.io/vega-editor/app/';
const TYPES = {
VegaLite: require('./charts/VegaLite'),
Bar: require('./charts/Bar'),
DistrictBar: require('./charts/DistrictBar'),
DemographicBar: require('./charts/DemographicBar'),
ZurichMap: require('./charts/ZurichMap')
};
const app = express();
app.get('/:type', (request, response) => {
if (!TYPES[request.params.type]) {
return response.status(404).send('Not Found');
}
const spec = TYPES[request.params.type]({
spec: JSON.parse(request.query.spec)
});
vg.parse.spec(spec, (error, chart) => {
if (error) {
return response.status(400).send(error.toString());
}
const view = chart({renderer: 'svg'});
const svg = view.update().svg();
response
.set('Cache-Control', `public, max-age=${60 * 60}`)
.type('svg').send(svg);
view.destroy();
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Listening on ${PORT}`)); // eslint-disable-line no-console