This repository has been archived by the owner on Dec 15, 2019. It is now read-only.
/
seqdia.go
160 lines (140 loc) · 3.4 KB
/
seqdia.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package design
import (
"io"
"reflect"
"github.com/gregoryv/go-design/shape"
)
// NewSequenceDiagram returns a sequence diagram with default column
// width.
func NewSequenceDiagram() *SequenceDiagram {
return &SequenceDiagram{
Diagram: NewDiagram(),
ColWidth: 190,
VMargin: 10,
}
}
// SequenceDiagram defines columns and links between columns.
type SequenceDiagram struct {
Diagram
ColWidth int
VMargin int // top margin for each horizontal lane
columns []string
links []*Link
}
// WriteSvg renders the diagram as SVG to the given writer.
func (d *SequenceDiagram) WriteSvg(w io.Writer) error {
var (
colWidth = d.ColWidth
top = d.top()
x = d.Pad.Left
y1 = top + d.TextPad.Bottom + d.Font.LineHeight // below label
y2 = d.Height()
)
lines := make([]*shape.Line, len(d.columns))
for i, column := range d.columns {
label := shape.NewLabel(column)
label.Font = d.Font
label.Pad = d.Pad
label.SetX(i * colWidth)
label.SetY(top)
firstColumn := i == 0
if firstColumn {
x += label.Width() / 2
}
line := shape.NewLine(x, y1, x, y2)
line.SetClass("column-line")
lines[i] = line
x += colWidth
d.VAlignCenter(lines[i], label)
d.Place(lines[i], label)
}
y := y1 + d.plainHeight()
for _, lnk := range d.links {
fromX := lines[lnk.fromIndex].Start.X
toX := lines[lnk.toIndex].Start.X
label := shape.NewLabel(lnk.text)
label.Font = d.Font
label.Pad = d.Pad
label.SetX(fromX)
label.SetY(y - 3 - d.Font.LineHeight)
if lnk.toSelf() {
margin := 15
// add two lines + arrow
l1 := shape.NewLine(fromX, y, fromX+margin, y)
l1.SetClass(lnk.class())
l2 := shape.NewLine(fromX+margin, y, fromX+margin, y+d.Font.LineHeight*2)
l2.SetClass(lnk.class())
d.HAlignCenter(l2, label)
label.SetX(fromX + l1.Width() + d.TextPad.Left)
label.SetY(y + 3)
arrow := shape.NewArrow(
l2.End.X,
l2.End.Y,
l1.Start.X,
l2.End.Y,
)
arrow.SetClass(lnk.class())
d.Place(l1, l2, arrow, label)
y += d.selfHeight()
} else {
arrow := shape.NewArrow(
fromX,
y,
toX,
y,
)
arrow.SetClass(lnk.class())
d.VAlignCenter(arrow, label)
d.Place(arrow, label)
y += d.plainHeight()
}
}
return d.Diagram.WriteSvg(w)
}
// Width returns the total width of the diagram
func (d *SequenceDiagram) Width() int {
if d.Svg.Width != 0 {
return d.Svg.Width
}
return len(d.columns) * d.ColWidth
}
// Height returns the total height of the diagram
func (d *SequenceDiagram) Height() int {
if d.Svg.Height != 0 {
return d.Svg.Height
}
if len(d.columns) == 0 {
return 0
}
height := d.top() + d.plainHeight()
for _, lnk := range d.links {
if lnk.toSelf() {
height += d.selfHeight()
continue
}
height += d.plainHeight()
}
return height
}
// selfHeight is the height of a self referencing link
func (d *SequenceDiagram) selfHeight() int {
return 3*d.Font.LineHeight + d.Pad.Bottom
}
// plainHeight returns the height of and arrow and label
func (d *SequenceDiagram) plainHeight() int {
return d.Font.LineHeight + d.Pad.Bottom + d.VMargin
}
func (d *SequenceDiagram) top() int {
return d.Pad.Top
}
func (d *SequenceDiagram) AddColumns(names ...string) {
d.columns = append(d.columns, names...)
}
func (d *SequenceDiagram) SaveAs(filename string) error {
return saveAs(d, d.Style, filename)
}
func (d *SequenceDiagram) AddStruct(obj interface{}) string {
name := reflect.TypeOf(obj).String()
d.AddColumns(name)
return name
}