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 ( import (
"errors" "errors"
"math"
"sort" "sort"
"github.com/wcharczuk/go-chart/v2" "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 { type Renderer interface {
Render() (Box, error) Render() (Box, error)
} }

View file

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

View file

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

View file

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

View file

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