Published
Edited
Jul 29, 2020
Importers
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
slope_chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height + graph.margin.bottom])
.attr("font-family", graph.font)
.attr("font-size", 10);
let n = slope_data.dates.length
let formatNumber = slope_y.tickFormat(100)

// Dates at top
svg.append("g")
.attr("text-anchor", "middle")
.selectAll("g")
//.data([slope_data.dates])
.data(["2017-18","2018-19"]) //we hardcode the dates here to make things look nicer
.join("g")
.attr("transform", (d, i) => `translate(${slope_x(i)},20)`)
//.call(g => g.append("text").text(d => d.getFullYear()))
.call(g => g.append("text").text(d => d)) //just use whats in the data struct that we hardcoded
.call(g => g.append("line").attr("y1", 3).attr("y2", 9).attr("stroke", "currentColor"));

// Connecting lines
svg.append("g")
.attr("fill", "none")
.attr("stroke", colours.primary)
.selectAll("path")
.data(slope_data)
.join("path")
.attr("d", d => slope_line(d.values));

// Force names
svg.append("g")
.selectAll("g")
.data(slope_data.dates)
.join("g")
.attr("transform", (d, i) => `translate(${slope_x(i) + (i === 0 ? -padding : i === n - 1 ? padding : 0)},0)`)
.attr("text-anchor", (d, i) => i === 0 ? "end" : i === n - 1 ? "start" : "middle")
.selectAll("text")
.data((d, i) => d3.zip(
slope_data.map(i === 0 ? d => `${d.name} ${formatNumber(d.values[i])}`
: i === n - 1 ? d => `${formatNumber(d.values[i])} ${d.name}`
: d => `${formatNumber(d.values[i])}`),
slope_dodge(slope_data.map(d => slope_y(d.values[i])))))
.join("text")
.attr("y", ([, y]) => y)
.attr("dy", "0.35em")
.text(([text]) => text)
.call(slope_halo);

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
slope_line = d3.line()
.x((d, i) => slope_x(i))
.y(slope_y)
Insert cell
slope_x = d3.scalePoint()
.domain(d3.range(slope_data.dates.length))
.range([0, width]) //hardcoded margin adjustment to -205 to align graph _on the main report page_
.padding(0.5)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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