Published
Edited
Jun 25, 2020
3 stars
Insert cell
Insert cell
Insert cell
manila_data = {
const text = await FileAttachment("manila_temp.csv").text();
return d3.csvParse(text, ({timestamp, temperature}) => ({
timestamp: parseDate(timestamp),
temperature: temperature
}));
}
Insert cell
ontario_data = {
const text = await FileAttachment("ontario_temp.csv").text();
return d3.csvParse(text, ({timestamp, temperature}) => ({
timestamp: parseDate(timestamp),
temperature: temperature
}));
}
Insert cell
Insert cell
data = ({series : [{name : "Manila", values:manila_data.map(item => item.temperature)},
{name : "Ontario", values:ontario_data.map(item => item.temperature)}],
dates: manila_data.map(item => item.timestamp)})
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const x = d3.scaleUtc()
.domain(d3.extent(manila_data, d => d.timestamp))
.range([margin.left, width - margin.right])
const y = d3.scaleLinear()
.domain([0, d3.max(manila_data, d => d.temperature)]).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(manila_data.temperature))
const line = d3.line()
.defined(d => !isNaN(d.temperature))
.x(d => x(d.timestamp))
.y(d => y(d.temperature))
svg.append("g")
.call(xAxis);

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

svg.append("path")
.datum(manila_data)
.attr("fill", "none")
.attr("stroke", colors[0])
.attr("stroke-width", 1)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);
svg.append("path")
.datum(ontario_data)
.attr("fill", "none")
.attr("stroke", colors[1])
.attr("stroke-width", 1)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);

return svg.node();
}
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(data.dates))
.range([margin.left, width - margin.right])
const y = d3.scaleLinear()
.domain([0, d3.max(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(data.y))
svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);
const line = d3.line()
.defined(d => !isNaN(d))
.x((d, i) => x(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(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
function hover(svg, path) {
const x = d3.scaleUtc()
.domain(d3.extent(data.dates))
.range([margin.left, width - margin.right])
const y = d3.scaleLinear()
.domain([0, d3.max(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]);
const ym = y.invert(mouse[1]);
const i1 = d3.bisectLeft(data.dates, xm, 1);
const i0 = i1 - 1;
const i = xm - data.dates[i0] > data.dates[i1] - xm ? i1 : i0;
const s = d3.least(data.series, d => Math.abs(d.values[i] - ym));
path.attr("stroke", d => d === s ? null : "#ddd").filter(d => d === s).raise();
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);
}

function left() {
path.style("mix-blend-mode", "multiply").attr("stroke", null);
dot.attr("display", "none");
}
}
Insert cell
parseDate = d3.utcParse("%Y%m%dT%H00");
Insert cell
d3 = require("d3@5", "d3-array@2")
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
height = 500
Insert cell
colors = ["#F45656", "#EAEA34"]
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