Published
Edited
Aug 10, 2021
2 forks
2 stars
Insert cell
Insert cell
Insert cell
gridData = [
{ x: 0, y: 0 },
{ x: 0, y: 1 },
{ x: 0, y: 2 },
{ x: 1, y: 0 },
{ x: 1, y: 1 },
{ x: 1, y: 2 },
{ x: 2, y: 0 },
{ x: 2, y: 1 },
{ x: 2, y: 2 },
];
Insert cell
Insert cell
Insert cell
Insert cell
xScale = d3.scalePoint().domain([0, 1, 2]).range([margin, width - margin])
Insert cell
yScale = d3.scalePoint().domain([0, 1, 2]).range([height - margin, margin])
Insert cell
Insert cell
{
// init svg
const svg = DOM.svg(width, height)
const sel = d3.select(svg)

// create g tags to append to (in grid shape)
const join = sel.selectAll('g.grid')
.data(gridData)
.enter().append('g')
.attr('class', 'grid')
.attr('transform', (d, i) => {
return `translate(${xScale(d.x)}, ${yScale(d.y)})`
})

// e.g., add circles to each g
sel.selectAll('g.grid').each(function(d,i) {
const cell = d3.select(this).append('g')

cell.append('circle').attr('r', 20).attr('fill', 'red')
})

return svg
}
Insert cell
d3 = require("d3@6")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more