Published
Edited
Feb 10, 2022
Fork of Figure 9 A
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.attr("fill", "#8FC0FA")
.attr("fill-opacity", 0.8)
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(d.bus))
.attr("width", x.bandwidth())
.attr("y", d => y(d.total))
.attr("height", d => y(0) - y(d.total));

svg.append("path")
.attr("fill", "none")
.attr("stroke", colors[0])
.attr("stroke-miterlimit", 1)
.attr("stroke-width", 3)
.attr("d", line1(data));

svg.append("path")
.attr("fill", "none")
.attr("stroke", colors[1])
.attr("stroke-miterlimit", 1)
.attr("stroke-width", 3)
.attr("d", line2(data));

svg.append("path")
.attr("fill", "none")
.attr("stroke", colors[2])
.attr("stroke-miterlimit", 1)
.attr("stroke-width", 3)
.attr("d", line3(data));
svg.append("text")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("text-anchor", "middle")
.style("font-size", "12px")
.style("text-decoration", "underline")
.attr("stroke","#ffffff");


svg.append("g")
.attr("fill", "none")
.attr("pointer-events", "all")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(d.bus))
.attr("width", x.bandwidth())
.attr("y", 0)
.attr("height", height)
.append("title")
.text(d => `Bus ${d.bus}
Saturday Total Range ${d.total.toLocaleString("en")} km
Saturday Light Duty ${d.light.toLocaleString("en")} km
Saturday Medium Duty ${d.medium.toLocaleString("en")} km
Saturday Heavy Duty ${d.heavy.toLocaleString("en")} km`);

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("y", 0)
.attr("x", 9)
.attr("dy", ".35em")
.attr("transform", "rotate(90)")
.style("text-anchor", "start")
.attr("stroke","#ffffff");


svg.append("g")
.call(yAxis)
.attr("stroke","#ffffff");

return svg.node();
}
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("Figure 9 B.csv").text(), d3.autoType), {y1: "Rang(km)"})
Insert cell
line1 = d3.line()
.x(d => x(d.bus) + x.bandwidth() / 2)
.y(d => y(d.light))
Insert cell
line2 = d3.line()
.x(d => x(d.bus) + x.bandwidth() / 2)
.y(d => y(d.medium))
Insert cell
line3 = d3.line()
.x(d => x(d.bus) + x.bandwidth() / 2)
.y(d => y(d.heavy))
Insert cell
x = d3.scaleBand()
.domain(data.map(d => d.bus))
.rangeRound([margin.left, width - margin.right])
.padding(0.1)
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.total)])
.rangeRound([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
.style("color","#ffffff")
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.style("color", "#ffffff")
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(data.y1))
Insert cell
colors = ["#ffc34a","#ff894a","#ff4a4a"]
Insert cell
legend = ["Light Duty", "Medium Duty", "Heavy Duty"]
Insert cell
width=0.5 * window.innerWidth
Insert cell
height=260
Insert cell
margin = ({top: 20, right: 30, bottom: 50, left: 40})
Insert cell
d3 = require("d3@6")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more