Published
Edited
Nov 15, 2020
1 fork
1 star
Insert cell
md`# Arrival Delay vs Flight Distance`
Insert cell
FileAttachment("test.csv")
Insert cell
data = d3.csvParse(await FileAttachment("test.csv").text(), d3.autoType)
Insert cell
data_filt = data.filter(
d => d["Arrival Delay in Minutes"] < 300 && d["Flight Distance"] < 4000
)
Insert cell
render_data_table(data.slice(-11, -1))
Insert cell
chart_data = data_filt.map(d => ({
x: +d["Flight Distance"],
y: +d["Arrival Delay in Minutes"],
label: d.satisfaction,
id: d.id
}))
Insert cell
data.length
Insert cell
data.columns
Insert cell
satis = [...d3.group(chart_data, d => d.label).keys()]
Insert cell
md`#### Create a scatter plot`
Insert cell
viewof satisfact = autoSelect({
options: satis,
placeholder: "Search satisfied customers"
})
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

svg.append("g").call(xAxis);

svg.append("g").call(yAxis);

svg.node().update = currLabel => {
const labeled = chart_data.filter(d => d.label == currLabel);
const circles = svg
.selectAll("circle")
.data(chart_data, d => d.id)
.join(enter =>
enter
.append("circle")
.attr("cx", d => x(d.x))
.attr("cy", y.range()[0])
)
.transition()
.delay(d => x(d.x))
.attr("cx", d => x(d.x))
.attr("cy", d => y(d.y))
.attr("r", d => (d.label == currLabel || currLabel.length == 0 ? 2 : 0))
.style("fill", function(d) {
var content = d.label;
if (content === "neutral or dissatisfied") {
return "red";
} else {
return "green";
}
})
.style("opacity", 0.5);
};
// Add an x title
svg
.append("text")
.attr(
"transform",
`translate(${margin.left + (x.range()[1] - x.range()[0]) / 2},${height})`
)
.style("text-anchor", "middle")
.text("title");

return svg.node();
}
Insert cell
chart.update(satisfact)
Insert cell
y_domain = d3.extent(chart_data, d => d.y)
Insert cell
x_domain = d3.extent(chart_data, d => d.x)
Insert cell
x = d3
.scaleLinear()
.domain(x_domain)
.range([margin.left, width - margin.right])
Insert cell
y = d3
.scaleLinear()
.domain(y_domain)
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x))
Insert cell
yAxis = g =>
g.attr("transform", `translate(${margin.left},0)`).call(d3.axisLeft(y))
Insert cell
md`## Appendix`
Insert cell
d3 = require("d3")
Insert cell
import { aq, op } from "@uwdata/arquero"
Insert cell
import { legend } from "@d3/color-legend"
Insert cell
topojson = require("topojson-client@3")
Insert cell
import { ramp } from "@d3/sequential-scales"
Insert cell
import {
displayCaution,
render_data_table,
table_styles
} from "@uw-info474/utilities"
Insert cell
margin = ({ top: 20, right: 30, bottom: 40, left: 100 })
Insert cell
height = 500
Insert cell
import { autoSelect } from "@jashkenas/inputs"
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