feat: support null value for line chart

This commit is contained in:
vicanso 2022-09-15 20:09:00 +08:00
parent bb9af986be
commit 50605907c7
5 changed files with 32 additions and 2 deletions

View file

@ -24,6 +24,7 @@ package charts
import (
"errors"
"math"
"sort"
"github.com/wcharczuk/go-chart/v2"
@ -51,6 +52,18 @@ func SetDefaultHeight(height int) {
}
}
var nullValue = math.MaxFloat64
// SetNullValue sets the null value, default is MaxFloat64
func SetNullValue(v float64) {
nullValue = v
}
// GetNullValue gets the null value
func GetNullValue() float64 {
return nullValue
}
type Renderer interface {
Render() (Box, error)
}

View file

@ -29,7 +29,8 @@ func main() {
120,
132,
101,
134,
// 134,
charts.GetNullValue(),
90,
230,
210,

View file

@ -23,6 +23,8 @@
package charts
import (
"math"
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2/drawing"
)
@ -115,6 +117,9 @@ func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) (
points := make([]Point, 0)
for i, item := range series.Data {
h := yRange.getRestHeight(item.Value)
if item.Value == nullValue {
h = math.MaxInt
}
p := Point{
X: xValues[i],
Y: h,

View file

@ -438,11 +438,18 @@ func (p *Painter) MeasureTextMaxWidthHeight(textList []string) (int, int) {
}
func (p *Painter) LineStroke(points []Point) *Painter {
shouldMoveTo := false
for index, point := range points {
x := point.X
y := point.Y
if index == 0 {
if y == math.MaxInt {
p.Stroke()
shouldMoveTo = true
continue
}
if shouldMoveTo || index == 0 {
p.MoveTo(x, y)
shouldMoveTo = false
} else {
p.LineTo(x, y)
}

View file

@ -165,6 +165,10 @@ func (sl SeriesList) GetMaxMin(axisIndex int) (float64, float64) {
continue
}
for _, item := range series.Data {
// 如果为空值,忽略
if item.Value == nullValue {
continue
}
if item.Value > max {
max = item.Value
}