Published
Edited
Jun 27, 2020
Insert cell
md`# Grouped Bar Chart`
Insert cell
// Come si cambia il nome all'asse x?
Insert cell
// Come si possono mostrare i valori nelle colonne?
Insert cell
viewof dd1 = select({
title: "Città più visitate per regione.",
description: "Scegli la regione che preferisci.",
options: ["Toscana", "Emilia-Romagna", "Veneto"],
value: "Toscana"
})
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));

svg.append("g")
.selectAll("g")
.data(Fdata)
.join("g")
.attr("transform", d => `translate(${x0(d[groupKey])},0)`)
.selectAll("rect")
.data(d => keys.map(key => ({key, value: d[key]})))
.join("rect")
.attr("x", d => x1(d.key))
.attr("y", d => y(d.value))
.attr("width", x1.bandwidth())
.attr("height", d => y(0) - y(d.value))
.attr("fill", d => color(d.key));

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

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

return svg.node();
}
Insert cell
x0 = d3.scaleBand()
.domain(Fdata.map(d => d[groupKey]))
.rangeRound([margin.left, width - margin.right])
.paddingInner(0.1)
Insert cell
['ciao', 'ok']
Insert cell
x1 = d3.scaleBand()
.domain(keys)
.rangeRound([0, x0.bandwidth()])
.padding(0.05)
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(Fdata, d => d3.max(keys, key => d[key]))]).nice()
.rangeRound([height - margin.bottom, margin.top])
Insert cell
color = d3.scaleOrdinal()
.range(["#98abc5", "#8a89a6"])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x0).tickSizeOuter(0))
.call(g => g.select(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, "s"))
.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(Fdata.y))
Insert cell
Fdata
Insert cell
md`# Import`
Insert cell
diseq = 'https://raw.githubusercontent.com/JohnPFL/ciao/master/most_visited_3_proportions.csv'
Insert cell
data = d3.csv(diseq, d3.autoType)
Insert cell
Fdata = data.filter(d => d.region === dd1)
Insert cell
groupKey = data.columns[1]
Insert cell
keys = ['rev_rel', 'cities']
Insert cell
margin = ({top: 10, right: 10, bottom: 20, left: 40})
Insert cell
height = 500
Insert cell
d3 = require("d3@5")
Insert cell
import { select } from "@jashkenas/inputs"
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