-
Notifications
You must be signed in to change notification settings - Fork 1
/
chart_option.go
413 lines (371 loc) · 10.9 KB
/
chart_option.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package charts
import (
"fmt"
"sort"
"github.com/golang/freetype/truetype"
)
type ChartOption struct {
// OutputFormat specifies the output type of chart, "svg" or "png", default value is "png"
OutputFormat string
// Width is the width of chart, default width is 600.
Width int
// Height is the height of chart, default height is 400
Height int
// Theme specifies the colors used for the chart. Built in themes can be loaded using GetTheme with
// "light", "dark", "vivid-light", "vivid-dark", "ant" or "grafana".
Theme ColorPalette
// Padding specifies the padding for chart, default padding is [20, 10, 10, 10]
Padding Box
// XAxis are options for the x-axis.
XAxis XAxisOption
// YAxis are options for the y-axis (at most two).
YAxis []YAxisOption
// Title are options for rendering the title.
Title TitleOption
// Legend are options for the data legend.
Legend LegendOption
// Font is the font to use for rendering the chart.
Font *truetype.Font
// Box specifies the canvas box for the chart.
Box Box
// SeriesList provides the data series.
SeriesList SeriesList
// RadarIndicators are radar indicator list for radar charts
RadarIndicators []RadarIndicator
// SymbolShow set this to *false (using False()) to hide symbols.
SymbolShow *bool
// LineStrokeWidth is the stroke width for line charts.
LineStrokeWidth float64
// FillArea set to true to fill the area under the line in line charts
FillArea bool
// FillOpacity is the opacity (alpha) of the area fill in line charts.
FillOpacity uint8
// BarWidth is the width of the bars for bar charts.
BarWidth int
// BarHeight is the height of the bars for horizontal bar charts.
BarHeight int
// Children are child charts to render together.
Children []ChartOption
parent *Painter
// ValueFormatter to format numeric values into labels.
ValueFormatter ValueFormatter
}
// OptionFunc option function
type OptionFunc func(opt *ChartOption)
// SVGOutputOption set svg type of chart's output.
func SVGOutputOption() OptionFunc {
return OutputFormatOptionFunc(ChartOutputSVG)
}
// PNGOutputOption set png type of chart's output.
func PNGOutputOption() OptionFunc {
return OutputFormatOptionFunc(ChartOutputPNG)
}
// OutputFormatOptionFunc set type of chart's output.
func OutputFormatOptionFunc(t string) OptionFunc {
return func(opt *ChartOption) {
opt.OutputFormat = t
}
}
// FontOptionFunc set font of chart.
func FontOptionFunc(font *truetype.Font) OptionFunc {
return func(opt *ChartOption) {
opt.Font = font
}
}
// ThemeNameOptionFunc set them of chart by name.
func ThemeNameOptionFunc(theme string) OptionFunc {
return func(opt *ChartOption) {
opt.Theme = GetTheme(theme)
}
}
// ThemeOptionFunc set them of chart
func ThemeOptionFunc(theme ColorPalette) OptionFunc {
return func(opt *ChartOption) {
opt.Theme = theme
}
}
// TitleOptionFunc set title of chart
func TitleOptionFunc(title TitleOption) OptionFunc {
return func(opt *ChartOption) {
opt.Title = title
}
}
// TitleTextOptionFunc set title text of chart
func TitleTextOptionFunc(text string, subtext ...string) OptionFunc {
return func(opt *ChartOption) {
opt.Title.Text = text
if len(subtext) != 0 {
opt.Title.Subtext = subtext[0]
}
}
}
// LegendOptionFunc set legend of chart
func LegendOptionFunc(legend LegendOption) OptionFunc {
return func(opt *ChartOption) {
opt.Legend = legend
}
}
// LegendLabelsOptionFunc set legend labels of chart
func LegendLabelsOptionFunc(labels []string, left ...string) OptionFunc {
return func(opt *ChartOption) {
opt.Legend = NewLegendOption(labels, left...)
}
}
// XAxisOptionFunc set x-axis of chart
func XAxisOptionFunc(xAxisOption XAxisOption) OptionFunc {
return func(opt *ChartOption) {
opt.XAxis = xAxisOption
}
}
// XAxisDataOptionFunc set x-axis data of chart
func XAxisDataOptionFunc(data []string, boundaryGap ...*bool) OptionFunc {
return func(opt *ChartOption) {
opt.XAxis = NewXAxisOption(data, boundaryGap...)
}
}
// YAxisOptionFunc set y-axis of chart, supports up to two y-axis.
func YAxisOptionFunc(yAxisOption ...YAxisOption) OptionFunc {
return func(opt *ChartOption) {
opt.YAxis = yAxisOption
}
}
// YAxisDataOptionFunc set y-axis data of chart
func YAxisDataOptionFunc(data []string) OptionFunc {
return func(opt *ChartOption) {
opt.YAxis = NewYAxisOptions(data)
}
}
// WidthOptionFunc set width of chart
func WidthOptionFunc(width int) OptionFunc {
return func(opt *ChartOption) {
opt.Width = width
}
}
// HeightOptionFunc set height of chart
func HeightOptionFunc(height int) OptionFunc {
return func(opt *ChartOption) {
opt.Height = height
}
}
// PaddingOptionFunc set padding of chart
func PaddingOptionFunc(padding Box) OptionFunc {
return func(opt *ChartOption) {
opt.Padding = padding
}
}
// BoxOptionFunc set box of chart
func BoxOptionFunc(box Box) OptionFunc {
return func(opt *ChartOption) {
opt.Box = box
}
}
// PieSeriesShowLabel set pie series show label
func PieSeriesShowLabel() OptionFunc {
return func(opt *ChartOption) {
for index := range opt.SeriesList {
opt.SeriesList[index].Label.Show = true
}
}
}
// ChildOptionFunc add child chart
func ChildOptionFunc(child ...ChartOption) OptionFunc {
return func(opt *ChartOption) {
if opt.Children == nil {
opt.Children = make([]ChartOption, 0)
}
opt.Children = append(opt.Children, child...)
}
}
// RadarIndicatorOptionFunc set radar indicator of chart
func RadarIndicatorOptionFunc(names []string, values []float64) OptionFunc {
return func(opt *ChartOption) {
opt.RadarIndicators = NewRadarIndicators(names, values)
}
}
// MarkLineOptionFunc set mark line for series of chart
func MarkLineOptionFunc(seriesIndex int, markLineTypes ...string) OptionFunc {
return func(opt *ChartOption) {
if len(opt.SeriesList) <= seriesIndex {
return
}
opt.SeriesList[seriesIndex].MarkLine = NewMarkLine(markLineTypes...)
}
}
// MarkPointOptionFunc set mark point for series of chart
func MarkPointOptionFunc(seriesIndex int, markPointTypes ...string) OptionFunc {
return func(opt *ChartOption) {
if len(opt.SeriesList) <= seriesIndex {
return
}
opt.SeriesList[seriesIndex].MarkPoint = NewMarkPoint(markPointTypes...)
}
}
func (o *ChartOption) fillDefault() error {
o.Width = getDefaultInt(o.Width, defaultChartWidth)
o.Height = getDefaultInt(o.Height, defaultChartHeight)
yaxisCount := 1
for _, series := range o.SeriesList {
if series.YAxisIndex == 1 {
yaxisCount++
break
} else if series.YAxisIndex > 1 {
return fmt.Errorf("series '%s' specified invalid y-axis index: %v", series.Name, series.YAxisIndex)
}
}
if len(o.YAxis) < yaxisCount {
yAxisOptions := make([]YAxisOption, yaxisCount)
copy(yAxisOptions, o.YAxis)
o.YAxis = yAxisOptions
}
// TODO - this is a hack, we need to update the yaxis based on the markpoint state
// TODO - but can't do this earlier due to needing the axis initialized
// TODO - we should reconsider the API for configuration
for _, sl := range o.SeriesList {
if len(sl.MarkPoint.Data) > 0 { // if graph has markpoint
// adjust padding scale to give space for mark point (if not specified by user)
for i := range o.YAxis {
if o.YAxis[i].RangeValuePaddingScale == nil {
o.YAxis[i].RangeValuePaddingScale = FloatPointer(2.5)
}
}
break
}
}
if o.Font == nil {
o.Font = GetDefaultFont()
}
if o.Theme == nil {
o.Theme = GetDefaultTheme()
}
fillThemeDefaults(o.Theme, &o.Title, &o.Legend, &o.XAxis)
if o.Padding.IsZero() {
o.Padding = Box{
Top: 20,
Right: 20,
Bottom: 20,
Left: 20,
}
}
// association between legend and series name
if len(o.Legend.Data) == 0 {
o.Legend.Data = o.SeriesList.Names()
} else {
seriesCount := len(o.SeriesList)
for index, name := range o.Legend.Data {
if index < seriesCount && len(o.SeriesList[index].Name) == 0 {
o.SeriesList[index].Name = name
}
}
nameIndexDict := map[string]int{}
for index, name := range o.Legend.Data {
nameIndexDict[name] = index
}
// ensure order of series is consistent with legend
sort.Slice(o.SeriesList, func(i, j int) bool {
return nameIndexDict[o.SeriesList[i].Name] < nameIndexDict[o.SeriesList[j].Name]
})
}
return nil
}
func fillThemeDefaults(defaultTheme ColorPalette, title *TitleOption, legend *LegendOption, xaxis *XAxisOption) {
if title.Theme == nil {
title.Theme = defaultTheme
}
if legend.Theme == nil {
legend.Theme = defaultTheme
}
if xaxis.Theme == nil {
xaxis.Theme = defaultTheme
}
}
// LineRender line chart render
func LineRender(values [][]float64, opts ...OptionFunc) (*Painter, error) {
return Render(ChartOption{
SeriesList: NewSeriesListDataFromValues(values, ChartTypeLine),
}, opts...)
}
// BarRender bar chart render
func BarRender(values [][]float64, opts ...OptionFunc) (*Painter, error) {
return Render(ChartOption{
SeriesList: NewSeriesListDataFromValues(values, ChartTypeBar),
}, opts...)
}
// HorizontalBarRender horizontal bar chart render
func HorizontalBarRender(values [][]float64, opts ...OptionFunc) (*Painter, error) {
return Render(ChartOption{
SeriesList: NewSeriesListDataFromValues(values, ChartTypeHorizontalBar),
}, opts...)
}
// PieRender pie chart render
func PieRender(values []float64, opts ...OptionFunc) (*Painter, error) {
return Render(ChartOption{
SeriesList: NewPieSeriesList(values),
}, opts...)
}
// RadarRender radar chart render
func RadarRender(values [][]float64, opts ...OptionFunc) (*Painter, error) {
return Render(ChartOption{
SeriesList: NewSeriesListDataFromValues(values, ChartTypeRadar),
}, opts...)
}
// FunnelRender funnel chart render
func FunnelRender(values []float64, opts ...OptionFunc) (*Painter, error) {
return Render(ChartOption{
SeriesList: NewFunnelSeriesList(values),
}, opts...)
}
// TableRender table chart render
func TableRender(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 TableOptionRender(opt)
}
// TableOptionRender table render with option
func TableOptionRender(opt TableChartOption) (*Painter, error) {
if opt.OutputFormat == "" {
opt.OutputFormat = chartDefaultOutputFormat
}
if opt.Width <= 0 {
opt.Width = defaultChartWidth
}
p, err := NewPainter(PainterOptions{
OutputFormat: opt.OutputFormat,
Width: opt.Width,
Height: 100, // is only used to calculate the height of the table
Font: opt.Font,
})
if err != nil {
return nil, err
}
info, err := NewTableChart(p, opt).render()
if err != nil {
return nil, err
}
p, err = NewPainter(PainterOptions{
OutputFormat: opt.OutputFormat,
Width: info.Width,
Height: info.Height,
Font: opt.Font,
})
if err != nil {
return nil, err
}
if _, err = NewTableChart(p, opt).renderWithInfo(info); err != nil {
return nil, err
}
return p, nil
}