Published
Edited
Oct 21, 2021
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

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

const trail = svg
.append("path")
.attr("fill", "none")
.attr("stroke", "gray")
.attr("stroke-width", 0.5)
.attr("stroke-miterlimit", 1);

const path = svg
.append("path")
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-miterlimit", 1)
.attr("stroke-width", 2);

const yearTitle = svg
.append("text")
.attr("font-size", 80)
.attr("font-family", "sans")
.attr("x", (width * 3.5) / 5)
.attr("y", (height * 1.5) / 5)
.attr("opacity", 0.2);

const dot = svg.append("circle").attr("fill", "red").attr("r", 4);

return Object.assign(svg.node(), {
update(date) {
const year = date.getFullYear();

yearTitle.text(year);

if (year > 2017) {
svg
.selectAll(`.path-${year - 1}`)
.data([1])
.enter()
.append("path")
.classed(`path-${year - 1}`, true)
.attr("fill", "none")
.attr("stroke", "gray")
.attr("stroke-width", 0.5)
.attr("stroke-miterlimit", 1)
.attr("d", line(finalData.get(year - 1)));
}

const yearData = finalData.get(year);

const i = bisectDate(yearData, d3.utcMonth.offset(date, -3), 0);
const j = bisectDate(yearData, date, 0);

trail.attr("d", line(yearData.slice(0, i)));
path.attr("d", line(yearData.slice(Math.max(0, i - 1), j)));
dot.attr("cx", x(date.getDOY())).attr("cy", y(yearData[j - 1].moving));
}
});
}
Insert cell
y(11000)
Insert cell
margin.top
Insert cell
y(9998)
Insert cell
d3.max(data, (d) => +d.total)
Insert cell
Insert cell
finalData = {
let retval = new Map();

yearlyData.forEach((year) => {
const moving = movingAverage(
year[1].map((d) => +d.total),
14
);

var val = year[1];

retval.set(
year[0],
val.map((d, i) => {
return { date: d.date, total: d.total, moving: moving[i] };
})
);
});

return retval;
}
Insert cell
yearlyData = d3.groups(data, (d) => d.date.getFullYear())
Insert cell
bisectDate = d3.bisector((d) => d.date).left
Insert cell
chart.update(date)
Insert cell
line = d3
.line()
.x((d) => x(d.date.getDOY()))
.y((d) => y(d.moving))
// .curve(d3.curveBasis)
.curve(d3.curveCatmullRom)
Insert cell
Insert cell
Insert cell
x = d3
.scaleLinear()
.domain([0, 366])
.nice()
.rangeRound([margin.left, width - margin.right])
Insert cell
y = d3
.scaleLinear()
.domain([0, d3.max(data, (d) => +d.total)])
.range([height - margin.bottom, margin.top])
Insert cell
d3.max(data, (d) => d.total)
Insert cell
xAxis = (g) =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks())
.call((g) =>
g
.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text("Dan u godini →")
)
Insert cell
yAxis = (g) =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call((g) =>
g
.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text("↑ Ukupni dnevni promet po dionici")
)
Insert cell
grid = g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));
Insert cell
height = 640
Insert cell
margin = ({ top: 20, right: 20, bottom: 36, left: 50 })
Insert cell
import {Scrubber} from "@mbostock/scrubber"
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