Published
Edited
Sep 3, 2019
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
vl.markLine()
.data(data)
.encode(
vl.x().yearmonth("Flight_Date").axis({title: "Date"}),
vl.y().count()
.axis({title: "Count of Bird Strikes"}),
)
.height("300")
.width("800")
.title("Count of Bird Strikes at All Airports")
.render()
Insert cell
Insert cell
Insert cell
chart = {
// using example here as a guide: https://observablehq.com/@d3/line-chart
const margin = ({top: 40, right: 30, bottom: 30, left: 60})
const height = 400
const svg = d3.create("svg")
.attr("viewBox", [0,0,width,height]);
// why am I parsing, saving, and parsing again later?
const dateParse = d3.timeParse("%-m/%-d/%y 0:00")
const monthYearFormat = d3.timeFormat("%Y-%m")
const monthYearParse = d3.timeParse("%Y-%m")
const dayCounts = d3.nest()
.key(d => monthYearFormat(dateParse(d.Flight_Date)))
.rollup(d => d.length)
.entries(data)
const x = d3.scaleUtc()
.domain(d3.extent(dayCounts, d => monthYearParse(d.key)))
.range([margin.left, width - margin.right])
const y = d3.scaleLinear()
.domain([0, d3.max(dayCounts, d => d.value)])
.range([height - margin.bottom, margin.top])
.nice()
const xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
.call(g => g.selectAll(".tick line").clone() // gridlines
.attr("stroke-opacity", 0.1)
.attr("y1", margin.top + margin.bottom - height))
const yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.selectAll(".tick line").clone() // gridlines
.attr("stroke-opacity", 0.1)
.attr("x1", width - margin.left - margin.right + 12))
svg.append("g")
.call(xAxis)
svg.append("g")
.call(yAxis)
svg.append("path")
.datum(dayCounts)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 2)
.attr("d", d3.line()
.x(d => x(monthYearParse(d.key)))
.y(d => y(d.value)))
svg.append("text") // add a title
.attr("transform", `translate(${margin.left + width / 2.0},${margin.top / 2})`)
.attr("text-anchor", "middle")
.attr("alignment-baseline", "center")
.attr("font-weight", "bold")
.text("Count of Bird Strikes at All Airports")
svg.append("text") // add a y-axis label
.attr("transform", "rotate(-90)")
.attr("alignment-baseline", "center")
.attr("text-anchor", "middle")
.attr("x", -height/2.0) // mind-bendingly x/y swapped because of rotate(-90)
.attr("y", margin.left - 40)
.text("Count of Bird Strikes")
return svg.node();
// return dayCounts;
}
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