Published
Edited
Mar 13, 2019
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
chart = async (data) => {
const svg = d3.select(DOM.svg(width, height));
const x = d3.scaleBand()
.domain(data.map((object) => object[0]))
.range([margin.left, width - margin.right]);
const y = d3.scaleLinear().domain([0, d3.max(data, d => d[1])]).nice()
.rangeRound([height - margin.bottom, margin.top])
const bar = svg.append("g")
.attr("fill", "steelblue")
.selectAll("rect")
.data(data.map(obj => [obj[0], obj[1]]))
.join("rect")
.attr("x", d => x(d[0]))
.attr("y", d => y(d[1]))
.attr("width", x.bandwidth()-2)
.attr("height", d => y(0) - y(d[1]));
const xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-90)")
.call(g => g.append("text")
.attr("x", width - margin.right)
.attr("y", -4)
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(data.x));
const yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 4)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y));
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
return svg.node();
}
Insert cell
height = 500
Insert cell
margin = ({top: 20, right: 20, bottom: 100, left: 40})
Insert cell
d3 = require("d3@5")
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