-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
107 lines (92 loc) · 3.27 KB
/
index.ts
File metadata and controls
107 lines (92 loc) · 3.27 KB
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
import "@mapd/connector/dist/browser-connector";
import { Transforms2SQL } from "./transforms2sql";
import {
NormalizedUnitSpec,
extractTransforms,
selectedFields
} from "vega-lite/build/src/spec";
import { defaultConfig } from "vega-lite/build/src/config";
import embed from "vega-embed";
// connect to MapD server; start session
const connection = new (window as any).MapdCon()
.protocol("https")
.host("metis.mapd.com")
.port("443")
.dbName("mapd")
.user("mapd")
.password("HyperInteractive");
const session = connection.connectAsync();
const table = "flights_donotmodify";
const vlHeatmap: NormalizedUnitSpec = {
title: "Average Daily Flight Departure Delay",
mark: "rect",
encoding: {
x: { field: "flight_dayofmonth", type: "ordinal", title: "day" },
y: { field: "flight_month", type: "ordinal", title: "month" },
color: {
aggregate: "average",
field: "depdelay",
type: "quantitative",
title: "average delay"
}
}
};
const vlBarchart: NormalizedUnitSpec = {
title: "Number of Flights by Airline",
mark: "bar",
encoding: {
x: {
field: "carrier_name",
type: "ordinal",
title: "Airline"
},
y: { aggregate: "count", type: "quantitative", title: "Number of Flights" }
}
};
function loadDemo(containerName: string, spec: NormalizedUnitSpec): void {
const container = document.getElementById(containerName + "-container");
// Insert original vega spec
const ogSpecContainer = <HTMLDivElement>document.createElement("div");
const ogSpecCode = <HTMLElement>document.createElement("pre");
ogSpecCode.classList.add("prettyprint");
ogSpecCode.innerHTML = JSON.stringify(spec, null, 4);
ogSpecContainer.innerHTML = "<h3>Original Specification</h3>";
ogSpecContainer.appendChild(ogSpecCode);
container.appendChild(ogSpecContainer);
// Insert modified vega spec
const modifiedSpec = extractTransforms(spec, defaultConfig);
const modifiedSpecContainer = <HTMLDivElement>document.createElement("div");
const modifiedSpecCode = <HTMLElement>document.createElement("pre");
modifiedSpecCode.classList.add("prettyprint");
modifiedSpecCode.innerHTML = JSON.stringify(modifiedSpec, null, 4);
modifiedSpecContainer.innerHTML =
"<h3>Specification w/ Extracted Transforms</h3>";
modifiedSpecContainer.appendChild(modifiedSpecCode);
container.appendChild(modifiedSpecContainer);
// Insert transformation SQL
const transforms = modifiedSpec.transform;
const selects = selectedFields(spec);
selects.splice(selects.length - 1, 1);
delete modifiedSpec.transform;
const sql = Transforms2SQL.convert(table, selects, transforms);
const sqlContainer = <HTMLDivElement>document.createElement("div");
const sqlCode = <HTMLElement>document.createElement("pre");
sqlCode.classList.add("prettyprint");
sqlCode.innerHTML = sql;
sqlContainer.innerHTML = "<h3>Transforms as SQL</h3>";
sqlContainer.appendChild(sqlCode);
container.appendChild(sqlContainer);
// Insert visualization
session.then(s => {
s.queryAsync(sql).then(values => {
// load visualization
embed(
"#" + containerName + "-viz",
Object.assign({ data: { values } }, modifiedSpec),
{ defaultStyle: true }
);
});
});
}
loadDemo("barchart", vlBarchart);
loadDemo("heatmap", vlHeatmap);