Published
Edited
Jun 4, 2020
1 fork
1 star
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("stroke-width", 2);
const x = d3.scaleBand()
.domain(data.map(d => d.cat2))
.range([margin.left, width - margin.right])
.padding(1);
const xAxis = g => g
.attr("transform", `translate(0,30)`)
.call(d3.axisTop(x).tickSizeOuter(0))
.attr("text-anchor", "middle")
.style("font-size", "Large")
const y = d3.scaleLinear()
.domain(d3.extent(data.map(d => d.rank2)))
//.domain(["transform", `translate(${margin.left -50},0)`])
.range([height - margin.bottom, margin.top]);
const yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))

const gx = svg.append("g").call(xAxis);
const gy = svg.append("g").call(yAxis);

svg
.append("g")
.selectAll("text")
.data(data)
.join("text")
.attr("x", d => x(d.cat2))
.attr("y", d => y(d.rank2))
.text(d => d.state)
.attr("text-anchor" , "middle")
.call(drag);

return svg.node();
}
Insert cell
drag = {

function dragstarted(d) {
d3.select(this).raise().attr("stroke", "black");
}

function dragged(d) {
d3.select(this).attr("x", d.x = d3.event.x).attr("y", d.y = d3.event.y);
}

function dragended(d) {
d3.select(this).attr("stroke", null);
}

return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
data = d3.csvParse(await FileAttachment("table_data@1.csv").text(), d3.autoType)

Insert cell
Insert cell
Insert cell
//width = 400
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