Published
Edited
Mar 1, 2022
Importers
8 stars
Also listed in…
Hello
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

const line = d3
.line()
.x(d => xScale(d.year))
.y(d => yScale(d.value));

const area = d3
.area()
.x(d => xScale(d.year))
.y0(d => yScale(d.value - d.halfwidth))
.y1(d => yScale(d.value + d.halfwidth));

svg
.append("path")
.attr("fill", "pink")
.attr("d", area(fitted));

svg
.append("path")
.attr("fill", "none")
.attr("stroke", "black")
.attr("d", line(data));

svg
.append("path")
.attr("fill", "black")
.attr("d", line.curve(curvePoints(2))(data));

svg
.append("path")
.attr("fill", "none")
.attr("stroke", "red")
.attr("d", line.curve(d3.curveLinear)(fitted));

return svg.node();
}
Insert cell
Loess = import("https://cdn.skypack.dev/loess").then((d) => d.default)
Insert cell
options = ({ span, band, degree: +degree })
Insert cell
Insert cell
model = new Loess(
{
x: data.map(d => d.year), // x
y: data.map(d => d.value), // value
w: data.map(Math.random) // weights
},
options
)
Insert cell
fit = model.predict()
Insert cell
fitted = data.map((d, i) => ({
year: d.year,
value: fit.fitted[i],
halfwidth: fit.halfwidth[i]
}))
Insert cell
xScale = d3
.scaleLinear()
.domain(d3.extent(data, d => d.year))
.nice()
.range([10, width - 10])
Insert cell
yScale = d3
.scaleLinear()
.domain(d3.extent(data, d => d.value))
.nice()
.range([height, 0])
Insert cell
height = 500
Insert cell
d3 = require("d3@5")
Insert cell
// https://github.com/d3/d3-shape/issues/154#issuecomment-586640918
function curvePoints(radius = 0.5) {
const tau = 2 * Math.PI;
return function(context) {
return {
lineStart: () => {},
lineEnd: () => {},
point: (x, y) => {
context.moveTo(x + radius, y);
context.arc(x, y, radius, 0, tau);
}
};
};
}
Insert cell
import { select, slider } from "@jashkenas/inputs"
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