Public
Edited
Jan 1, 2023
Importers
Insert cell
Insert cell
Insert cell
SpaghettiChart(data, {
x: (d) => d.distance,
label: (d) => d.distance,
width: 600,
height: 250,
turns: 6
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
SegmentChart([6, 3, 12, 3, 9], { width, height: 20, space: 0.25 })
Insert cell
Insert cell
Insert cell
function SpaghettiChart(
data,
{
x = (d) => d,
label = (d) => d,
r = 20, // radius of the turns
space = 6, // space between line segments (px)
turns = 4, // number of turns
width = 600,
height = 400,
marginTop = 10,
marginBottom = 10,
marginLeft = 10,
marginRight = 10
}
) {
const chartWidth = width - marginLeft - marginRight;
const chartHeight = height - marginTop - marginBottom;

const total = d3.sum(data, x);
const lineLength = turns * chartWidth;

const xScale = d3.scaleLinear().domain([0, total]).range([0, lineLength]);

const { path: pathString } = curvedLine(chartWidth, r, lineLength);
const dasharray = data.map((d) => xScale(x(d))).join(` ${space} `);

console.log(dasharray);

const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height]);

svg
.append("rect")
.attr("width", width)
.attr("height", height)
.attr("fill", "#f8bbd0");

const group = svg
.append("g")
.attr("transform", `translate(${marginLeft}, ${marginTop})`);

const path = group
.append("path")
.attr("d", pathString)
.attr("stroke", "#d81b60")
.attr("stroke-dasharray", dasharray)
.attr("stroke-width", 6)
.attr("fill", "none");

return svg.node();
}
Insert cell
// join([2, 3, 4], 1) // [2, 1, 3, 1, 4]
function join(list, separator) {
return list.slice(1).reduce((acc, d) => [...acc, separator, d], [list[0]]);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = rawData
.map((row) => ({
date: new Date(row["Activity Date"]),
distance: row["Distance"], // [m]
time: row["Elapsed Time"], // [sec]
activityType: row["Activity Type"]
}))
.filter((d) => d.date > new Date("2022-09-01"))
Insert cell
activities = new Set(data.map(d => d.activityType))
Insert cell
space = ({
distance: 2000,
type: "marker"
})
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