forked from vicanso/go-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axis.go
208 lines (185 loc) · 4.98 KB
/
axis.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
// MIT License
// Copyright (c) 2021 Tree Xie
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package charts
import (
"math"
"github.com/dustin/go-humanize"
"github.com/wcharczuk/go-chart/v2"
)
type (
// AxisData string
XAxis struct {
// data value of axis
Data []string
// number of segments
SplitNumber int
}
)
type YAxisOption struct {
// formater of axis
Formater chart.ValueFormatter
// disabled axis
Disabled bool
// min value of axis
Min *float64
// max value of axis
Max *float64
}
const axisStrokeWidth = 1
func maxInt(values ...int) int {
result := 0
for _, v := range values {
if v > result {
result = v
}
}
return result
}
// GetXAxisAndValues returns x axis by theme, and the values of axis.
func GetXAxisAndValues(xAxis XAxis, tickPosition chart.TickPosition, theme string) (chart.XAxis, []float64) {
data := xAxis.Data
originalSize := len(data)
// 如果居中,则需要多添加一个值
if tickPosition == chart.TickPositionBetweenTicks {
data = append([]string{
"",
}, data...)
}
size := len(data)
xValues := make([]float64, size)
ticks := make([]chart.Tick, 0)
// tick width
maxTicks := maxInt(xAxis.SplitNumber, 10)
// 计息最多每个unit至少放多个
minUnitSize := originalSize / maxTicks
if originalSize%maxTicks != 0 {
minUnitSize++
}
unitSize := minUnitSize
// 尽可能选择一格展示更多的块
for i := minUnitSize; i < 2*minUnitSize; i++ {
if originalSize%i == 0 {
unitSize = i
}
}
for index, key := range data {
f := float64(index)
xValues[index] = f
if index%unitSize == 0 || index == size-1 {
ticks = append(ticks, chart.Tick{
Value: f,
Label: key,
})
}
}
return chart.XAxis{
Ticks: ticks,
TickPosition: tickPosition,
Style: chart.Style{
FontColor: getAxisColor(theme),
StrokeColor: getAxisColor(theme),
StrokeWidth: axisStrokeWidth,
},
}, xValues
}
func defaultFloatFormater(v interface{}) string {
value, ok := v.(float64)
if !ok {
return ""
}
// 大于10的则直接取整展示
if value >= 10 {
return humanize.CommafWithDigits(value, 0)
}
return humanize.CommafWithDigits(value, 2)
}
func newYContinuousRange(option *YAxisOption) *YContinuousRange {
m := YContinuousRange{}
m.Min = -math.MaxFloat64
m.Max = math.MaxFloat64
if option != nil {
if option.Min != nil {
m.Min = *option.Min
}
if option.Max != nil {
m.Max = *option.Max
}
}
return &m
}
// GetSecondaryYAxis returns the secondary y axis by theme
func GetSecondaryYAxis(theme string, option *YAxisOption) chart.YAxis {
strokeColor := getGridColor(theme)
yAxis := chart.YAxis{
Range: newYContinuousRange(option),
ValueFormatter: defaultFloatFormater,
AxisType: chart.YAxisSecondary,
GridMajorStyle: chart.Style{
StrokeColor: strokeColor,
StrokeWidth: axisStrokeWidth,
},
GridMinorStyle: chart.Style{
StrokeColor: strokeColor,
StrokeWidth: axisStrokeWidth,
},
Style: chart.Style{
FontColor: getAxisColor(theme),
// alpha 0,隐藏
StrokeColor: hiddenColor,
StrokeWidth: axisStrokeWidth,
},
}
setYAxisOption(&yAxis, option)
return yAxis
}
func setYAxisOption(yAxis *chart.YAxis, option *YAxisOption) {
if option == nil {
return
}
if option.Formater != nil {
yAxis.ValueFormatter = option.Formater
}
}
// GetYAxis returns the primary y axis by theme
func GetYAxis(theme string, option *YAxisOption) chart.YAxis {
disabled := false
if option != nil {
disabled = option.Disabled
}
hidden := chart.Hidden()
yAxis := chart.YAxis{
Range: newYContinuousRange(option),
ValueFormatter: defaultFloatFormater,
AxisType: chart.YAxisPrimary,
GridMajorStyle: hidden,
GridMinorStyle: hidden,
Style: chart.Style{
FontColor: getAxisColor(theme),
// alpha 0,隐藏
StrokeColor: hiddenColor,
StrokeWidth: axisStrokeWidth,
},
}
// 如果禁用,则默认为隐藏,并设置range
if disabled {
yAxis.Range = &HiddenRange{}
yAxis.Style.Hidden = true
}
setYAxisOption(&yAxis, option)
return yAxis
}