Published
Edited
May 24, 2019
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
MapdCon = require("https://rawgit.com/mapd/mapd-connector/master/dist/browser-connector.js")
.catch(() => new window.MapdCon())
Insert cell
Insert cell
connector = MapdCon
.protocol("https")
.host("metis.mapd.com")
.port("443")
.dbName("mapd")
.user("mapd")
.password("HyperInteractive")
.connectAsync()
Insert cell
Insert cell
tables = {
const result = await connector.getTablesAsync()
return result.map(d => d.name)
}
Insert cell
md`Count the number of rows from the flights table`
Insert cell
{
const n = await connector.queryAsync(
`SELECT
COUNT(*) AS n
FROM flights_donotmodify`
)
return n[0].n
}
Insert cell
md`What are the fields and their data types in the flights table?`
Insert cell
connector.getFieldsAsync("flights_donotmodify")
Insert cell
Insert cell
airlines = {
const airlines = await connector.queryAsync(`
SELECT DISTINCT carrier_name AS carrier
FROM flights_donotmodify
ORDER BY carrier_name`
)
return airlines.map(d => d.carrier)
}
Insert cell
Insert cell
createSQL = airlineName => `
SELECT
origin,
dest,
avg(origin_lon) as ox,
avg(origin_lat) as oy,
avg(dest_lon) as dx,
avg(dest_lat) as dy,
count(*) as n,
avg(arrdelay) as avg_arrdelay
FROM flights_donotmodify
WHERE carrier_name ILIKE '%${airlineName}%'
AND arrdelay IS NOT null
GROUP BY origin, dest
ORDER BY CASE
WHEN avg_arrdelay = 0 THEN 3
WHEN avg_arrdelay > 0 THEN 2
WHEN avg_arrdelay < 0 THEN 1
END DESC`
Insert cell
Insert cell
data = await connector.queryAsync(createSQL(airline))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
xExtent = d3.extent(data.reduce((acc, cur) => [...acc, cur.dx, cur.ox], []))
Insert cell
yExtent = d3.extent(data.reduce((acc, cur) => [...acc, cur.dy, cur.oy], []))
Insert cell
countExtent = d3.extent(data, d => d.n)
Insert cell
Insert cell
Insert cell
bbox = {
return {
type: "LineString",
"coordinates": [[xExtent[0], yExtent[0]], [xExtent[1], yExtent[1]]]
}
}
Insert cell
Insert cell
Insert cell
Insert cell
colorScale = d3.scaleLinear()
.range(colors)
.domain(delayExtent)
.interpolate(d3.interpolateHcl)
Insert cell
Insert cell
lineWidth = d3.scaleLinear().domain(d3.extent(data, d => d.n)).range([0.01, 15])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more