Public
Edited
Nov 6, 2023
5 forks
5 stars
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

var newline = d3
.line()
.defined((d) => !isNaN(d.value))
.x((d) => x(d.date))
.y((d) => y(d.value));

var newdata = data.map((p, index) =>
index === data.length - 1 ? [p] : [p, data[index + 1]]
);
var bounds = d3.extent(data, (d) => d.value);
var interval = bounds[1] - bounds[0];

var gradientColor = (p) => {
return d3.interpolateHslLong(
"#F2695C",
"#022859"
)((p[0].value - bounds[0]) / interval);
};

svg
.selectAll("path")
.data(newdata)
.enter()
.append("path")
.attr("d", (p) => newline(p))
.attr("class", "line")
.attr("stroke", (p) => gradientColor(p));

svg.append("g").call(yAxis);

svg.append("g").call(xAxis);

return svg.node();
}
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
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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