Published
Edited
Aug 5, 2019
Insert cell
md`# Google Spreadsheet Example`
Insert cell
chart = {
const svg = d3.create('svg')
.attr("viewBox", [0, 0, width, height]);
svg.append("g")
.attr("fill", "steelblue")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(d.Label))
.attr("y", d => y(d.Value))
.attr("height", d => y(0) - y(d.Value))
.attr("width", x.bandwidth());

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

svg.append("g")
.call(yAxis);
return svg.node();
}
Insert cell
data = (await d3.csv(url, d => {
d.Value = +d.Value;
return d;
})).sort((a, b) => b.Value - a.Value)
Insert cell
x = d3.scaleBand()
.domain(data.map(d => d.Label))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.Value)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
Insert cell
margin = ({top: 20, right: 0, bottom: 30, left: 40})
Insert cell
height = 500
Insert cell
width = 500
Insert cell
url = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vRiKMg960prAPBQAvxmAKXwBldgpFycZJQcw7-QuaaTZcNmW_o7iNNKcPdHAePX-Wf_YSyK_1LOQANy/pub?gid=0&single=true&output=csv'
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