Published
Edited
Sep 8, 2020
Fork of Donut Chart
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.append("g")
.attr("fill", "#ffc709")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d, i) => x(i))
.attr("y", d => y(d.value))
.attr("height", d => y(0) - y(d.value))
.attr("width", x.bandwidth());

svg.append("g")
.call(xAxis)
.selectAll("text")
.data(data)
.text(d => `${d.name} (${brazil_number_format(d.value)})`)
.attr("color", "black")
.attr("font-size", 18)
.attr("text-anchor", "start")
.attr("transform", `translate(-15,-10)rotate(-90)`)

svg.append("g")
.call(yAxis)
.selectAll("text")
.attr("color", "black")
.attr("font-size", 16)
return svg.node();
}
Insert cell
//data = d3.csvParse(await FileAttachment("population-by-age.csv").text(), d3.autoType)
Insert cell
data = ([
{
"name": "A",
"value": 502.590
},
{
"name": "B",
"value": 580.7
}
])
Insert cell
height = Math.min(width, 500)
Insert cell
x = d3.scaleBand()
.domain(d3.range(data.length))
.range([margin.left, width - margin.right])
.padding(0.1)

// x = d3.scaleUtc()
// .domain(d3.extent(data, d => d.date))
// .rangeRound([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)]).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).tickFormat(i => data[i].name).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickFormat(d => brazil_number_format(d)))
.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")
.attr("id", "mobility-title")
.text(""))
.call(g => g.selectAll(".tick line")
.select(function() { return this.parentNode.appendChild(this.cloneNode()); })
.attr("stroke-opacity", 0.3)
.attr("stroke-width", 2)
.attr("stroke", "#ccc")
.attr("x2", width - margin.left - margin.right))
Insert cell
arc = {
const radius = Math.min(width, height) / 2;
return d3.arc().innerRadius(radius * 0.67).outerRadius(radius - 1);
}
Insert cell
pie = d3.pie()
.padAngle(0.005)
.sort(null)
.value(d => d.value)
Insert cell
margin = ({top: 30, right: 30, bottom: 30, left: 90})
Insert cell
locale = (
{
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["R$", ""],
"dateTime": "%d/%m/%Y %H:%M:%S",
"date": "%d/%m/%Y",
"time": "%H:%M:%S",
"periods": ["AM", "PM"],
"days": ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"],
"shortDays": ["Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sáb"],
"months": ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
"shortMonths": ["Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez"]
}
)
Insert cell
brazil_locale = d3.formatDefaultLocale(locale);
Insert cell
brazil_number_format = brazil_locale.format(",.2f")
Insert cell
d3 = require("d3@5")
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