-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.js
118 lines (111 loc) · 2.49 KB
/
visualize.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
const vega = require('vega');
const { createCanvas } = require('canvas');
const fs = require('fs');
const canvas = createCanvas(500, 500);
const config = data => ({
background: 'white',
$schema: 'https://vega.github.io/schema/vega/v5.json',
width: 500,
height: 200,
padding: 5,
signals: [
{
name: 'interpolate',
value: 'linear',
bind: {
input: 'select',
options: [
'basis',
'cardinal',
'catmull-rom',
'linear',
'monotone',
'natural',
'step',
'step-after',
'step-before',
],
},
},
],
data: [
{
name: 'table',
values: data,
},
],
scales: [
{
name: 'x',
type: 'point',
range: 'width',
domain: { data: 'table', field: 'x' },
},
{
name: 'y',
type: 'linear',
range: 'height',
nice: true,
zero: true,
domain: { data: 'table', field: 'y' },
},
{
name: 'color',
type: 'ordinal',
range: 'category',
domain: { data: 'table', field: 'c' },
},
],
axes: [{ orient: 'bottom', scale: 'x' }, { orient: 'left', scale: 'y' }],
marks: [
{
type: 'group',
from: {
facet: {
name: 'series',
data: 'table',
groupby: 'c',
},
},
marks: [
{
type: 'line',
from: { data: 'series' },
encode: {
enter: {
x: { scale: 'x', field: 'x' },
y: { scale: 'y', field: 'y' },
stroke: { scale: 'color', field: 'c' },
strokeWidth: { value: 2 },
},
update: {
interpolate: { signal: 'interpolate' },
fillOpacity: { value: 1 },
},
hover: {
fillOpacity: { value: 0.5 },
},
},
},
],
},
],
});
module.exports.plot = (data, title) => {
var view = new vega.View(vega.parse(config(data)))
.renderer('none')
.initialize();
// generate static PNG file from chart
view
.toCanvas()
.then(function(canvas) {
// process node-canvas instance for example, generate a PNG stream to write var
// stream = canvas.createPNGStream();
console.log('Writing PNG to file...');
fs.writeFile(`${title}.png`, canvas.toBuffer(), () => {});
})
.catch(function(err) {
console.log('Error writing PNG to file:');
console.error(err);
});
};