Skip to main content

Grid Diagrams

Grid diagrams let you display objects in a structured grid.

DAGGER ENGINEANY DOCKER COMPATIBLE RUNTIMEANY CIWINDOWSLINUXMACOSKUBERNETES

Two keywords do all the magic:

  • grid-rows
  • grid-columns

Setting just grid-rows:

ExecutiveLegislativeJudicial

Setting just grid-columns:

ExecutiveLegislativeJudicial

Setting both grid-rows and grid-columns:

ExecutiveLegislativeJudicial

Width and height

To create specific constructions, use width and/or height.

ExecutiveLegislativeJudicialThe American Government

Notice how objects are evenly distributed within each row.

Cells expand to fill

When you define only one of row or column, objects will expand.

Notice how Voters and Non-voters fill the space.

ExecutiveLegislativeJudicialThe American GovernmentVotersNon-voters

Dominant direction

When you apply both row and column, the first appearance is the dominant direction. The dominant direction is the order in which cells are filled.

For example:

Since grid-rows is defined first, objects will fill rows before moving onto columns.

a ab abc abcd abcde abcdef abcdefg abcdefgh

But if it were reversed:

It would do the opposite.

a ab abc abcd abcde abcdef abcdefg abcdefgh
info

These animations are also pure D2, so you can animate grid diagrams being built-up. Use the animate-interval flag with this code. More on this later, in the composition section.

Gap size

You can control the gap size of the grid with 3 keywords:

  • vertical-gap
  • horizontal-gap
  • grid-gap

Setting grid-gap is equivalent to setting both vertical-gap and horizontal-gap.

vertical-gap and horizontal-gap can override grid-gap.

Gap size 0

grid-gap: 0 in particular can create some interesting constructions:

Like this map of Japan

北海道青森秋田岩手石川新潟山形宮城福井富山群馬栃木福島山口島根鳥取兵庫京都滋賀長野山梨埼玉茨城広島岡山大阪奈良岐阜愛知静岡TOKYO千葉長崎佐賀福岡和歌山三重神奈川熊本大分愛媛香川鹿児島宮崎高知徳島沖縄

D2 source

Or a table of data

ElementAtomic NumberAtomic MassMelting PointHydrogen11.008-259.16Carbon612.0113500Oxygen815.999-218.79
info

You may find it easier to just use Markdown tables though, especially if there are duplicate cells.

Month Savings Expenses Balance
January $250 $150 $100
February $80 $200 -$120
March $420 $180 $240

Gap size 0

Connections

Connections for grids themselves work normally as you'd expect.

TeleportJust-in-time Access viaInfrastructureIdentity ProviderEngineersMachinesHTTPS://> kubectl> tsh> apiDB Clients

Identity Native Proxy

Audit LogCert AuthoritySlackMattermostJiraPagerdutyEmailsshKubernetesMy SQLMongoDBPSQLWindows all connections audited and logged

Source code here.

Connections between grid cells

Connections between shapes inside a grid work a bit differently. Because a grid structure imposes positioning outside what the layout engine controls, the layout engine is also unable to make routes. Therefore, these connections are center-center straight segments, i.e., no path-finding.

npm i -g@forge/cliSet up anAtlassian siteView the helloworld appforgetunnelforgeloginforgecreateforgedeployforgeinstallHot reloadchanges?Step 1Step 2Step 3Step 4forgedeploy⬤ Forge CLI⬤ Required⬤ Optional YesNo

Source code here.

Random Matrix Generatorn-sized Vectorn-sized Vectorn-sized VectorDot productTarget dimension matrixm-sized Vectorm-sized Vectorm-sized Vector0.620.15n0.910.480.620.15n0.910.480.620.15n0.910.480.410.320.920.130.740.150.530.210.970.450.110.050.610.770.590.830.620.15m0.910.480.620.15m0.910.480.620.15m0.910.48

Source code here.

Nesting

Currently you can nest grid diagrams within grid diagrams. Nesting other types is coming soon.

headerfootercontentsidebar

Aligning with invisible elements

A common technique to align grid elements to your liking is to pad the grid with invisible elements.

Consider the following diagram.

us-east-1us-west-1abcdea

It'd be nicer if it were centered. This can be achieved by adding 2 invisible elements.

us-east-1us-west-1abcdea

Troubleshooting

Why is there extra padding in one cell?

Elements in a grid column have the same width and elements in a grid row have the same height.

So in this example, a small empty space in "Backend Node" is present.

KubernetesBackend NodeFrontend NodeClusterIPService 1Deployment 1ClusterIPService 2Deployment 2NEXT POD 1NEXT POD 2NEXT POD 3FLASK POD 1FLASK POD 2FLASK POD 3

It's due to the label of "Flask Pod" being slightly longer than "Next Pod". So the way we fix that is to set widths to match.

KubernetesBackend NodeFrontend NodeClusterIPService 1Deployment 1ClusterIPService 2Deployment 2NEXT POD 1NEXT POD 2NEXT POD 3FLASK POD 1FLASK POD 2FLASK POD 3