feat: add axis draw function
This commit is contained in:
parent
c5d95eae0a
commit
4ac419fce9
3 changed files with 611 additions and 10 deletions
81
util.go
Normal file
81
util.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
// MIT License
|
||||
|
||||
// Copyright (c) 2022 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 "github.com/wcharczuk/go-chart/v2"
|
||||
|
||||
func getDefaultInt(value, defaultValue int) int {
|
||||
if value == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func autoDivide(max, size int) []int {
|
||||
unit := max / size
|
||||
|
||||
rest := max - unit*size
|
||||
values := make([]int, size+1)
|
||||
value := 0
|
||||
for i := 0; i < size; i++ {
|
||||
values[i] = value
|
||||
if i < rest {
|
||||
value++
|
||||
}
|
||||
value += unit
|
||||
}
|
||||
values[size] = max
|
||||
return values
|
||||
}
|
||||
func maxInt(values ...int) int {
|
||||
result := 0
|
||||
for _, v := range values {
|
||||
if v > result {
|
||||
result = v
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func measureTextMaxWidthHeight(textList []string, r chart.Renderer) (int, int) {
|
||||
maxWidth := 0
|
||||
maxHeight := 0
|
||||
for _, text := range textList {
|
||||
box := r.MeasureText(text)
|
||||
maxWidth = maxInt(maxWidth, box.Width())
|
||||
maxHeight = maxInt(maxHeight, box.Height())
|
||||
}
|
||||
return maxWidth, maxHeight
|
||||
}
|
||||
|
||||
func reverseStringSlice(stringList []string) {
|
||||
for i, j := 0, len(stringList)-1; i < j; i, j = i+1, j-1 {
|
||||
stringList[i], stringList[j] = stringList[j], stringList[i]
|
||||
}
|
||||
}
|
||||
|
||||
func reverseIntSlice(intList []int) {
|
||||
for i, j := 0, len(intList)-1; i < j; i, j = i+1, j-1 {
|
||||
intList[i], intList[j] = intList[j], intList[i]
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue