Skip to content

Hexagon grid with Plot - How? #2262

Answered by mbostock
andreassolberg asked this question in Q&A
Discussion options

You must be logged in to vote

You either need to specify the hexagon grid in abstract coordinates and project them (either using a scale or a projection), or you need to compute the hexagon grid in screen coordinates.

If you want to use screen coordinates, you could use identity scales, which effectively disables the x and y scales.

Plot.plot({
  x: { type: "identity" },
  y: { type: "identity" },
  marks: [
    Plot.hexagon(d3.cross(d3.range(2, 16), d3.range(2, 11)), {
      r: radius, // Hexagon radius
      x: ([i, j]) => radius * (2 * i + (j) % 2), // Horizontal position
      y: ([i, j]) => radius * Math.sqrt(3) * j // Vertical position
    })
  ]
})

If you want to use abstract coordinates, you could use a geo m…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@andreassolberg
Comment options

Comment options

You must be logged in to vote
1 reply
@andreassolberg
Comment options

Answer selected by andreassolberg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants