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 package chart
import "math"
// Annotation is a label on the chart. // Annotation is a label on the chart.
type Annotation struct { type Annotation struct {
X, Y float64 X, Y float64
@ -32,10 +34,10 @@ func (as AnnotationSeries) GetYAxis() YAxisType {
// Measure returns a bounds box of the series. // Measure returns a bounds box of the series.
func (as AnnotationSeries) Measure(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) Box { func (as AnnotationSeries) Measure(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) Box {
box := Box{ box := Box{
Top: canvasBox.Bottom, Top: math.MaxInt32,
Left: canvasBox.Right, Left: math.MaxInt32,
Right: canvasBox.Left, Right: 0,
Bottom: canvasBox.Top, Bottom: 0,
} }
if as.Style.Show { if as.Style.Show {
style := as.Style.WithDefaultsFrom(Style{ 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 { for _, a := range as.Annotations {
lx := canvasBox.Right - xrange.Translate(a.X) lx := canvasBox.Right - xrange.Translate(a.X)
ly := yrange.Translate(a.Y) + canvasBox.Top ly := yrange.Translate(a.Y) + canvasBox.Top
aBox := MeasureAnnotation(r, canvasBox, xrange, yrange, style, lx, ly, a.Label) ab := MeasureAnnotation(r, canvasBox, xrange, yrange, style, lx, ly, a.Label)
if aBox.Top < box.Top { if ab.Top < box.Top {
box.Top = aBox.Top box.Top = ab.Top
} }
if aBox.Left < box.Left { if ab.Left < box.Left {
box.Left = aBox.Left box.Left = ab.Left
} }
if aBox.Right > box.Right { if ab.Right > box.Right {
box.Right = aBox.Right box.Right = ab.Right
} }
if aBox.Bottom > box.Bottom { if ab.Bottom > box.Bottom {
box.Bottom = aBox.Bottom box.Bottom = ab.Bottom
} }
} }
} }

View File

@ -3,6 +3,7 @@ package chart
import ( import (
"errors" "errors"
"io" "io"
"math"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/drawing" "github.com/wcharczuk/go-chart/drawing"
@ -92,7 +93,7 @@ func (c Chart) Render(rp RendererProvider, w io.Writer) error {
c.drawCanvas(r, canvasBox) c.drawCanvas(r, canvasBox)
c.drawAxes(r, canvasBox, xr, yr, yra, xt, yt, yta) c.drawAxes(r, canvasBox, xr, yr, yra, xt, yt, yta)
for index, series := range c.Series { for index, series := range c.Series {
c.drawSeries(r, canvasBox, xr, yr, yra, series, index) c.drawSeries(r, canvasBox, xr, yr, yra, series, index)
} }
@ -101,48 +102,36 @@ func (c Chart) Render(rp RendererProvider, w io.Writer) error {
} }
func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) { func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
//iterate over each series, pull out the min/max for x,y var globalMinX, globalMaxX float64 = math.MaxFloat64, 0
var didSetFirstValues bool var globalMinY, globalMaxY float64 = math.MaxFloat64, 0
var globalMinYA, globalMaxYA float64 = math.MaxFloat64, 0
var globalMinX, globalMaxX float64
var globalMinY, globalMaxY float64
var globalMinYA, globalMaxYA float64
for _, s := range c.Series { for _, s := range c.Series {
seriesAxis := s.GetYAxis()
if vp, isValueProvider := s.(ValueProvider); isValueProvider { if vp, isValueProvider := s.(ValueProvider); isValueProvider {
seriesLength := vp.Len() seriesLength := vp.Len()
for index := 0; index < seriesLength; index++ { for index := 0; index < seriesLength; index++ {
vx, vy := vp.GetValue(index) vx, vy := vp.GetValue(index)
if didSetFirstValues { if globalMinX > vx {
if globalMinX > vx { globalMinX = vx
globalMinX = vx }
if globalMaxX < vx {
globalMaxX = vx
}
if seriesAxis == YAxisPrimary {
if globalMinY > vy {
globalMinY = vy
} }
if globalMaxX < vx { if globalMaxY < vy {
globalMaxX = vx globalMaxY = vy
} }
if s.GetYAxis() == YAxisPrimary { } else if seriesAxis == YAxisSecondary {
if globalMinY > vy { if globalMinYA > vy {
globalMinY = vy globalMinYA = vy
}
if globalMaxY < vy {
globalMaxY = vy
}
} else if s.GetYAxis() == YAxisSecondary {
if globalMinYA > vy {
globalMinYA = vy
}
if globalMaxYA < vy {
globalMaxYA = vy
}
} }
} else { if globalMaxYA < vy {
globalMinX, globalMaxX = vx, vx globalMaxYA = vy
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.Min = globalMinYA
yrangeAlt.Max = globalMaxYA yrangeAlt.Max = globalMaxYA
} }
return return
} }
@ -317,28 +307,28 @@ func (c Chart) setRangeDomains(canvasBox Box, xrange, yrange, yrangeAlt Range) (
func (c Chart) hasAnnotationSeries() bool { func (c Chart) hasAnnotationSeries() bool {
for _, s := range c.Series { for _, s := range c.Series {
if as, isAnnotationSeries:= s.(AnnotationSeries); isAnnotationSeries { if as, isAnnotationSeries := s.(AnnotationSeries); isAnnotationSeries {
if as.Style.Show { if as.Style.Show {
return true return true
} }
} }
} }
return false return false
} }
func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, yra Range, xf, yf, yfa ValueFormatter) Box { 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 { for seriesIndex, s := range c.Series {
if as, isAnnotationSeries:= s.(AnnotationSeries); isAnnotationSeries { if as, isAnnotationSeries := s.(AnnotationSeries); isAnnotationSeries {
if as.Style.Show { if as.Style.Show {
style := c.getSeriesStyleDefaults(seriesIndex) style := c.getSeriesStyleDefaults(seriesIndex)
var annotationBounds Box var annotationBounds Box
if as.YAxis == YAxisPrimary { if as.YAxis == YAxisPrimary {
annotationBounds = as.Measure(r, canvasBox, xr, yr, style) annotationBounds = as.Measure(r, canvasBox, xr, yr, style)
} else if as.YAxis == YAxisSecondary { } 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 { if annotationMinY > annotationBounds.Top {
annotationMinY = annotationBounds.Top annotationMinY = annotationBounds.Top
} }
@ -359,15 +349,15 @@ func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr,
} }
newBox := Box{ newBox := Box{
Top: canvasBox.Top, Top: canvasBox.Top,
Left: canvasBox.Left, Left: canvasBox.Left,
Right: canvasBox.Right, Right: canvasBox.Right,
Bottom: canvasBox.Bottom, Bottom: canvasBox.Bottom,
} }
if annotationMinY < 0 { if annotationMinY < 0 {
// figure out how much top padding to add // figure out how much top padding to add
delta := -1*annotationMinY delta := -1 * annotationMinY
newBox.Top = canvasBox.Top+delta newBox.Top = canvasBox.Top + delta
} }
if annotationMaxX > c.Width { if annotationMaxX > c.Width {
@ -378,13 +368,14 @@ func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr,
if annotationMinX < 0 { if annotationMinX < 0 {
// figure out how much left padding to add // figure out how much left padding to add
delta := -1*annotationMinX delta := -1 * annotationMinX
newBox.Left = canvasBox.Left + delta newBox.Left = canvasBox.Left + delta
} }
if annotationMaxY > c.Height { if annotationMaxY > c.Height {
//figure out how much bottom padding to add //figure out how much bottom padding to add
delta := annotationMaxY - c.Height delta := annotationMaxY - c.Height
println("bottom delta", annotationMaxY, c.Height)
newBox.Bottom = canvasBox.Bottom - delta newBox.Bottom = canvasBox.Bottom - delta
} }

View File

@ -71,6 +71,10 @@ func (xa XAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter)
Value: cursor, Value: cursor,
Label: vf(cursor), Label: vf(cursor),
}) })
if len(ticks) == 20 {
return ticks
}
} }
return ticks return ticks
} }

View File

@ -58,6 +58,9 @@ func (ya YAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter)
Value: cursor, Value: cursor,
Label: vf(cursor), Label: vf(cursor),
}) })
if len(ticks) == 20 {
return ticks
}
} }
return ticks return ticks
} }