bugfixes with mixed ranges.
This commit is contained in:
parent
9aa2a45a39
commit
e781e7b4e1
4 changed files with 59 additions and 59 deletions
|
@ -1,5 +1,7 @@
|
|||
package chart
|
||||
|
||||
import "math"
|
||||
|
||||
// Annotation is a label on the chart.
|
||||
type Annotation struct {
|
||||
X, Y float64
|
||||
|
@ -32,10 +34,10 @@ func (as AnnotationSeries) GetYAxis() YAxisType {
|
|||
// Measure returns a bounds box of the series.
|
||||
func (as AnnotationSeries) Measure(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) Box {
|
||||
box := Box{
|
||||
Top: canvasBox.Bottom,
|
||||
Left: canvasBox.Right,
|
||||
Right: canvasBox.Left,
|
||||
Bottom: canvasBox.Top,
|
||||
Top: math.MaxInt32,
|
||||
Left: math.MaxInt32,
|
||||
Right: 0,
|
||||
Bottom: 0,
|
||||
}
|
||||
if as.Style.Show {
|
||||
style := as.Style.WithDefaultsFrom(Style{
|
||||
|
@ -49,18 +51,18 @@ func (as AnnotationSeries) Measure(r Renderer, canvasBox Box, xrange, yrange Ran
|
|||
for _, a := range as.Annotations {
|
||||
lx := canvasBox.Right - xrange.Translate(a.X)
|
||||
ly := yrange.Translate(a.Y) + canvasBox.Top
|
||||
aBox := MeasureAnnotation(r, canvasBox, xrange, yrange, style, lx, ly, a.Label)
|
||||
if aBox.Top < box.Top {
|
||||
box.Top = aBox.Top
|
||||
ab := MeasureAnnotation(r, canvasBox, xrange, yrange, style, lx, ly, a.Label)
|
||||
if ab.Top < box.Top {
|
||||
box.Top = ab.Top
|
||||
}
|
||||
if aBox.Left < box.Left {
|
||||
box.Left = aBox.Left
|
||||
if ab.Left < box.Left {
|
||||
box.Left = ab.Left
|
||||
}
|
||||
if aBox.Right > box.Right {
|
||||
box.Right = aBox.Right
|
||||
if ab.Right > box.Right {
|
||||
box.Right = ab.Right
|
||||
}
|
||||
if aBox.Bottom > box.Bottom {
|
||||
box.Bottom = aBox.Bottom
|
||||
if ab.Bottom > box.Bottom {
|
||||
box.Bottom = ab.Bottom
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
41
chart.go
41
chart.go
|
@ -3,6 +3,7 @@ package chart
|
|||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"math"
|
||||
|
||||
"github.com/golang/freetype/truetype"
|
||||
"github.com/wcharczuk/go-chart/drawing"
|
||||
|
@ -101,33 +102,30 @@ func (c Chart) Render(rp RendererProvider, w io.Writer) error {
|
|||
}
|
||||
|
||||
func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
|
||||
//iterate over each series, pull out the min/max for x,y
|
||||
var didSetFirstValues bool
|
||||
|
||||
var globalMinX, globalMaxX float64
|
||||
var globalMinY, globalMaxY float64
|
||||
var globalMinYA, globalMaxYA float64
|
||||
var globalMinX, globalMaxX float64 = math.MaxFloat64, 0
|
||||
var globalMinY, globalMaxY float64 = math.MaxFloat64, 0
|
||||
var globalMinYA, globalMaxYA float64 = math.MaxFloat64, 0
|
||||
|
||||
for _, s := range c.Series {
|
||||
seriesAxis := s.GetYAxis()
|
||||
if vp, isValueProvider := s.(ValueProvider); isValueProvider {
|
||||
seriesLength := vp.Len()
|
||||
for index := 0; index < seriesLength; index++ {
|
||||
vx, vy := vp.GetValue(index)
|
||||
if didSetFirstValues {
|
||||
if globalMinX > vx {
|
||||
globalMinX = vx
|
||||
}
|
||||
if globalMaxX < vx {
|
||||
globalMaxX = vx
|
||||
}
|
||||
if s.GetYAxis() == YAxisPrimary {
|
||||
if seriesAxis == YAxisPrimary {
|
||||
if globalMinY > vy {
|
||||
globalMinY = vy
|
||||
}
|
||||
if globalMaxY < vy {
|
||||
globalMaxY = vy
|
||||
}
|
||||
} else if s.GetYAxis() == YAxisSecondary {
|
||||
} else if seriesAxis == YAxisSecondary {
|
||||
if globalMinYA > vy {
|
||||
globalMinYA = vy
|
||||
}
|
||||
|
@ -135,15 +133,6 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
|
|||
globalMaxYA = vy
|
||||
}
|
||||
}
|
||||
} else {
|
||||
globalMinX, globalMaxX = vx, vx
|
||||
if s.GetYAxis() == YAxisPrimary {
|
||||
globalMinY, globalMaxY = vy, vy
|
||||
} else if s.GetYAxis() == YAxisSecondary {
|
||||
globalMinYA, globalMaxYA = vy, vy
|
||||
}
|
||||
didSetFirstValues = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -171,6 +160,7 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
|
|||
yrangeAlt.Min = globalMinYA
|
||||
yrangeAlt.Max = globalMaxYA
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -317,7 +307,7 @@ func (c Chart) setRangeDomains(canvasBox Box, xrange, yrange, yrangeAlt Range) (
|
|||
|
||||
func (c Chart) hasAnnotationSeries() bool {
|
||||
for _, s := range c.Series {
|
||||
if as, isAnnotationSeries:= s.(AnnotationSeries); isAnnotationSeries {
|
||||
if as, isAnnotationSeries := s.(AnnotationSeries); isAnnotationSeries {
|
||||
if as.Style.Show {
|
||||
return true
|
||||
}
|
||||
|
@ -327,16 +317,16 @@ func (c Chart) hasAnnotationSeries() bool {
|
|||
}
|
||||
|
||||
func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, yra Range, xf, yf, yfa ValueFormatter) Box {
|
||||
annotationMinX, annotationMaxX, annotationMinY, annotationMaxY := canvasBox.Right, canvasBox.Left, canvasBox.Bottom, canvasBox.Top
|
||||
annotationMinX, annotationMaxX, annotationMinY, annotationMaxY := math.MaxInt32, 0, math.MaxInt32, 0
|
||||
for seriesIndex, s := range c.Series {
|
||||
if as, isAnnotationSeries:= s.(AnnotationSeries); isAnnotationSeries {
|
||||
if as, isAnnotationSeries := s.(AnnotationSeries); isAnnotationSeries {
|
||||
if as.Style.Show {
|
||||
style := c.getSeriesStyleDefaults(seriesIndex)
|
||||
var annotationBounds Box
|
||||
if as.YAxis == YAxisPrimary {
|
||||
annotationBounds = as.Measure(r, canvasBox, xr, yr, style)
|
||||
} else if as.YAxis == YAxisSecondary {
|
||||
annotationBounds = as.Measure(r, canvasBox, xr, yr, style)
|
||||
annotationBounds = as.Measure(r, canvasBox, xr, yra, style)
|
||||
}
|
||||
|
||||
if annotationMinY > annotationBounds.Top {
|
||||
|
@ -366,8 +356,8 @@ func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr,
|
|||
}
|
||||
if annotationMinY < 0 {
|
||||
// figure out how much top padding to add
|
||||
delta := -1*annotationMinY
|
||||
newBox.Top = canvasBox.Top+delta
|
||||
delta := -1 * annotationMinY
|
||||
newBox.Top = canvasBox.Top + delta
|
||||
}
|
||||
|
||||
if annotationMaxX > c.Width {
|
||||
|
@ -378,13 +368,14 @@ func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr,
|
|||
|
||||
if annotationMinX < 0 {
|
||||
// figure out how much left padding to add
|
||||
delta := -1*annotationMinX
|
||||
delta := -1 * annotationMinX
|
||||
newBox.Left = canvasBox.Left + delta
|
||||
}
|
||||
|
||||
if annotationMaxY > c.Height {
|
||||
//figure out how much bottom padding to add
|
||||
delta := annotationMaxY - c.Height
|
||||
println("bottom delta", annotationMaxY, c.Height)
|
||||
newBox.Bottom = canvasBox.Bottom - delta
|
||||
}
|
||||
|
||||
|
|
4
xaxis.go
4
xaxis.go
|
@ -71,6 +71,10 @@ func (xa XAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter)
|
|||
Value: cursor,
|
||||
Label: vf(cursor),
|
||||
})
|
||||
|
||||
if len(ticks) == 20 {
|
||||
return ticks
|
||||
}
|
||||
}
|
||||
return ticks
|
||||
}
|
||||
|
|
3
yaxis.go
3
yaxis.go
|
@ -58,6 +58,9 @@ func (ya YAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter)
|
|||
Value: cursor,
|
||||
Label: vf(cursor),
|
||||
})
|
||||
if len(ticks) == 20 {
|
||||
return ticks
|
||||
}
|
||||
}
|
||||
return ticks
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue