Public
Edited
Sep 26, 2023
Fork of Simple D3
1 fork
Insert cell
Insert cell
chart = {
const width = 600; // width of canvas
const height = 250; // height of canvas

// padding on left for congressional districts

let padding_left = 30;

// Create a horizontal scale for the number of Majority-minority seats.
const x = d3.scaleLinear()
.domain([0,d3.max(data, d => +d.number_ma_mj)])
.range([padding_left, (width-padding_left)])


// Create a vertial scale for the congressional districts.
const y = d3.scaleBand()
.domain(d3.map(data, d => d.congress))
.range([height, 0])
.padding(0.1);

// Create SVG canvas to put graphics in.
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto;");

// Create SVG canvas to put graphics in.


svg.append("g")
.attr("class", "bars")
.attr("fill", "steelblue")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(0) + 20)
.attr("y", d => y(d.congress))
.attr("height", 50)
.attr("width", d => x(d.number_ma_mj))
.join("text")


svg.append("g")
.selectAll("text")
.data(data)
.join("text")
.attr('x', d => x(d.number_ma_mj) + padding_left)
.attr('y', d => y(d.congress) + y.bandwidth() / 2) // Adjust the "-5" for vertical positioning
.attr('text-anchor', 'end')
.text(d => d.number_ma_mj);

svg.append("g")
.selectAll("text")
.data(data)
.join("text")
.attr('x', d => x(0))
.attr('y', d => y(d.congress) + y.bandwidth() / 2) // Adjust the "-5" for vertical positioning
.attr('text-anchor', 'middle')
.text(d => d.congress);



return svg.node();
}
Insert cell
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