Skip to content

Commit 3883186

Browse files
committed
add charting library basics
1 parent df85549 commit 3883186

File tree

5 files changed

+590
-3
lines changed

5 files changed

+590
-3
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@
6868
},
6969
"dependencies": {
7070
"bytes": "^3.0.0",
71+
"canvas": "^2.5.0",
7172
"get-folder-size": "^2.0.0",
7273
"glob": "^7.1.3",
7374
"gzip-size": "^5.0.0",
74-
"lodash": "^4.17.11"
75+
"lodash": "^4.17.11",
76+
"vega": "^5.4.0"
7577
},
7678
"publishConfig": {
7779
"access": "public"

src/charts/generateChart.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as vega from "vega";
2+
import * as fs from "fs";
3+
4+
const chartDefinition = require("./vega-spec.json");
5+
6+
interface DataPoint {
7+
x: string;
8+
y: number;
9+
}
10+
11+
export async function generateChart(path: string, dataPoints: DataPoint[]): Promise<void> {
12+
const wrappedDataPoints = [
13+
{
14+
name: "main",
15+
values: dataPoints.map(dp => ({ ...dp, c: 0 })),
16+
},
17+
];
18+
chartDefinition.data = wrappedDataPoints;
19+
20+
const view = new vega.View(vega.parse(chartDefinition), { renderer: "none" }).initialize();
21+
22+
const canvas = await view.toCanvas();
23+
24+
fs.writeFileSync(path, canvas.toBuffer());
25+
}

src/charts/vega-spec.json

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"$schema": "https://vega.github.io/schema/vega/v5.json",
3+
"width": 500,
4+
"height": 200,
5+
"padding": 5,
6+
7+
"signals": [
8+
{
9+
"name": "interpolate",
10+
"value": "linear",
11+
"bind": {
12+
"input": "select",
13+
"options": [
14+
"basis",
15+
"cardinal",
16+
"catmull-rom",
17+
"linear",
18+
"monotone",
19+
"natural",
20+
"step",
21+
"step-after",
22+
"step-before"
23+
]
24+
}
25+
}
26+
],
27+
28+
"data": [],
29+
30+
"scales": [
31+
{
32+
"name": "x",
33+
"type": "point",
34+
"range": "width",
35+
"domain": { "data": "main", "field": "x" }
36+
},
37+
{
38+
"name": "y",
39+
"type": "linear",
40+
"range": "height",
41+
"nice": true,
42+
"zero": true,
43+
"domain": { "data": "main", "field": "y" }
44+
},
45+
{
46+
"name": "color",
47+
"type": "ordinal",
48+
"range": "category",
49+
"domain": { "data": "main", "field": "c" }
50+
}
51+
],
52+
53+
"axes": [{ "orient": "bottom", "scale": "x" }, { "orient": "left", "scale": "y" }],
54+
55+
"marks": [
56+
{
57+
"type": "group",
58+
"from": {
59+
"facet": {
60+
"name": "series",
61+
"data": "main",
62+
"groupby": "c"
63+
}
64+
},
65+
"marks": [
66+
{
67+
"type": "line",
68+
"from": { "data": "series" },
69+
"encode": {
70+
"enter": {
71+
"x": { "scale": "x", "field": "x" },
72+
"y": { "scale": "y", "field": "y" },
73+
"stroke": { "scale": "color", "field": "c" },
74+
"strokeWidth": { "value": 2 }
75+
},
76+
"update": {
77+
"interpolate": { "signal": "interpolate" },
78+
"fillOpacity": { "value": 1 }
79+
},
80+
"hover": {
81+
"fillOpacity": { "value": 0.5 }
82+
}
83+
}
84+
}
85+
]
86+
}
87+
]
88+
}

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"lib": ["es2015", "esnext.asynciterable"],
1717
"sourceMap": true,
1818
"declaration": true,
19-
"outDir": "lib"
19+
"outDir": "lib",
20+
"skipLibCheck": true
2021
},
2122
"include": ["src/**/*.ts"],
2223
"exclude": ["node_modules"]

0 commit comments

Comments
 (0)