-
Notifications
You must be signed in to change notification settings - Fork 547
/
chart3d.go
183 lines (164 loc) · 6.35 KB
/
chart3d.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
package charts
import (
"io"
)
// Chart3D is a chart in 3D coordinate.
type Chart3D struct {
BaseOpts
Series
XAxis3D XAxis3DOpts
YAxis3D YAxis3DOpts
ZAxis3D ZAxis3DOpts
Grid3D Grid3DOpts
xData, yData interface{}
}
func (c *Chart3D) initChart3D() {
c.JSAssets.Add("echarts-gl.min.js")
c.Has3DAxis = true
}
// SetGlobalOptions sets options for the Chart3D instance.
func (c *Chart3D) SetGlobalOptions(options ...GlobalOptser) *Chart3D {
c.BaseOpts.setBaseGlobalOptions(options...)
c.setChart3DGlobalOptions(options...)
return c
}
func (c *Chart3D) setChart3DGlobalOptions(options ...GlobalOptser) {
for i := 0; i < len(options); i++ {
switch option := options[i].(type) {
case XAxis3DOpts:
c.XAxis3D = option
case YAxis3DOpts:
c.YAxis3D = option
case ZAxis3DOpts:
c.ZAxis3D = option
case Grid3DOpts:
c.Grid3D = option
}
}
}
func (c *Chart3D) validateOpts() {
// 确保 XY 轴数据项不会被抹除
if c.XAxis3D.Data == nil {
c.XAxis3D.Data = c.xData
}
if c.YAxis3D.Data == nil {
c.YAxis3D.Data = c.yData
}
c.validateAssets(c.AssetsHost)
}
func (c *Chart3D) addZAxis(chartType, name string, zAxis interface{}, options ...SeriesOptser) {
series := singleSeries{
Name: name,
Type: chartType,
Data: zAxis,
CoordSystem: ChartType.Cartesian3D,
}
series.setSingleSeriesOpts(options...)
c.Series = append(c.Series, series)
c.setColor(options...)
}
// Render renders the chart and writes the output to given writers.
func (c *Chart3D) Render(w ...io.Writer) error {
c.insertSeriesColors(c.appendColor)
c.validateOpts()
return renderToWriter(c, "chart", []string{}, w...)
}
// Grid3DOpts contains options for the 3D coordinate.
type Grid3DOpts struct {
// 是否显示三维笛卡尔坐标系
Show bool `json:"show,omitempty"`
// 三维笛卡尔坐标系组件在三维场景中的宽度
// 默认 100
BoxWidth float32 `json:"boxWidth,omitempty"`
// 三维笛卡尔坐标系组件在三维场景中的高度
// 默认 100
BoxHeight float32 `json:"boxHeight,omitempty"`
// 三维笛卡尔坐标系组件在三维场景中的深度
// 默认 100
BoxDepth float32 `json:"boxDepth,omitempty"`
// 用于鼠标的旋转,缩放等视角控制
ViewControl ViewControlOpts `json:"viewControl,omitempty"`
}
func (Grid3DOpts) MarkGlobal() {}
// ViewControlOpts contains options for view controlling.
type ViewControlOpts struct {
// 是否开启视角绕物体的自动旋转查看
AutoRotate bool `json:"autoRotate,omitempty"`
// 物体自转的速度。单位为角度 / 秒,默认为 10 ,也就是 36 秒转一圈
AutoRotateSpeed float32 `json:"autoRotateSpeed,omitempty"`
}
// XAxis3DOpts contains options for X axis in the 3D coordinate.
type XAxis3DOpts struct {
// 是否显示 3D X 轴
Show bool `json:"show,omitempty"`
// X 坐标轴名称
Name bool `json:"name,omitempty"`
// X 坐标轴使用的 grid3D 组件的索引。默认使用第一个 grid3D 组件
Grid3DIndex int `json:"grid3DIndex,omitempty"`
// X 坐标轴类型,可选:
// "value" 数值轴,适用于连续数据。
// "category" 类目轴,适用于离散的类目数据,为该类型时必须通过 data 设置类目数据。
// "log" 对数轴。适用于对数数据。
Type string `json:"type,omitempty"`
// X 坐标轴刻度最小值。
// 可以设置成特殊值 "dataMin",此时取数据在该轴上的最小值作为最小刻度。
// 不设置时会自动计算最小值保证坐标轴刻度的均匀分布
Min interface{} `json:"min,omitempty"`
// X 坐标轴刻度最大值。
// 可以设置成特殊值 "dataMax",此时取数据在该轴上的最大值作为最大刻度。
// 不设置时会自动计算最大值保证坐标轴刻度的均匀分布
Max interface{} `json:"max,omitempty"`
// 类目数据,在类目轴(type: 'category')中有效
Data interface{} `json:"data,omitempty"`
}
func (XAxis3DOpts) MarkGlobal() {}
// YAxis3DOpts contains options for Y axis in the 3D coordinate.
type YAxis3DOpts struct {
// 是否显示 3D Y 轴
Show bool `json:"show,omitempty"`
// Y 坐标轴名称
Name bool `json:"name,omitempty"`
// Y 坐标轴使用的 grid3D 组件的索引。默认使用第一个 grid3D 组件
Grid3DIndex int `json:"grid3DIndex,omitempty"`
// Y 坐标轴类型,可选:
// "value" 数值轴,适用于连续数据。
// "category" 类目轴,适用于离散的类目数据,为该类型时必须通过 data 设置类目数据。
// "log" 对数轴。适用于对数数据。
Type string `json:"type,omitempty"`
// Y 坐标轴刻度最小值。
// 可以设置成特殊值 "dataMin",此时取数据在该轴上的最小值作为最小刻度。
// 不设置时会自动计算最小值保证坐标轴刻度的均匀分布
Min interface{} `json:"min,omitempty"`
// Y 坐标轴刻度最大值。
// 可以设置成特殊值 "dataMax",此时取数据在该轴上的最大值作为最大刻度。
// 不设置时会自动计算最大值保证坐标轴刻度的均匀分布
Max interface{} `json:"max,omitempty"`
// 类目数据,在类目轴(type: 'category')中有效
Data interface{} `json:"data,omitempty"`
}
func (YAxis3DOpts) MarkGlobal() {}
// ZAxis3DOpts contains options for Z axis in the 3D coordinate.
type ZAxis3DOpts struct {
// 是否显示 3D Z 轴
Show bool `json:"show,omitempty"`
// Z 坐标轴名称
Name bool `json:"name,omitempty"`
// Z 坐标轴使用的 grid3D 组件的索引。默认使用第一个 grid3D 组件
Grid3DIndex int `json:"grid3DIndex,omitempty"`
// Z 坐标轴类型,可选:
// "value" 数值轴,适用于连续数据。
// "category" 类目轴,适用于离散的类目数据,为该类型时必须通过 data 设置类目数据。
// "log" 对数轴。适用于对数数据。
Type string `json:"type,omitempty"`
// Z 坐标轴刻度最小值。
// 可以设置成特殊值 "dataMin",此时取数据在该轴上的最小值作为最小刻度。
// 不设置时会自动计算最小值保证坐标轴刻度的均匀分布
Min interface{} `json:"min,omitempty"`
// Z 坐标轴刻度最大值。
// 可以设置成特殊值 "dataMax",此时取数据在该轴上的最大值作为最大刻度。
// 不设置时会自动计算最大值保证坐标轴刻度的均匀分布
Max interface{} `json:"max,omitempty"`
// 类目数据,在类目轴(type: 'category')中有效
Data interface{} `json:"data,omitempty"`
}
func (ZAxis3DOpts) MarkGlobal() {}