Skip to content

Commit

Permalink
Merge pull request #2407 from alixander/fix-legend
Browse files Browse the repository at this point in the history
fix legend
  • Loading branch information
alixander authored Mar 4, 2025
2 parents 7f3984a + d1c05bc commit 8a624ab
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 0 deletions.
10 changes: 10 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"oss.terrastruct.com/d2/d2parser"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/lib/color"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/textmeasure"
)

Expand Down Expand Up @@ -135,9 +136,18 @@ func (c *compiler) compileLegend(g *d2graph.Graph, m *d2ir.Map) {
continue
}
}
obj.Box = &geo.Box{}
obj.TopLeft = geo.NewPoint(0, 0)
objects = append(objects, obj)
}

for _, edge := range legendGraph.Edges {
edge.Route = []*geo.Point{
{X: 0, Y: 0},
{X: 0, Y: 0},
}
}

legend := &d2graph.Legend{
Objects: objects,
Edges: legendGraph.Edges,
Expand Down
25 changes: 25 additions & 0 deletions d2exporter/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestExport(t *testing.T) {
t.Run("connection", testConnection)
t.Run("label", testLabel)
t.Run("theme", testTheme)
t.Run("legend", testLegend)
}

func testShape(t *testing.T) {
Expand Down Expand Up @@ -204,6 +205,30 @@ func testTheme(t *testing.T) {
runa(t, tcs)
}

func testLegend(t *testing.T) {
tcs := []testCase{
{
name: "basic_legend",
dsl: `vars: {
d2-legend: {
legend: {
l1: Rectangles {shape: rectangle}
l2: Ovals {shape: oval}
l1 -> l2: Connection
}
}
}
x: {shape: rectangle}
y: {shape: oval}
x -> y: connects
`,
},
}

runa(t, tcs)
}

func runa(t *testing.T, tcs []testCase) {
for _, tc := range tcs {
tc := tc
Expand Down
36 changes: 36 additions & 0 deletions testdata/d2compiler/TestCompile/legend.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8a624ab

Please sign in to comment.