Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

- labels on scenario/step boards can be set with primary value (like layers) [#2579](https://github.com/terrastruct/d2/pull/2579)
- autoformatter preserves order of boards [#2580](https://github.com/terrastruct/d2/pull/2580)
- rename "Legend" with a title/label of your choosing (especially useful for non-English diagrams) [#2582](https://github.com/terrastruct/d2/pull/2582)

#### Bugfixes ⛑️

Expand Down
4 changes: 4 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func (c *compiler) compileLegend(g *d2graph.Graph, m *d2ir.Map) {
Edges: legendGraph.Edges,
}

if legendField.Primary() != nil && legendField.Primary().Value != nil {
legend.Label = legendField.Primary().Value.ScalarString()
}

if len(legend.Objects) > 0 || len(legend.Edges) > 0 {
g.Legend = legend
}
Expand Down
6 changes: 5 additions & 1 deletion d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ x: {

text: `
vars: {
d2-legend: {
d2-legend: "凡例"{
User: "A person who interacts with the system" {
shape: person
style: {
Expand Down Expand Up @@ -762,6 +762,10 @@ user -> db: Uses
return
}

if g.Legend.Label != "凡例" {
t.Errorf("Expected label")
}

// 2. Verify the correct objects are in the legend
if len(g.Legend.Objects) != 2 {
t.Errorf("Expected 2 objects in legend, got %d", len(g.Legend.Objects))
Expand Down
4 changes: 3 additions & 1 deletion d2exporter/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func Export(ctx context.Context, g *d2graph.Graph, fontFamily *d2fonts.FontFamil
}

if g.Legend != nil {
legend := &d2target.Legend{}
legend := &d2target.Legend{
Label: g.Legend.Label,
}

if len(g.Legend.Objects) > 0 {
legend.Shapes = make([]d2target.Shape, len(g.Legend.Objects))
Expand Down
1 change: 1 addition & 0 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Graph struct {
}

type Legend struct {
Label string `json:"label,omitempty"`
Objects []*Object `json:"objects,omitempty"`
Edges []*Edge `json:"edges,omitempty"`
}
Expand Down
8 changes: 6 additions & 2 deletions d2renderers/d2svg/d2svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,12 @@ func RenderLegend(buf *bytes.Buffer, diagram *d2target.Diagram, diagramHash stri
legendEl.Rx = 4
fmt.Fprint(buf, legendEl.Render())

fmt.Fprintf(buf, `<text class="text-bold" x="%d" y="%d" style="font-size: %dpx;">Legend</text>`,
legendX+LEGEND_PADDING, legendY+LEGEND_PADDING+LEGEND_FONT_SIZE, LEGEND_FONT_SIZE+2)
legendLabel := "Legend"
if diagram.Legend.Label != "" {
legendLabel = diagram.Legend.Label
}
fmt.Fprintf(buf, `<text class="text-bold" x="%d" y="%d" style="font-size: %dpx;">%s</text>`,
legendX+LEGEND_PADDING, legendY+LEGEND_PADDING+LEGEND_FONT_SIZE, LEGEND_FONT_SIZE+2, html.EscapeString(legendLabel))

currentY := legendY + LEGEND_PADDING*2 + LEGEND_FONT_SIZE

Expand Down
7 changes: 6 additions & 1 deletion d2target/d2target.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type Diagram struct {
}

type Legend struct {
Label string `json:"label,omitempty"`
Shapes []Shape `json:"shapes,omitempty"`
Connections []Connection `json:"connections,omitempty"`
}
Expand Down Expand Up @@ -545,7 +546,11 @@ func (diagram Diagram) GetCorpus() string {
}

if diagram.Legend != nil {
corpus += "Legend"
if diagram.Legend.Label != "" {
corpus += diagram.Legend.Label
} else {
corpus += "Legend"
}
for _, s := range diagram.Legend.Shapes {
corpus += s.Label
}
Expand Down
1 change: 1 addition & 0 deletions e2etests/testdata/txtar/legend/dagre/board.exp.json

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

6 changes: 3 additions & 3 deletions e2etests/testdata/txtar/legend/dagre/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions e2etests/testdata/txtar/legend/elk/board.exp.json

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

6 changes: 3 additions & 3 deletions e2etests/testdata/txtar/legend/elk/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion e2etests/txtar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ c4person -> styling.c4styled -> styling.c4sized

-- legend --
vars: {
d2-legend: {
d2-legend: "凡例"{
a: {
label: Microservice
}
Expand Down
227 changes: 119 additions & 108 deletions testdata/d2compiler/TestCompile/legend.exp.json

Large diffs are not rendered by default.