Published
Edited
Nov 17, 2021
1 fork
Insert cell
Insert cell
covidBloody = {
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);

svg
.append("text")
.attr("x", width / 2)
.attr("y", 60)
.attr("font-size", 40)
// .attr("font-weight", 700)
.attr("text-anchor", "middle")
.text("Tjedne COVID smrti");

svg
.selectAll("rect")
.data(weeklyData)
.enter()
.append("rect")
.attr("fill", "#C5161D")
.attr("width", xBand.bandwidth() + 1)
.attr("height", 0)
// .attr("height", (d) => y(d.sum) - margin.top)
.attr("x", (d, i) => xBand(i))
.attr("y", margin.top)
.transition()
.duration((d, i) => 3000 + Math.random() * 1500)
.attr("height", (d) => y(d.sum) - margin.top);

var fontSizeLabel = Math.floor(width / 58);
svg
.append("text")
.attr("x", x(new Date(2020, 11, 22)))
.attr("y", y(100))
.attr("font-size", fontSizeLabel)
.attr("font-family", "sans-serif")
.attr("fill", "white")
.attr("text-anchor", "middle")
.attr("opacity", 0.75)
.text("UKUPNO");

svg
.append("text")
.attr("x", x(new Date(2020, 11, 22)))
.attr("y", y(100) + fontSizeLabel * 1.25)
.attr("font-size", Math.floor(width / 58))
.attr("font-family", "sans-serif")
.attr("fill", "white")
.attr("text-anchor", "middle")
.attr("opacity", 0.75)
.text("UMRLIH");

var countTextNumber = svg
.append("text")
.attr("x", x(new Date(2020, 11, 22)))
.attr("y", y(100) + fontSizeLabel * 3)
.attr("font-size", Math.floor(width / 35))
.attr("fill", "white")
.attr("text-anchor", "middle")

.text(d3.format(",")(total));

return svg.node();
}
Insert cell
width / 16
Insert cell
new Date(2022, 11, 15)
Insert cell
total = d3.sum(weeklyData, (d) => d.sum)
Insert cell
grid = (g) =>
g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.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
xAxis = (g) =>
g.attr("transform", `translate(0,${margin.top})`).call(
d3
.axisTop(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.selectAll(".tick").attr("font-size", 12) //.attr("font-weight", 700)
)
// .call((g) =>
// g
// .select(".tick:last-of-type text")
// .clone()
// .attr("x", 3)
// .attr("text-anchor", "start")
// .attr("font-weight", "bold")
// // .attr("fill", "grey")
// .text("Tjedno umrlih")
// )
Insert cell
xBand = d3
.scaleBand()
.domain(d3.range(weeklyData.length))
.range([margin.left, width - margin.right])
.padding(0)
Insert cell
x = d3
.scaleUtc()
.domain(d3.extent(data, (d) => d.date))
.range([margin.left, width - margin.right])
Insert cell
y(0)
Insert cell
margin.top
Insert cell
y = d3
.scaleLinear()
.domain([0, d3.max(weeklyData, (d) => d.sum)])
.nice()
.range([margin.top, height - margin.bottom])
Insert cell
margin = ({ top: 100, right: 30, bottom: 30, left: 40 })
Insert cell
height = 700
Insert cell
weeklyData = {
let retval = [];

var sum = 0;
var week = null;
for (var i = 0; i < data.length; i++) {
let week_nr = getWeek(data[i].date);
if (!week) week = week_nr;
if (week_nr != week) {
retval.push({
week: week,
sum: sum,
year: data[i].date.getFullYear()
});
sum = 0;
week = week_nr;
}
sum += data[i].value;

if (i == data.length - 1) {
retval.push({
week: week,
sum: sum,
year: data[i].date.getFullYear()
});
}
}

return retval;
}
Insert cell
Insert cell
d3.sum(data, (d) => d.value)
Insert cell
data
Insert cell
import { data } from "@bagami/daily-covid-19-deaths-croatia"
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