Repository navigation
Expand file tree
/
Copy pathtable.go
More file actions
398 lines (366 loc) · 10.6 KB
/
Copy pathtable.go
File metadata and controls
398 lines (366 loc) · 10.6 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package charts
import (
"errors"
"github.com/go-analyze/charts/chartdraw"
)
// TableOptionRenderDirect renders a table directly to an image with the provided options.
// Table options differ from other charts as they include Painter initialization state.
// To render a table on an existing Painter, use TableOptionRender.
func TableOptionRenderDirect(opt TableChartOption) (*Painter, error) {
if opt.OutputFormat == "" {
opt.OutputFormat = chartDefaultOutputFormat
}
if opt.Width <= 0 {
opt.Width = defaultChartWidth
}
p := NewPainter(PainterOptions{
OutputFormat: opt.OutputFormat,
Width: opt.Width,
Height: 100, // is only used to calculate the height of the table
Font: opt.FontStyle.Font,
})
info, err := newTableChart(p, opt).render()
if err != nil {
return nil, err
}
p = NewPainter(PainterOptions{
OutputFormat: opt.OutputFormat,
Width: info.width,
Height: info.height,
Font: opt.FontStyle.Font,
})
if _, err = newTableChart(p, opt).renderWithInfo(info); err != nil {
return nil, err
}
return p, nil
}
// TableRenderValues renders a table chart with the provided header and data values.
func TableRenderValues(header []string, data [][]string, spanMaps ...map[int]int) (*Painter, error) {
opt := TableChartOption{
Header: header,
Data: data,
}
if len(spanMaps) != 0 {
spanMap := spanMaps[0]
spans := make([]int, len(opt.Header))
for index := range spans {
v, ok := spanMap[index]
if !ok {
v = 1
}
spans[index] = v
}
opt.Spans = spans
}
return TableOptionRenderDirect(opt)
}
type tableChart struct {
p *Painter
opt *TableChartOption
}
// newTableChart returns a table chart renderer.
func newTableChart(p *Painter, opt TableChartOption) *tableChart {
return &tableChart{
p: p,
opt: &opt,
}
}
// TableCell represents a single cell in a table.
type TableCell struct {
// Text is the text content of the table cell.
Text string
// FontStyle contains the font configuration for the cell text.
FontStyle FontStyle
// FillColor sets the background color for this table cell.
FillColor Color
// Row is the row index of the table cell.
Row int
// Column is the column index of the table cell.
Column int
}
// TableChartOption defines options for rendering a table chart.
type TableChartOption struct {
// OutputFormat specifies the output type: "svg", "png", "jpg".
OutputFormat string
// Theme specifies the colors used for the table.
Theme ColorPalette
// Padding specifies the padding around the table.
Padding Box
// Width specifies the width of the table.
Width int
// Header provides header data for the top of the table.
Header []string
// Data provides the row and column data for the table.
Data [][]string
// Spans provides the column span for each column.
Spans []int
// TextAligns specifies the text alignment for each cell.
TextAligns []string
// FontStyle contains the font configuration for table text.
FontStyle FontStyle
// HeaderBackgroundColor provides the background color for the header row.
HeaderBackgroundColor Color
// HeaderFontColor specifies the text color for header text.
HeaderFontColor Color
// RowBackgroundColors specifies the background colors for each row.
RowBackgroundColors []Color
// BackgroundColor specifies the general background color.
BackgroundColor Color
// CellModifier is an optional function to modify TableCell style or content before rendering.
CellModifier func(TableCell) TableCell
}
type tableSetting struct {
// headerColor specifies the color of the header.
headerColor Color
// headerFontColor specifies the color of header text.
headerFontColor Color
// fontColor specifies the color of table text.
fontColor Color
// rowColors specifies an array of colors for each row.
rowColors []Color
}
var tableLightThemeSetting = tableSetting{
headerColor: Color{R: 220, G: 220, B: 220, A: 255},
headerFontColor: Color{R: 80, G: 80, B: 80, A: 255},
fontColor: Color{R: 50, G: 50, B: 50, A: 255},
rowColors: []Color{
ColorWhite,
{R: 245, G: 245, B: 245, A: 255},
},
}
var tableDarkThemeSetting = tableSetting{
headerColor: Color{R: 38, G: 38, B: 42, A: 255},
headerFontColor: Color{R: 216, G: 217, B: 218, A: 255},
fontColor: Color{R: 216, G: 217, B: 218, A: 255},
rowColors: []Color{
{R: 24, G: 24, B: 28, A: 255},
{R: 38, G: 38, B: 42, A: 255},
},
}
type renderInfo struct {
width int
height int
headerHeight int
rowHeights []int
columnWidths []int
tableCells [][]TableCell
}
func (t *tableChart) render() (*renderInfo, error) {
info := renderInfo{}
p := t.p
if t.opt.Theme == nil {
t.opt.Theme = getPreferredTheme(p.theme)
}
opt := t.opt
if len(opt.Header) == 0 {
return nil, errors.New("header must not be empty")
}
fontStyle := opt.FontStyle
if fontStyle.FontSize <= 0 {
fontStyle.FontSize = defaultFontSize
}
if fontStyle.FontColor.IsZero() {
if opt.Theme.IsDark() {
fontStyle.FontColor = tableDarkThemeSetting.fontColor
} else {
fontStyle.FontColor = tableLightThemeSetting.fontColor
}
}
if fontStyle.Font == nil {
fontStyle.Font = GetDefaultFont()
}
if opt.HeaderFontColor.IsZero() {
if opt.Theme.IsDark() {
opt.HeaderFontColor = tableDarkThemeSetting.headerFontColor
} else {
opt.HeaderFontColor = tableLightThemeSetting.headerFontColor
}
}
spans := opt.Spans
if len(spans) != len(opt.Header) {
newSpans := make([]int, len(opt.Header))
for index := range opt.Header {
if index >= len(spans) {
newSpans[index] = 1
} else {
newSpans[index] = spans[index]
}
}
spans = newSpans
}
sum := chartdraw.SumInt(spans...)
values := autoDivideSpans(p.Width(), sum, spans)
columnWidths := make([]int, 0, len(values))
for index, v := range values {
if index == len(values)-1 {
break
}
columnWidths = append(columnWidths, values[index+1]-v)
}
info.columnWidths = columnWidths
var height int
headerFontStyle := FontStyle{
FontSize: fontStyle.FontSize,
FontColor: opt.HeaderFontColor,
Font: fontStyle.Font,
}
getTextAlign := func(index int) string {
if len(opt.TextAligns) <= index {
return ""
}
return opt.TextAligns[index]
}
// processing of the table cells
renderTableCells := func(
fontStyle FontStyle,
fillColor Color,
rowIndex int,
textList []string,
currentHeight int,
cellPadding Box,
) ([]TableCell, int) {
var cellMaxHeight int
paddingHeight := cellPadding.Top + cellPadding.Bottom
paddingWidth := cellPadding.Left + cellPadding.Right
columnCount := len(values) - 1
if len(textList) < columnCount {
columnCount = len(textList)
}
cells := make([]TableCell, columnCount)
for index, text := range textList[:columnCount] {
tc := TableCell{
Text: text,
Row: rowIndex,
Column: index,
FontStyle: fontStyle,
FillColor: fillColor,
}
if opt.CellModifier != nil {
tc = opt.CellModifier(tc)
// TODO - deprecate this behavior
// Update style values to capture any changes
fontStyle = tc.FontStyle
fillColor = tc.FillColor
}
cells[index] = tc
x := values[index]
y := currentHeight + cellPadding.Top
width := values[index+1] - x
x += cellPadding.Left
width -= paddingWidth
box := p.TextFit(text, x, y+int(fontStyle.FontSize), width, fontStyle, getTextAlign(index))
// calculate the highest height
if box.Height()+paddingHeight > cellMaxHeight {
cellMaxHeight = box.Height() + paddingHeight
}
}
return cells, cellMaxHeight
}
info.tableCells = make([][]TableCell, len(opt.Data)+1)
// processing of the table headers
headerCells, headerHeight := renderTableCells(headerFontStyle, ColorTransparent,
0, opt.Header, height, opt.Padding)
info.tableCells[0] = headerCells
height += headerHeight
info.headerHeight = headerHeight
// processing of the table contents
for index, textList := range opt.Data {
newCells, cellHeight := renderTableCells(fontStyle, ColorTransparent, index+1, textList, height, opt.Padding)
info.tableCells[index+1] = newCells
info.rowHeights = append(info.rowHeights, cellHeight)
height += cellHeight
}
info.width = p.Width()
info.height = height
return &info, nil
}
func (t *tableChart) renderWithInfo(info *renderInfo) (Box, error) {
p := t.p
if t.opt.Theme == nil {
t.opt.Theme = getPreferredTheme(p.theme)
}
opt := t.opt
if !opt.BackgroundColor.IsZero() {
p.drawBackground(opt.BackgroundColor)
}
if opt.HeaderBackgroundColor.IsZero() {
if opt.Theme.IsDark() {
opt.HeaderBackgroundColor = tableDarkThemeSetting.headerColor
} else {
opt.HeaderBackgroundColor = tableLightThemeSetting.headerColor
}
}
p.FilledRect(0, 0, info.width, info.headerHeight, opt.HeaderBackgroundColor, ColorTransparent, 0.0)
if opt.RowBackgroundColors == nil {
if opt.Theme.IsDark() {
opt.RowBackgroundColors = tableDarkThemeSetting.rowColors
} else {
opt.RowBackgroundColors = tableLightThemeSetting.rowColors
}
}
currentHeight := info.headerHeight
for index, h := range info.rowHeights {
color := opt.RowBackgroundColors[index%len(opt.RowBackgroundColors)]
child := p.Child(PainterPaddingOption(Box{
Top: currentHeight,
IsSet: true,
}))
child.FilledRect(0, 0, p.Width(), h, color, color, 0.0)
currentHeight += h
}
// adjust the background color according to the set table style
if opt.CellModifier != nil {
arr := make([][]string, 1, len(opt.Data)+1)
arr[0] = opt.Header
arr = append(arr, opt.Data...)
var top int
heights := make([]int, 1, len(info.rowHeights)+1)
heights[0] = info.headerHeight
heights = append(heights, info.rowHeights...)
// loop through all table cells to generate background color
for i := range arr {
var left int
for j, tc := range info.tableCells[i] {
if !tc.FillColor.IsZero() {
padding := opt.Padding
child := p.Child(PainterPaddingOption(Box{
Top: top + padding.Top,
Left: left + padding.Left,
IsSet: true,
}))
w := info.columnWidths[j] - padding.Left - padding.Right
h := heights[i] - padding.Top - padding.Bottom
child.FilledRect(0, 0, w, h, tc.FillColor, tc.FillColor, 0.0)
}
left += info.columnWidths[j]
}
top += heights[i]
}
}
if _, err := t.render(); err != nil {
return BoxZero, err
}
return Box{
Right: info.width,
Bottom: info.height,
IsSet: true,
}, nil
}
func (t *tableChart) Render() (Box, error) {
p := t.p
if !t.opt.BackgroundColor.IsZero() {
p.drawBackground(t.opt.BackgroundColor)
}
r := p.render
fn := chartdraw.PNG
if p.outputFormat == ChartOutputSVG {
fn = chartdraw.SVG
}
p.render = fn(p.Width(), 100)
info, err := t.render()
if err != nil {
return BoxZero, err
}
p.render = r
return t.renderWithInfo(info)
}