Debian Codegraph @

charts/rectangle.go

raw symbols SCIP
1 package charts
2
3 import (
4 "github.com/go-echarts/go-echarts/v2/opts"
5 )
6
7 type Overlaper interface {
8 asRectChart() *RectChart
9 }
10
11 // XYAxis represent the X and Y axis in the rectangular coordinates.
12 type XYAxis struct {
13 XAxisList []opts.XAxis `json:"xaxis"`
14 YAxisList []opts.YAxis `json:"yaxis"`
15 }
16
17 func (xy *XYAxis) initXYAxis() {
18 xy.XAxisList = append(xy.XAxisList, opts.XAxis{})
19 xy.YAxisList = append(xy.YAxisList, opts.YAxis{})
20 }
21
22 // ExtendXAxis adds new X axes.
23 func (xy *XYAxis) ExtendXAxis(xAxis ...opts.XAxis) {
24 xy.XAxisList = append(xy.XAxisList, xAxis...)
25 }
26
27 // ExtendYAxis adds new Y axes.
28 func (xy *XYAxis) ExtendYAxis(yAxis ...opts.YAxis) {
29 xy.YAxisList = append(xy.YAxisList, yAxis...)
30 }
31
32 // WithXAxisOpts sets the X axis.
33 func WithXAxisOpts(opt opts.XAxis, index ...int) GlobalOpts {
34 return func(bc *BaseConfiguration) {
35 if len(index) == 0 {
36 index = []int{0}
37 }
38 for i := 0; i < len(index); i++ {
39 bc.XYAxis.XAxisList[index[i]] = opt
40 }
41 }
42 }
43
44 // WithYAxisOpts sets the Y axis.
45 func WithYAxisOpts(opt opts.YAxis, index ...int) GlobalOpts {
46 return func(bc *BaseConfiguration) {
47 if len(index) == 0 {
48 index = []int{0}
49 }
50 for i := 0; i < len(index); i++ {
51 bc.XYAxis.YAxisList[index[i]] = opt
52 }
53 }
54 }
55
56 // RectConfiguration contains options for the rectangular coordinates.
57 type RectConfiguration struct {
58 BaseConfiguration
59 }
60
61 func (rect *RectConfiguration) setRectGlobalOptions(options ...GlobalOpts) {
62 rect.BaseConfiguration.setBaseGlobalOptions(options...)
63 }
64
65 // RectChart is a chart in RectChart coordinate.
66 type RectChart struct {
67 RectConfiguration
68
69 xAxisData interface{}
70 }
71
72 func (rc *RectChart) asRectChart() *RectChart {
73 return rc
74 }
75
76 // The default RectAggregator for the series data merge
77 func (rc *RectChart) overlap() RectAggregator {
78 return func(this *RectChart, that *RectChart) *RectChart {
79 this.MultiSeries = append(this.MultiSeries, that.MultiSeries...)
80 return this
81 }
82 }
83
84 // SetGlobalOptions sets options for the RectChart instance.
85 func (rc *RectChart) SetGlobalOptions(options ...GlobalOpts) *RectChart {
86 rc.RectConfiguration.setRectGlobalOptions(options...)
87 return rc
88 }
89
90 // Overlap composes multiple charts' Series data into one single canvas.
91 // It is only suited for some of the charts which are in rectangular coordinate.
92 // Supported charts: Bar/BoxPlot/Line/Scatter/EffectScatter/Kline/HeatMap/Custom
93 func (rc *RectChart) Overlap(a ...Overlaper) {
94 rc.Aggregate(rc.overlap(), a...)
95 }
96
97 // RectAggregator is a binary operator for the RectChart merge function
98 type RectAggregator func(this *RectChart, that *RectChart) *RectChart
99
100 // Aggregate aggregation multiple rect charts into one single canvas.
101 // An aggregator will merge all charts in orders into the final one.
102 // It is only suited for some of the charts which are in rectangular coordinate.
103 func (rc *RectChart) Aggregate(aggregator RectAggregator, charts ...Overlaper) {
104 identity := rc
105 for _, o := range charts {
106 c := o.asRectChart()
107 identity = aggregator(identity, c)
108 }
109 }
110
111 // Validate validates the given configuration.
112 func (rc *RectChart) Validate() {
113 // Make sure that the data of X axis won't be cleaned for XAxisOpts
114 rc.XAxisList[0].Data = rc.xAxisData
115 // Make sure that the labels of Y axis show correctly
116 for i := 0; i < len(rc.YAxisList); i++ {
117 rc.YAxisList[i].AxisLabel.Show = opts.Bool(true)
118 }
119 rc.Assets.Validate(rc.AssetsHost)
120 }