Published
Edited
Apr 9, 2019
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));

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

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

const path = svg.append("g")
.attr("fill", "none")
.attr("stroke", "#0077C0")
.attr("stroke-width", 1.5)
.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));

svg.call(hover, path);

return svg.node();
}
Insert cell
function hover(svg, path) {
svg
.style("position", "relative");
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);
const xm = x.invert(d3.event.layerX);
const i1 = d3.bisectLeft(data.game_num, xm, 1);
const i0 = i1 - 1;
const i = xm - data.game_num[i0] > data.game_num[i1] - xm ? i1 : i0;
const s = data.series.reduce((a, b) => Math.abs(a.values[i] - ym) < Math.abs(b.values[i] - ym) ? a : b);
path.attr("stroke", d => d === s ? null : "#ddd").filter(d => d === s).raise();
dot.attr("transform", `translate(${x(data.game_num[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);
}

function left() {
path.style("mix-blend-mode", "multiply").attr("stroke", null);
dot.attr("display", "none");
}
}
Insert cell
height = 500
Insert cell
margin = ({top: 20, right: 100, bottom: 30, left: 30})
Insert cell
x = d3.scaleLinear()
.domain([1, 82])
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([d3.max(data.series, d => d3.max(d.values)), d3.min(data.series, d => d3.min(d.values))]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 40).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y))
Insert cell
line = d3.line()
.defined(d => !isNaN(d))
.x((d, i) => x(data.game_num[i]))
.y(d => y(d))
Insert cell
data = {
const data = await d3.csv("https://gist.githubusercontent.com/bencf/836f44550ca1494a4d190cf9dd66678f/raw/a601bed931016afb6ffe76a6f584d29adc8c9da5/2018%2520drtg%2520rolling%2520averages%25202.csv");
var values = new Array();
var teams = new Array();
data.forEach(function(d) {
var name = d.abbr;
if (!teams.includes(name)) {
teams.push(name);
values[name] = new Array();
}
values[name].push(+d.pts_per_poss);
});
var newData = new Array();
teams.forEach(function(team) {
var obj = {name: team, values: values[team]};
newData.push(obj);
});
return {
y: "Points Allowed Per 100 Possessions",
series: newData,
game_num: d3.range(1, 83)
};
}
Insert cell
d3 = require("d3@5")
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