Merge branch 'master' of github.com:wcharczuk/go-chart

This commit is contained in:
Will Charczuk 2016-07-10 18:13:59 -07:00
commit 36e012f8cb
4 changed files with 59 additions and 59 deletions

View file

@ -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
}
}
}

View file

@ -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
}
@ -327,7 +317,7 @@ 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.Style.Show {
@ -336,7 +326,7 @@ func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr,
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 {
@ -385,6 +375,7 @@ func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr,
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
}

View file

@ -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
}

View file

@ -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
}