Published
Edited
Jul 14, 2020
Fork of Calendar
Insert cell
Insert cell
Insert cell
data = {
const data = d3.csvParse(await FileAttachment("philippines_2020_07_07.csv").text(), d3.autoType);
return d3.pairs(data, ({retail_and_recreation_percent_change_from_baseline: previous},
{date, retail_and_recreation_percent_change_from_baseline, grocery_and_pharmacy_percent_change_from_baseline, residential_percent_change_from_baseline
}) => {
return {date: date, value: (retail_and_recreation_percent_change_from_baseline
- previous) / previous, retail_and_recreation: retail_and_recreation_percent_change_from_baseline, grocery_and_pharmacy: grocery_and_pharmacy_percent_change_from_baseline,
residential: residential_percent_change_from_baseline
};
});
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart2 = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("overflow", "visible");
const x = d3.scaleUtc()
.domain(d3.extent(line_chart_data.dates))
.range([margin.left, width - margin.right])
const y = d3.scaleLinear()
.domain([-100, d3.max(line_chart_data.series, d => d3.max(d.values))]).nice()
.range([height - margin.bottom, margin.top])
const xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
const 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(line_chart_data.y))
svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);
const line = d3.line()
.defined(d => !isNaN(d))
.x((d, i) => x(line_chart_data.dates[i]))
.y(d => y(d))
const path = svg.append("g")
.attr("fill", "none")
.attr("stroke", colors[0])
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.selectAll("path")
.data(line_chart_data.series)
.join("path")
.style("mix-blend-mode", "multiply")
.attr("d", d => line(d.values));

svg.call(hover, path);

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, calendar_width, calendar_height * years.length])
.attr("font-family", "sans-serif")
.attr("font-size", 10);

const year = svg.selectAll("g")
.data(years)
.join("g")
.attr("transform", (d, i) => `translate(40.5,${calendar_height * i + cellSize * 1.5})`);

year.append("text")
.attr("x", -5)
.attr("y", -5)
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(([key]) => key);

year.append("g")
.attr("text-anchor", "end")
.selectAll("text")
.data((weekday === "weekday" ? d3.range(2, 7) : d3.range(7)).map(i => new Date(1995, 0, i)))
.join("text")
.attr("x", -5)
.attr("y", d => (countDay(d) + 0.5) * cellSize)
.attr("dy", "0.31em")
.text(formatDay);

year.append("g")
.selectAll("rect")
.data(weekday === "weekday"
? ([, values]) => values.filter(d => ![0, 6].includes(d.date.getUTCDay()))
: ([, values]) => values)
.join("rect")
.attr("width", cellSize - 1)
.attr("height", cellSize - 1)
.attr("x", d => timeWeek.count(d3.utcYear(d.date), d.date) * cellSize + 0.5)
.attr("y", d => countDay(d.date) * cellSize + 0.5)
.attr("fill", d => color(d.value))
.append("title")
.text(d => `${formatDate(d.date)}
Retail Percent Change:${formatValue(d.value)}${d.retail_and_recreation === undefined ? "" : `
Retail Change From Baseline: ${formatClose(d.retail_and_recreation)}`}`);

const month = year.append("g")
.selectAll("g")
.data(([, values]) => d3.utcMonths(d3.utcMonth(values[0].date), values[values.length - 1].date))
.join("g");

month.filter((d, i) => i).append("path")
.attr("fill", "none")
.attr("stroke", "#fff")
.attr("stroke-width", 3)
.attr("d", pathMonth);

month.append("text")
.attr("x", d => timeWeek.count(d3.utcYear(d), timeWeek.ceil(d)) * cellSize + 2)
.attr("y", -5)
.text(formatMonth);

return svg.node();
}
Insert cell
Insert cell
Insert cell
years = d3.groups(data, d => d.date.getUTCFullYear()).reverse()
Insert cell
cellSize = 17
Insert cell
calendar_width = 954
Insert cell
calendar_height = cellSize * (weekday === "weekday" ? 7 : 9)
Insert cell
timeWeek = weekday === "sunday" ? d3.utcSunday : d3.utcMonday
Insert cell
countDay = weekday === "sunday" ? d => d.getUTCDay() : d => (d.getUTCDay() + 6) % 7
Insert cell
function pathMonth(t) {
const n = weekday === "weekday" ? 5 : 7;
const d = Math.max(0, Math.min(n, countDay(t)));
const w = timeWeek.count(d3.utcYear(t), t);
return `${d === 0 ? `M${w * cellSize},0`
: d === n ? `M${(w + 1) * cellSize},0`
: `M${(w + 1) * cellSize},0V${d * cellSize}H${w * cellSize}`}V${n * cellSize}`;
}
Insert cell
formatValue = d3.format("+.2%")
Insert cell
formatClose = d3.format(",.2f")
Insert cell
formatDate = d3.utcFormat("%x")
Insert cell
formatDay = d => "SMTWTFS"[d.getUTCDay()]
Insert cell
formatMonth = d3.utcFormat("%b")
Insert cell
color = {
const max = d3.quantile(data.map(d => Math.abs(d.value)).sort(d3.ascending), 0.5);
return d3.scaleSequential(d3.interpolatePiYG).domain([-max, +max]);
}
Insert cell
d3 = require("d3@v5", "d3-array@2")
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
function hover(svg, path) {
const x = d3.scaleUtc()
.domain(d3.extent(line_chart_data.dates))
.range([margin.left, width - margin.right])
const y = d3.scaleLinear()
.domain([-100, d3.max(line_chart_data.series, d => d3.max(d.values))]).nice()
.range([height - margin.bottom, margin.top])
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")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "middle")
.attr("y", -8);

function moved() {
d3.event.preventDefault();
const mouse = d3.mouse(this);
const xm = x.invert(mouse[0]); // x coordinate of mouse pointer
const ym = y.invert(mouse[1]); // y coordinate of mouse pointer
const i1 = d3.bisectLeft(line_chart_data.dates, xm, 1);
const i0 = i1 - 1;
const i = xm - line_chart_data.dates[i0] > line_chart_data.dates[i1] - xm ? i1 : i0;
const s = d3.least(line_chart_data.series, d => Math.abs(d.values[i] - ym));
path.attr("stroke", d => d === s ? null : "#ddd").filter(d => d === s).raise();
console.log(s);
dot.attr("transform", `translate(${x(line_chart_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);
}

function left() {
path.style("mix-blend-mode", "multiply").attr("stroke", null);
dot.attr("display", "none");
}
}
Insert cell
height = 250;
Insert cell
colors = ["#F45656", "#EAEA34"]
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