-
Notifications
You must be signed in to change notification settings - Fork 6
/
vl2vg4gemini.js
66 lines (56 loc) · 1.68 KB
/
vl2vg4gemini.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
import * as d3 from "d3";
import * as vegaLite from "vega-lite";
export default function vl2vg4gemini(vlSpec) {
let vgSpec = vegaLite.compile(vlSpec).spec;
vgSpec.axes = mergeDuplicatedAxes(vgSpec.axes);
appendNamesOnGuides(vgSpec);
return vgSpec;
}
export function castVL2VG(vlSpec) {
if (vlSpec && vlSpec.$schema && vlSpec.$schema.indexOf("https://vega.github.io/schema/vega-lite") >= 0){
return vl2vg4gemini(vlSpec)
}
return vlSpec
}
function appendNamesOnGuides(vgSpec){
if (vgSpec.axes) {
vgSpec.axes.forEach(axis => {
if (!axis.encode) {
axis.encode = {axis: {name: axis.scale}};
} else {
axis.encode.axis = { ...axis.encode.axis, name: axis.scale };
}
});
}
if (vgSpec.legends) {
vgSpec.legends.forEach((legend, i) => {
if (!legend.encode) {
legend.encode = {legend: {name: `legend${i}`}};
} else {
legend.encode.legend = Object.assign({}, legend.encode.legend, {name: `legend${i}`});
}
});
}
}
function mergeDuplicatedAxes(vegaAxes) {
if (!vegaAxes || vegaAxes.length <= 0) {
return [];
}
let axesScales = vegaAxes.filter(a => a.grid).map(a => a.scale);
return d3.rollups(vegaAxes,
axes => {
let axisWithGrid = axes.find(a => a.grid);
let axisWithoutGrid = { ...axes.find(a => !a.grid) };
if (axisWithGrid) {
axisWithoutGrid.grid = true;
if (axisWithGrid.gridScale) {
axisWithoutGrid.gridScale = axisWithGrid.gridScale;
}
axisWithoutGrid.zindex = 0;
}
return axisWithoutGrid;
},
axis => axis.scale
).map(d => d[1])
.sort((a,b) => (axesScales.indexOf(a.scale) - axesScales.indexOf(b.scale)));
}