Public
Edited
Jul 31, 2024
1 fork
1 star
Insert cell
md`# SPIP Statistics`
Insert cell
height = 500
Insert cell
color = d3
.scaleOrdinal()
.domain(keys)
.range(spipColors)
Insert cell
spipColors = ["#101010", "#101010", "#303030", "#505050", "#707070", "#909090", "#900", "#FFFFFF", "#900", "#FFE599", "#FFE599", "#B6D7A8", "#D5A6BD", "#D5A6BD"]
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.selectAll("path")
.data(series)
.join("path")
.attr("fill", ({key}) => color(key))
.attr("d", area)
.append("title")
.text(({key}) => key);

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

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

return svg.node();
}
Insert cell
spip = Object.assign(
d3.csvParse(await fetch("https://jamesrezo.github.io/stats-backup/spip.since-2022-02-23.csv")
.then(response => response.text())).map(d => {
d.version = ["all", "older", "unknown"].includes(d.version)
? d.version
: "v" + d.version;
d.date = d3.utcParse("%Y-%m-%d")(d.date.slice(0, 10));
return d;
}),
{ y: "Sites" }
)
Insert cell
bydate = d3.rollup(
spip,
v => {
const r = {
date: v[0].date
};
v.forEach(d => (r[d.version] = d.sites));
if (r["v3.1"]) r["all"] = 2 * r["all"] - d3.sum(keys, k => r[k]);
return r;
},
d => +(24 * 3600 * 1000 * +Math.floor(d.date / (24 * 3600 * 1000)))
)
Insert cell
keys = new Set(spip.map(d => d.version).sort())
Insert cell
series = d3
.stack()
.keys(keys)
.order(d3.stackOrderReverse)
.value((d, key) => d[key] || 0)(bydate.values())
Insert cell
area = d3
.area()
.x(d => x(d.data.date))
.y0(d => y(d[0]))
.y1(d => y(d[1]))
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(spip, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = {
const max = d3.max(series, d => d3.max(d, d => d[1]));
return d3
.scaleLinear()
.domain([0, max])
.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 / 80).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(spip.y))
Insert cell
d3 = require("d3@6.0.0-rc.3")
Insert cell
import {swatches} from "@d3/color-legend"
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