package d2grid
import (
"strconv"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/lib/geo"
)
type gridDiagram struct {
root *d2graph .Object
objects []*d2graph .Object
edges []*d2graph .Edge
rows int
columns int
rowDirected bool
width float64
height float64
verticalGap int
horizontalGap int
}
func newGridDiagram(root *d2graph .Object ) *gridDiagram {
gd := gridDiagram {
root : root ,
objects : root .ChildrenArray ,
verticalGap : DEFAULT_GAP ,
horizontalGap : DEFAULT_GAP ,
}
if root .GridRows != nil {
gd .rows , _ = strconv .Atoi (root .GridRows .Value )
}
if root .GridColumns != nil {
gd .columns , _ = strconv .Atoi (root .GridColumns .Value )
}
if gd .rows != 0 && gd .columns != 0 {
if root .GridRows .MapKey .Range .Before (root .GridColumns .MapKey .Range ) {
gd .rowDirected = true
}
capacity := gd .rows * gd .columns
for capacity < len (gd .objects ) {
if gd .rowDirected {
gd .rows ++
capacity += gd .columns
} else {
gd .columns ++
capacity += gd .rows
}
}
} else if gd .columns == 0 {
gd .rowDirected = true
if len (gd .objects ) < gd .rows {
gd .rows = len (gd .objects )
}
} else {
if len (gd .objects ) < gd .columns {
gd .columns = len (gd .objects )
}
}
if root .GridGap != nil {
gd .verticalGap , _ = strconv .Atoi (root .GridGap .Value )
gd .horizontalGap = gd .verticalGap
}
if root .VerticalGap != nil {
gd .verticalGap , _ = strconv .Atoi (root .VerticalGap .Value )
}
if root .HorizontalGap != nil {
gd .horizontalGap , _ = strconv .Atoi (root .HorizontalGap .Value )
}
for _ , o := range gd .objects {
o .TopLeft = geo .NewPoint (0 , 0 )
}
return &gd
}
func (gd *gridDiagram ) shift (dx , dy float64 ) {
for _ , obj := range gd .objects {
obj .MoveWithDescendants (dx , dy )
}
for _ , e := range gd .edges {
e .Move (dx , dy )
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .