Published
Edited
May 14, 2022
Insert cell
# Bar-Chart 2
Insert cell
chart = {
const svg = d3.create("svg")
.attr("height", height - margin.top - margin.bottom)
.attr("width", width - margin.left - margin.right)
.attr("viewBox", [0, 0, width, height]);
svg
.append("g")
.attr("fill", c1)
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d, i) => x(i))
.attr("y", (d) => y(d.Rainfall))
.transition()
.ease(d3.easeBounceOut)
.duration(speed)
.attr("height", d => y(0) - y(d.Rainfall))
.attr("width", x.bandwidth())
.attr("class", "rectangle");

svg
.append("text")
.attr("text-anchor", "middle")
.attr("y", 0 - (margin.left /4))
.attr("x", 0 - (height / 2))
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.attr("font-size", "16px")
.text("Rainfall (mm)");

svg
.append("text")
.attr("x", (width / 2))
.attr("y", margin.top)
.attr("text-anchor", "middle")
.style("font-size", "24px")
.style("text-decoration", "underline")
.text("Donegal Rainfall 2020 - 2022");

svg.append("g")
.call(xAxis)
.selectAll("text")
.attr("transform", "rotate(90)")
.attr("text-anchor", "start")
.attr("x", "10")
.attr("y", "-5");

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

return svg.node();
}
Insert cell
viewof size = select({
title: "Chart Size",
options: [400, 500, 600],
value: 400
})
Insert cell
viewof c1 = color({
value: "#0000ff",
title: "Color",
description: "Pls Choose"
})
Insert cell
viewof speed = select({
title: "Transition Speed",
options: [1000, 2000, 3000],
value: 400
})
Insert cell
function xAxis(g) {
g.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(d3.axisBottom(x).tickFormat(i => data[i].Date))
.attr("font-size", "14px")
}
Insert cell
function yAxis(g) {
g.attr("transform", `translate(${margin.left}, 0)`)
.call(d3.axisLeft(y).ticks(null, data.format))
.attr("font-size", "14px")
}
Insert cell
margin = ({top: 50, bottom: 60, left: 50, right: 50});
Insert cell
height = size
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return d.Rainfall; })])
.range([height - margin.bottom, margin.top])
Insert cell
x = d3.scaleBand()
.domain(d3.range(data.length))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
data = FileAttachment("rain.csv").csv({typed:true})
Insert cell
import {color} from "@jashkenas/inputs"
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