evenly size grid nodes
This commit is contained in:
parent
06fb313d3e
commit
acd0638325
2 changed files with 47 additions and 13 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package d2grid
|
package d2grid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"oss.terrastruct.com/d2/d2graph"
|
"oss.terrastruct.com/d2/d2graph"
|
||||||
|
|
@ -12,6 +13,8 @@ type grid struct {
|
||||||
rows int
|
rows int
|
||||||
columns int
|
columns int
|
||||||
|
|
||||||
|
cellWidth float64
|
||||||
|
cellHeight float64
|
||||||
width float64
|
width float64
|
||||||
height float64
|
height float64
|
||||||
}
|
}
|
||||||
|
|
@ -26,7 +29,6 @@ func newGrid(root *d2graph.Object) *grid {
|
||||||
}
|
}
|
||||||
|
|
||||||
// compute exact row/column count based on values entered
|
// compute exact row/column count based on values entered
|
||||||
// TODO consider making this based on node dimensions
|
|
||||||
if g.rows == 0 {
|
if g.rows == 0 {
|
||||||
// set rows based on number of columns
|
// set rows based on number of columns
|
||||||
g.rows = len(g.nodes) / g.columns
|
g.rows = len(g.nodes) / g.columns
|
||||||
|
|
@ -48,6 +50,43 @@ func newGrid(root *d2graph.Object) *grid {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if we have the following nodes for a 2 row, 3 column grid
|
||||||
|
// . ┌A──────────────────┐ ┌B─────┐ ┌C────────────┐ ┌D───────────┐ ┌E───────────────────┐
|
||||||
|
// . │ │ │ │ │ │ │ │ │ │
|
||||||
|
// . │ │ │ │ │ │ │ │ │ │
|
||||||
|
// . └───────────────────┘ │ │ │ │ │ │ │ │
|
||||||
|
// . │ │ └─────────────┘ │ │ │ │
|
||||||
|
// . │ │ │ │ └────────────────────┘
|
||||||
|
// . └──────┘ │ │
|
||||||
|
// . └────────────┘
|
||||||
|
// Then we must get the max width and max height to determine the grid cell size
|
||||||
|
// . maxWidth├────────────────────┤
|
||||||
|
// . ┌A───────────────────┐ ┌B───────────────────┐ ┌C───────────────────┐ ┬maxHeight
|
||||||
|
// . │ │ │ │ │ │ │
|
||||||
|
// . │ │ │ │ │ │ │
|
||||||
|
// . │ │ │ │ │ │ │
|
||||||
|
// . │ │ │ │ │ │ │
|
||||||
|
// . │ │ │ │ │ │ │
|
||||||
|
// . │ │ │ │ │ │ │
|
||||||
|
// . └────────────────────┘ └────────────────────┘ └────────────────────┘ ┴
|
||||||
|
// . ┌D───────────────────┐ ┌E───────────────────┐
|
||||||
|
// . │ │ │ │
|
||||||
|
// . │ │ │ │
|
||||||
|
// . │ │ │ │
|
||||||
|
// . │ │ │ │
|
||||||
|
// . │ │ │ │
|
||||||
|
// . │ │ │ │
|
||||||
|
// . └────────────────────┘ └────────────────────┘
|
||||||
|
var maxWidth, maxHeight float64
|
||||||
|
for _, n := range g.nodes {
|
||||||
|
maxWidth = math.Max(maxWidth, n.Width)
|
||||||
|
maxHeight = math.Max(maxHeight, n.Height)
|
||||||
|
}
|
||||||
|
g.cellWidth = maxWidth
|
||||||
|
g.cellHeight = maxHeight
|
||||||
|
g.width = maxWidth + (float64(g.columns)-1)*(maxWidth+HORIZONTAL_PAD)
|
||||||
|
g.height = maxHeight + (float64(g.rows)-1)*(maxHeight+VERTICAL_PAD)
|
||||||
|
|
||||||
return &g
|
return &g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package d2grid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"math"
|
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"oss.terrastruct.com/d2/d2graph"
|
"oss.terrastruct.com/d2/d2graph"
|
||||||
|
|
@ -69,7 +68,7 @@ func withoutGrids(ctx context.Context, g *d2graph.Graph) (idToGrid map[string]*g
|
||||||
}
|
}
|
||||||
obj.Children = make(map[string]*d2graph.Object)
|
obj.Children = make(map[string]*d2graph.Object)
|
||||||
obj.ChildrenArray = nil
|
obj.ChildrenArray = nil
|
||||||
obj.Box = geo.NewBox(nil, grid.width+CONTAINER_PADDING*2, grid.height+CONTAINER_PADDING*2)
|
obj.Box = geo.NewBox(nil, grid.width+2*CONTAINER_PADDING, grid.height+2*CONTAINER_PADDING)
|
||||||
obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter))
|
obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter))
|
||||||
grids[obj.AbsID()] = grid
|
grids[obj.AbsID()] = grid
|
||||||
|
|
||||||
|
|
@ -97,21 +96,17 @@ func layoutGrid(g *d2graph.Graph, obj *d2graph.Object) (*grid, error) {
|
||||||
|
|
||||||
// position nodes
|
// position nodes
|
||||||
cursor := geo.NewPoint(0, 0)
|
cursor := geo.NewPoint(0, 0)
|
||||||
maxWidth := 0.
|
|
||||||
for i := 0; i < grid.rows; i++ {
|
for i := 0; i < grid.rows; i++ {
|
||||||
maxHeight := 0.
|
|
||||||
for j := 0; j < grid.columns; j++ {
|
for j := 0; j < grid.columns; j++ {
|
||||||
n := grid.nodes[i*grid.columns+j]
|
n := grid.nodes[i*grid.columns+j]
|
||||||
|
n.Width = grid.cellWidth
|
||||||
|
n.Height = grid.cellHeight
|
||||||
n.TopLeft = cursor.Copy()
|
n.TopLeft = cursor.Copy()
|
||||||
cursor.X += n.Width + HORIZONTAL_PAD
|
cursor.X += grid.cellWidth + HORIZONTAL_PAD
|
||||||
maxHeight = math.Max(maxHeight, n.Height)
|
|
||||||
}
|
}
|
||||||
maxWidth = math.Max(maxWidth, cursor.X-HORIZONTAL_PAD)
|
|
||||||
cursor.X = 0
|
cursor.X = 0
|
||||||
cursor.Y += float64(maxHeight) + VERTICAL_PAD
|
cursor.Y += float64(grid.cellHeight) + VERTICAL_PAD
|
||||||
}
|
}
|
||||||
grid.width = maxWidth
|
|
||||||
grid.height = cursor.Y - VERTICAL_PAD
|
|
||||||
|
|
||||||
// position labels and icons
|
// position labels and icons
|
||||||
for _, n := range grid.nodes {
|
for _, n := range grid.nodes {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue