Published
Edited
Mar 30, 2020
Importers
Insert cell
Insert cell
chart = {
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("overflow", "visible");

svg
.append("g")
.call(xAxis)
.style("font-size", "18px");

svg
.append("g")
.call(yAxis)
.style("font-size", "18px");

const path = svg
.append("g")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 3)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.selectAll("path")
.data(data.series)
.join("path")
.style("mix-blend-mode", "multiply")
.attr("d", d => line(d.values))
.attr("stroke", d => (d.name === filter ? "red" : "#ddd"));

svg
.append("text")
.attr("y", 20) //magic number here
.attr("x", -height / 2)
.attr('text-anchor', 'middle')
.attr("class", "myLabel") //easy to style with CSS
.attr("transform", "rotate(-90)")
.text(data.y);

svg
.append("text")
.attr("y", height - 10) //magic number here
.attr("x", width / 2)
.attr('text-anchor', 'middle')
.attr("class", "myLabel") //easy to style with CSS
.text(data.x);

svg.call(hover, path);

return svg.node();
}
Insert cell
Annotations = [{ x: 500, y: 300, title: "Test" }]
Insert cell
{
const svg = d3.select(chart);
Annotations.forEach(function(d, i) {
svg
.selectAll("text.title")
.data([null])
.join("text")
.attr("class", "title")
.attr("x", d.x)
.attr("y", d.y)
.attr("font-size", "0.85em")
.attr("text-anchor", "middle")
.text(d.title);
});
}
Insert cell
lineSent = d3
.line()
.defined(value => value)
.x((d, i) => xAxis(data.dates[i]))
.y(d => yAxis(d))
Insert cell
filter = "Camden NJ"
Insert cell
testList = {
let s = Array(100).fill(15);
let t = new Array(66).fill(undefined);

return s.concat(t);
}
Insert cell
data = {
const data = d3.tsvParse(await FileAttachment("unemployment.tsv").text());
const columns = data.columns.slice(1);
let s = data.map(d => ({
name: d.name.replace(/, ([\w-]+).*/, " $1"),
values: columns.map(k => +d[k])
}));
s.push({
name: "test",
values: testList
});
return {
x: "Days",
y: "% Unemployment",
series: s,
dates: Array.from(Array(columns.length).keys())
};
}
Insert cell
function hover(svg, path) {
if ("ontouchstart" in document)
svg
.style("-webkit-tap-highlight-color", "transparent")
.on("touchmove", moved)
.on("touchstart", entered)
.on("touchend", left);
else
svg
.on("mousemove", moved)
.on("mouseenter", entered)
.on("mouseleave", left);

const dot = svg.append("g").attr("display", "none");

dot.append("circle").attr("r", 2.5);

dot
.append("text")
.style("font", "10px sans-serif")
.attr("text-anchor", "middle")
.attr("y", -8);

function moved() {
d3.event.preventDefault();
const ym = y.invert(d3.event.layerY + offsetY);
const xm = x.invert(d3.event.layerX + offsetX);
const i1 = d3.bisectLeft(data.dates, xm, 1);
const i0 = i1 - 1;
const i = xm - data.dates[i0] > data.dates[i1] - xm ? i1 : i0;
let filtData = data.series.filter(x => x.values[i] !== undefined);
const s = filtData.reduce((a, b) =>
Math.abs(a.values[i] - ym) < Math.abs(b.values[i] - ym) ? a : b
);
debugger;
path.attr("stroke", d =>
d.name === filter ? "red" : d === s ? "blue" : "#ddd"
);

dot.attr("transform", `translate(${x(data.dates[i])},${y(s.values[i])})`);
dot.select("text").text(s.name);
}

function entered() {
path.style("mix-blend-mode", null).attr("stroke", "#ddd");
dot.attr("display", null);
return "";
}

function left() {
let paths = path._groups[0];
path.attr("stroke", d => (d.name === filter ? "red" : "#ddd"));
dot.attr("display", "none");
}
}
Insert cell
data.series.filter(d => d.name === filter)
Insert cell
offsetX = 0
Insert cell
offsetY = 0
Insert cell
height = 600
Insert cell
margin = ({ top: 20, right: 20, bottom: 60, left: 90 })
Insert cell
x = d3
.scaleLinear()
.domain(d3.extent(data.dates))
.range([margin.left, width - margin.right])
Insert cell
y = logScale
? d3
.scaleLog()
.domain([yMin, d3.max(data.series, d => d3.max(d.values))])
.nice()
.range([height - margin.bottom, margin.top])
: d3
.scaleLinear()
.domain([yMin, d3.max(data.series, d => d3.max(d.values))])
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
yMin = 1
Insert cell
logScale = false
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.style("font", "18px times")
.call(
d3
.axisBottom(x)
.ticks(width / 80)
.tickSizeOuter(0)
)
Insert cell
yAxis = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.style("font", "18px times")
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
Insert cell
line = d3.line()
.defined(d => !isNaN(d))
.x((d, i) => x(data.dates[i]))
.y(d => y(d))
Insert cell
d3 = require("d3@5", "d3-array@2")
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