Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
ss = require("simple-statistics@7")
Insert cell
Insert cell
Insert cell
Insert cell
linearRegression = ss.linearRegression(data.map(d => ([+d.x, d.y]))); // coerce d.x date to a number
Insert cell
Insert cell
linearRegressionLine = ss.linearRegressionLine(linearRegression);
Insert cell
Insert cell
regressionEndpoints = [
{ x: data[0].x, y: linearRegressionLine(+data[0].x) },
{ x: data[data.length - 1].x, y: linearRegressionLine(+data[data.length - 1].x) }
]
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("title", data.barChartSVGTitle)
.attr("class", "chart")
.attr("viewBox", [0, 0, width, height]);
const defs = svg
.append("defs");

const pencil = DOM.uid("pencil");
defs.append("filter").attr("id", pencil.id)
.html(`<feTurbulence baseFrequency="0.01" numOctaves="6" type="fractalNoise" />
<feDisplacementMap scale="4" in="SourceGraphic" xChannelSelector="R" yChannelSelector="G" />
<feGaussianBlur stdDeviation="0.5" />`)
svg.append("g")
.attr("class", "chart-title")
.call(chartTitle);
svg.append("g")
.attr("class", "bars")
.selectAll("rect")
.data(data)
.join("rect")
.attr("role", "presentation")
.attr("class", "bar")
.attr("x", d => x(d.x))
.attr("y", d => y(d.y))
.attr("width", x.bandwidth())
.attr("height", d => y(0) - y(d.y))
.style("fill", barColor);
svg.append("path")
.attr("id", "linear-regression-fitted-line")
.datum(regressionEndpoints)
.attr("fill", "none")
.attr("filter", pencil) // optional pencil filter styling
.attr("stroke", "#534e7a")
.attr("stroke-width", 3)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", d3.line()
.x(d => x(d.x))
.y(d => y(d.y)));

svg.append("g")
.attr("class", "chart-by")
.call(chartBy)
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
x = d3.scaleBand()
.domain(data.map(d => d.x))
.range([margin.left, width - margin.right])
.padding(0.1);
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.y)]).nice()
.range([height - margin.bottom, margin.top]);
Insert cell
chartTitle = g => g
.append("text")
.attr("id", "chart-title")
.attr("x", 0)
.attr("y", 0)
.attr("dy", "1em")
.style("font-weight", "800")
.style("font-size", "clamp(.7rem, 2.5vw, 1.2rem)") // minimum, preferred, maximum
.text(data.barChartTitle);
Insert cell
chartBy = g => g
.attr("transform", `translate(${width}, ${height})`)
.append("text")
.attr("id", "source")
.attr("x", 0)
.attr("y", 0)
.attr("dy", "-0.5em")
.attr("text-anchor", "end")
.text('@DiDoesDigital');
Insert cell
Insert cell
margin = ({top: 24, right: 0, bottom: 48, left: 0})
Insert cell
Insert cell
barColor = "#766FAE"
Insert cell
inactiveColor = "#E6E5F0"
Insert cell
activeColor = "#534e7a"
Insert cell
Insert cell
Insert cell
accessibleDataTable = render_data_table(data, {caption: data.tableCaption, columns: data.slice[0], focusable: false})
Insert cell
Insert cell
data[0]
Insert cell
Insert cell
d3 = require("d3@5", "d3-array@2.4")
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