Public
Edited
May 27
Insert cell
Insert cell
data = [
{ name: "Αθήνα", value: 643452 },
{ name: "Θεσσαλονίκη", value: 319045 },
{ name: "Πάτρα", value: 215922 },
{ name: "Ηράκλειο", value: 179302 }
]


Insert cell
d3 = require("d3@7")

Insert cell
chart = {
const width = 500;
const height = 500;
const margin = { top: 20, right: 20, bottom: 30, left: 80 };

const svg = d3.select(DOM.svg(width, height));

const x = d3.scaleBand()
.domain(data.map(d => d.name))
.range([margin.left, width - margin.right])
.padding(0.1);

const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.nice()
.range([height - margin.bottom, margin.top]);

svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(d.name))
.attr("y", y(0))
.attr("height", 0)
.attr("width", x.bandwidth())
.attr("fill", "steelblue")
.on("mouseover", function () {
d3.select(this).transition().duration(200).attr("fill", "orange");
})
.on("mouseout", function () {
d3.select(this).transition().duration(200).attr("fill", "steelblue");
})
.transition()
.duration(800)
.delay((d, i) => i * 100)
.attr("y", d => y(d.value))
.attr("height", d => y(0) - y(d.value));

svg.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x));

svg.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickFormat(d3.format(",")))

return svg.node();
}

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