This commit is contained in:
Will Charczuk 2016-07-11 18:48:51 -07:00
parent 01983f4e86
commit 9a5138b21d
17 changed files with 477 additions and 308 deletions

View file

@ -5,6 +5,9 @@ go-chart
Package `chart` is a very simple golang native charting library that supports timeseries and continuous Package `chart` is a very simple golang native charting library that supports timeseries and continuous
line charts. line charts.
The API is still in a bit of flux, so it is adviseable to wait until I tag a v1.0 release before using
in a production capacity.
# Installation # Installation
To install `chart` run the following: To install `chart` run the following:

View file

@ -49,21 +49,13 @@ func (as AnnotationSeries) Measure(r Renderer, canvasBox Box, xrange, yrange Ran
Padding: DefaultAnnotationPadding, Padding: DefaultAnnotationPadding,
}) })
for _, a := range as.Annotations { for _, a := range as.Annotations {
lx := canvasBox.Right - xrange.Translate(a.X) lx := canvasBox.Left - xrange.Translate(a.X)
ly := yrange.Translate(a.Y) + canvasBox.Top ly := canvasBox.Bottom - yrange.Translate(a.Y)
ab := MeasureAnnotation(r, canvasBox, xrange, yrange, style, lx, ly, a.Label) ab := MeasureAnnotation(r, canvasBox, style, lx, ly, a.Label)
if ab.Top < box.Top { box.Top = MinInt(box.Top, ab.Top)
box.Top = ab.Top box.Left = MinInt(box.Left, ab.Left)
} box.Right = MaxInt(box.Right, ab.Right)
if ab.Left < box.Left { box.Bottom = MaxInt(box.Bottom, ab.Bottom)
box.Left = ab.Left
}
if ab.Right > box.Right {
box.Right = ab.Right
}
if ab.Bottom > box.Bottom {
box.Bottom = ab.Bottom
}
} }
} }
return box return box
@ -81,9 +73,9 @@ func (as AnnotationSeries) Render(r Renderer, canvasBox Box, xrange, yrange Rang
Padding: DefaultAnnotationPadding, Padding: DefaultAnnotationPadding,
}) })
for _, a := range as.Annotations { for _, a := range as.Annotations {
lx := canvasBox.Right - xrange.Translate(a.X) lx := canvasBox.Left + xrange.Translate(a.X)
ly := yrange.Translate(a.Y) + canvasBox.Top ly := canvasBox.Bottom - yrange.Translate(a.Y)
DrawAnnotation(r, canvasBox, xrange, yrange, style, lx, ly, a.Label) DrawAnnotation(r, canvasBox, style, lx, ly, a.Label)
} }
} }
} }

159
chart.go
View file

@ -6,7 +6,6 @@ import (
"math" "math"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/drawing"
) )
// Chart is what we're drawing. // Chart is what we're drawing.
@ -57,6 +56,8 @@ func (c Chart) Render(rp RendererProvider, w io.Writer) error {
if len(c.Series) == 0 { if len(c.Series) == 0 {
return errors.New("Please provide at least one series") return errors.New("Please provide at least one series")
} }
c.YAxisSecondary.AxisType = YAxisSecondary
r, err := rp(c.Width, c.Height) r, err := rp(c.Width, c.Height)
if err != nil { if err != nil {
return err return err
@ -80,7 +81,7 @@ func (c Chart) Render(rp RendererProvider, w io.Writer) error {
if c.hasAxes() { if c.hasAxes() {
xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa) xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa)
canvasBox = c.getAxisAdjustedCanvasBox(r, canvasBox, xt, yt, yta) canvasBox = c.getAxisAdjustedCanvasBox(r, canvasBox, xr, yr, yra, xt, yt, yta)
xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra) xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra)
xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa) xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa)
} }
@ -143,6 +144,7 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
} else { } else {
xrange.Min = globalMinX xrange.Min = globalMinX
xrange.Max = globalMaxX xrange.Max = globalMaxX
xrange.Min, xrange.Max = xrange.GetRoundedRangeBounds()
} }
if !c.YAxis.Range.IsZero() { if !c.YAxis.Range.IsZero() {
@ -151,6 +153,7 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
} else { } else {
yrange.Min = globalMinY yrange.Min = globalMinY
yrange.Max = globalMaxY yrange.Max = globalMaxY
yrange.Min, yrange.Max = yrange.GetRoundedRangeBounds()
} }
if !c.YAxisSecondary.Range.IsZero() { if !c.YAxisSecondary.Range.IsZero() {
@ -159,6 +162,7 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
} else { } else {
yrangeAlt.Min = globalMinYA yrangeAlt.Min = globalMinYA
yrangeAlt.Max = globalMaxYA yrangeAlt.Max = globalMaxYA
yrangeAlt.Min, yrangeAlt.Max = yrangeAlt.GetRoundedRangeBounds()
} }
return return
@ -222,84 +226,63 @@ func (c Chart) getAxesTicks(r Renderer, xr, yr, yar Range, xf, yf, yfa ValueForm
return return
} }
func (c Chart) getAxisAdjustedCanvasBox(r Renderer, defaults Box, xticks, yticks, yticksAlt []Tick) Box { func (c Chart) getAxisAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, yra Range, xticks, yticks, yticksAlt []Tick) Box {
canvasBox := Box{} axesMinX, axesMaxX, axesMinY, axesMaxY := math.MaxInt32, 0, math.MaxInt32, 0
var dpl, dpr, dpb int
if c.XAxis.Style.Show { if c.XAxis.Style.Show {
dpb = c.getXAxisHeight(r, xticks) axesBounds := c.XAxis.Measure(r, canvasBox, xr, xticks)
axesMinY = MinInt(axesMinX, axesBounds.Top)
axesMinX = MinInt(axesMinY, axesBounds.Left)
axesMaxX = MaxInt(axesMaxX, axesBounds.Right)
axesMaxY = MaxInt(axesMaxY, axesBounds.Bottom)
} }
if c.YAxis.Style.Show { if c.YAxis.Style.Show {
dpr = c.getYAxisWidth(r, yticks) axesBounds := c.YAxis.Measure(r, canvasBox, yr, yticks)
axesMinY = MinInt(axesMinX, axesBounds.Top)
axesMinX = MinInt(axesMinY, axesBounds.Left)
axesMaxX = MaxInt(axesMaxX, axesBounds.Right)
axesMaxY = MaxInt(axesMaxY, axesBounds.Bottom)
} }
if c.YAxisSecondary.Style.Show { if c.YAxisSecondary.Style.Show {
dpl = c.getYAxisSecondaryWidth(r, yticksAlt) axesBounds := c.YAxisSecondary.Measure(r, canvasBox, yra, yticksAlt)
axesMinY = MinInt(axesMinX, axesBounds.Top)
axesMinX = MinInt(axesMinY, axesBounds.Left)
axesMaxX = MaxInt(axesMaxX, axesBounds.Right)
axesMaxY = MaxInt(axesMaxY, axesBounds.Bottom)
}
newBox := Box{
Top: canvasBox.Top,
Left: canvasBox.Left,
Right: canvasBox.Right,
Bottom: canvasBox.Bottom,
} }
canvasBox.Top = defaults.Top if axesMinY < 0 {
if dpl != 0 { // figure out how much top padding to add
canvasBox.Left = c.Canvas.Padding.GetLeft(dpl) delta := -1 * axesMinY
} else { newBox.Top = canvasBox.Top + delta
canvasBox.Left = defaults.Left
}
if dpr != 0 {
canvasBox.Right = c.Width - c.Canvas.Padding.GetRight(dpr)
} else {
canvasBox.Right = defaults.Right
}
if dpb != 0 {
canvasBox.Bottom = c.Height - c.Canvas.Padding.GetBottom(dpb)
} else {
canvasBox.Bottom = defaults.Bottom
} }
canvasBox.Width = canvasBox.Right - canvasBox.Left if axesMinX < 0 {
canvasBox.Height = canvasBox.Bottom - canvasBox.Top // figure out how much left padding to add
return canvasBox delta := -1 * axesMinX
} newBox.Left = canvasBox.Left + delta
func (c Chart) getXAxisHeight(r Renderer, ticks []Tick) int {
r.SetFontSize(c.XAxis.Style.GetFontSize(DefaultFontSize))
r.SetFont(c.XAxis.Style.GetFont(c.Font))
var textHeight int
for _, t := range ticks {
_, th := r.MeasureText(t.Label)
if th > textHeight {
textHeight = th
}
}
return textHeight + (2 * DefaultXAxisMargin) // top and bottom.
}
func (c Chart) getYAxisWidth(r Renderer, ticks []Tick) int {
r.SetFontSize(c.YAxis.Style.GetFontSize(DefaultFontSize))
r.SetFont(c.YAxis.Style.GetFont(c.Font))
var textWidth int
for _, t := range ticks {
tw, _ := r.MeasureText(t.Label)
if tw > textWidth {
textWidth = tw
}
} }
return textWidth + DefaultYAxisMargin if axesMaxX > c.Width {
} // figure out how much right padding to add
delta := axesMaxX - c.Width
func (c Chart) getYAxisSecondaryWidth(r Renderer, ticks []Tick) int { newBox.Right = canvasBox.Right - delta
r.SetFontSize(c.YAxisSecondary.Style.GetFontSize(DefaultFontSize))
r.SetFont(c.YAxisSecondary.Style.GetFont(c.Font))
var textWidth int
for _, t := range ticks {
tw, _ := r.MeasureText(t.Label)
if tw > textWidth {
textWidth = tw
}
} }
return textWidth + DefaultYAxisMargin if axesMaxY > c.Height {
//figure out how much bottom padding to add
delta := axesMaxY - c.Height
newBox.Bottom = canvasBox.Bottom - delta
}
newBox.Height = newBox.Bottom - newBox.Top
newBox.Width = newBox.Right - newBox.Left
return newBox
} }
func (c Chart) setRangeDomains(canvasBox Box, xrange, yrange, yrangeAlt Range) (Range, Range, Range) { func (c Chart) setRangeDomains(canvasBox Box, xrange, yrange, yrangeAlt Range) (Range, Range, Range) {
@ -333,21 +316,10 @@ func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr,
annotationBounds = as.Measure(r, canvasBox, xr, yra, style) annotationBounds = as.Measure(r, canvasBox, xr, yra, style)
} }
if annotationMinY > annotationBounds.Top { annotationMinY = MinInt(annotationMinY, annotationBounds.Top)
annotationMinY = annotationBounds.Top annotationMinX = MinInt(annotationMinX, annotationBounds.Left)
} annotationMaxX = MaxInt(annotationMaxX, annotationBounds.Right)
annotationMaxY = MaxInt(annotationMaxY, annotationBounds.Bottom)
if annotationMinX > annotationBounds.Left {
annotationMinX = annotationBounds.Left
}
if annotationMaxX < annotationBounds.Right {
annotationMaxX = annotationBounds.Right
}
if annotationMaxY < annotationBounds.Bottom {
annotationMaxY = annotationBounds.Bottom
}
} }
} }
} }
@ -364,22 +336,21 @@ func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr,
newBox.Top = canvasBox.Top + delta newBox.Top = canvasBox.Top + delta
} }
if annotationMaxX > c.Width {
// figure out how much right padding to add
delta := annotationMaxX - c.Width
newBox.Right = canvasBox.Right - delta
}
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 annotationMaxX > c.Width {
// figure out how much right padding to add
delta := annotationMaxX - c.Width
newBox.Right = canvasBox.Right - 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
} }
@ -420,10 +391,10 @@ func (c Chart) drawAxes(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt Ran
c.XAxis.Render(r, canvasBox, xrange, xticks) c.XAxis.Render(r, canvasBox, xrange, xticks)
} }
if c.YAxis.Style.Show { if c.YAxis.Style.Show {
c.YAxis.Render(r, canvasBox, yrange, YAxisPrimary, yticks) c.YAxis.Render(r, canvasBox, yrange, yticks)
} }
if c.YAxisSecondary.Style.Show { if c.YAxisSecondary.Style.Show {
c.YAxisSecondary.Render(r, canvasBox, yrangeAlt, YAxisSecondary, yticksAlt) c.YAxisSecondary.Render(r, canvasBox, yrangeAlt, yticksAlt)
} }
} }
@ -453,10 +424,10 @@ func (c Chart) drawTitle(r Renderer) {
titleFontSize := c.TitleStyle.GetFontSize(DefaultTitleFontSize) titleFontSize := c.TitleStyle.GetFontSize(DefaultTitleFontSize)
r.SetFontSize(titleFontSize) r.SetFontSize(titleFontSize)
textWidthPoints, textHeightPoints := r.MeasureText(c.Title) textBox := r.MeasureText(c.Title)
textWidth := int(drawing.PointsToPixels(r.GetDPI(), float64(textWidthPoints))) textWidth := textBox.Width
textHeight := int(drawing.PointsToPixels(r.GetDPI(), float64(textHeightPoints))) textHeight := textBox.Height
titleX := (c.Width >> 1) - (textWidth >> 1) titleX := (c.Width >> 1) - (textWidth >> 1)
titleY := c.TitleStyle.Padding.GetTop(DefaultTitleTop) + textHeight titleY := c.TitleStyle.Padding.GetTop(DefaultTitleTop) + textHeight

View file

@ -38,11 +38,13 @@ const (
// DefaultXAxisMargin is the default distance from bottom of the canvas to the x axis labels. // DefaultXAxisMargin is the default distance from bottom of the canvas to the x axis labels.
DefaultXAxisMargin = 10 DefaultXAxisMargin = 10
//DefaultVerticalTickWidth is half the margin. //DefaultVerticalTickHeight is half the margin.
DefaultVerticalTickWidth = DefaultYAxisMargin >> 1 DefaultVerticalTickHeight = DefaultXAxisMargin >> 1
//DefaultHorizontalTickWidth is half the margin. //DefaultHorizontalTickWidth is half the margin.
DefaultHorizontalTickWidth = DefaultXAxisMargin >> 1 DefaultHorizontalTickWidth = DefaultYAxisMargin >> 1
// DefaultTickCount is the defautl number of ticks to show
DefaultTickCount = 10
// DefaultMinimumTickHorizontalSpacing is the minimum distance between horizontal ticks. // DefaultMinimumTickHorizontalSpacing is the minimum distance between horizontal ticks.
DefaultMinimumTickHorizontalSpacing = 20 DefaultMinimumTickHorizontalSpacing = 20
@ -91,6 +93,17 @@ var (
} }
) )
var (
// DashArrayDots is a dash array that represents '....' style stroke dashes.
DashArrayDots = []int{1, 1}
// DashArrayDashesSmall is a dash array that represents '- - -' style stroke dashes.
DashArrayDashesSmall = []int{3, 3}
// DashArrayDashesMedium is a dash array that represents '-- -- --' style stroke dashes.
DashArrayDashesMedium = []int{5, 5}
// DashArrayDashesLarge is a dash array that represents '----- ----- -----' style stroke dashes.
DashArrayDashesLarge = []int{10, 10}
)
// GetDefaultSeriesStrokeColor returns a color from the default list by index. // GetDefaultSeriesStrokeColor returns a color from the default list by index.
// NOTE: the index will wrap around (using a modulo).g // NOTE: the index will wrap around (using a modulo).g
func GetDefaultSeriesStrokeColor(index int) drawing.Color { func GetDefaultSeriesStrokeColor(index int) drawing.Color {
@ -100,7 +113,7 @@ func GetDefaultSeriesStrokeColor(index int) drawing.Color {
var ( var (
// DefaultAnnotationPadding is the padding around an annotation. // DefaultAnnotationPadding is the padding around an annotation.
DefaultAnnotationPadding = Box{Top: 5, Left: 5, Right: 5, Bottom: 5} DefaultAnnotationPadding = Box{Top: 3, Left: 5, Right: 5, Bottom: 5}
// DefaultBackgroundPadding is the default canvas padding config. // DefaultBackgroundPadding is the default canvas padding config.
DefaultBackgroundPadding = Box{Top: 5, Left: 5, Right: 5, Bottom: 5} DefaultBackgroundPadding = Box{Top: 5, Left: 5, Right: 5, Bottom: 5}
) )

View file

@ -6,13 +6,12 @@ func DrawLineSeries(r Renderer, canvasBox Box, xrange, yrange Range, s Style, vs
return return
} }
ct := canvasBox.Top
cb := canvasBox.Bottom cb := canvasBox.Bottom
cr := canvasBox.Right cl := canvasBox.Left
v0x, v0y := vs.GetValue(0) v0x, v0y := vs.GetValue(0)
x0 := cr - xrange.Translate(v0x) x0 := cl + xrange.Translate(v0x)
y0 := yrange.Translate(v0y) + ct y0 := cb - yrange.Translate(v0y)
var vx, vy float64 var vx, vy float64
var x, y int var x, y int
@ -23,8 +22,8 @@ func DrawLineSeries(r Renderer, canvasBox Box, xrange, yrange Range, s Style, vs
r.MoveTo(x0, y0) r.MoveTo(x0, y0)
for i := 1; i < vs.Len(); i++ { for i := 1; i < vs.Len(); i++ {
vx, vy = vs.GetValue(i) vx, vy = vs.GetValue(i)
x = cr - xrange.Translate(vx) x = cl + xrange.Translate(vx)
y = yrange.Translate(vy) + ct y = cb - yrange.Translate(vy)
r.LineTo(x, y) r.LineTo(x, y)
} }
r.LineTo(x, cb) r.LineTo(x, cb)
@ -40,19 +39,19 @@ func DrawLineSeries(r Renderer, canvasBox Box, xrange, yrange Range, s Style, vs
r.MoveTo(x0, y0) r.MoveTo(x0, y0)
for i := 1; i < vs.Len(); i++ { for i := 1; i < vs.Len(); i++ {
vx, vy = vs.GetValue(i) vx, vy = vs.GetValue(i)
x = cr - xrange.Translate(vx) x = cl + xrange.Translate(vx)
y = yrange.Translate(vy) + ct y = cb - yrange.Translate(vy)
r.LineTo(x, y) r.LineTo(x, y)
} }
r.Stroke() r.Stroke()
} }
// MeasureAnnotation measures how big an annotation would be. // MeasureAnnotation measures how big an annotation would be.
func MeasureAnnotation(r Renderer, canvasBox Box, xrange, yrange Range, s Style, lx, ly int, label string) Box { func MeasureAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label string) Box {
r.SetFont(s.GetFont()) r.SetFont(s.GetFont())
r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize)) r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize))
textWidth, textHeight := r.MeasureText(label) textBox := r.MeasureText(label)
halfTextHeight := textHeight >> 1 halfTextHeight := textBox.Height >> 1
pt := s.Padding.GetTop(DefaultAnnotationPadding.Top) pt := s.Padding.GetTop(DefaultAnnotationPadding.Top)
pl := s.Padding.GetLeft(DefaultAnnotationPadding.Left) pl := s.Padding.GetLeft(DefaultAnnotationPadding.Left)
@ -62,7 +61,7 @@ func MeasureAnnotation(r Renderer, canvasBox Box, xrange, yrange Range, s Style,
strokeWidth := s.GetStrokeWidth() strokeWidth := s.GetStrokeWidth()
top := ly - (pt + halfTextHeight) top := ly - (pt + halfTextHeight)
right := lx + pl + pr + textWidth + DefaultAnnotationDeltaWidth + int(strokeWidth) right := lx + pl + pr + textBox.Width + DefaultAnnotationDeltaWidth + int(strokeWidth)
bottom := ly + (pb + halfTextHeight) bottom := ly + (pb + halfTextHeight)
return Box{ return Box{
@ -76,11 +75,11 @@ func MeasureAnnotation(r Renderer, canvasBox Box, xrange, yrange Range, s Style,
} }
// DrawAnnotation draws an anotation with a renderer. // DrawAnnotation draws an anotation with a renderer.
func DrawAnnotation(r Renderer, canvasBox Box, xrange, yrange Range, s Style, lx, ly int, label string) { func DrawAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label string) {
r.SetFont(s.GetFont()) r.SetFont(s.GetFont())
r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize)) r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize))
textWidth, textHeight := r.MeasureText(label) textBox := r.MeasureText(label)
halfTextHeight := textHeight >> 1 halfTextHeight := textBox.Height >> 1
pt := s.Padding.GetTop(DefaultAnnotationPadding.Top) pt := s.Padding.GetTop(DefaultAnnotationPadding.Top)
pl := s.Padding.GetLeft(DefaultAnnotationPadding.Left) pl := s.Padding.GetLeft(DefaultAnnotationPadding.Left)
@ -93,10 +92,10 @@ func DrawAnnotation(r Renderer, canvasBox Box, xrange, yrange Range, s Style, lx
ltx := lx + DefaultAnnotationDeltaWidth ltx := lx + DefaultAnnotationDeltaWidth
lty := ly - (pt + halfTextHeight) lty := ly - (pt + halfTextHeight)
rtx := lx + pl + pr + textWidth + DefaultAnnotationDeltaWidth rtx := lx + pl + pr + textBox.Width + DefaultAnnotationDeltaWidth
rty := ly - (pt + halfTextHeight) rty := ly - (pt + halfTextHeight)
rbx := lx + pl + pr + textWidth + DefaultAnnotationDeltaWidth rbx := lx + pl + pr + textBox.Width + DefaultAnnotationDeltaWidth
rby := ly + (pb + halfTextHeight) rby := ly + (pb + halfTextHeight)
lbx := lx + DefaultAnnotationDeltaWidth lbx := lx + DefaultAnnotationDeltaWidth
@ -119,3 +118,16 @@ func DrawAnnotation(r Renderer, canvasBox Box, xrange, yrange Range, s Style, lx
r.SetFontColor(s.GetFontColor(DefaultTextColor)) r.SetFontColor(s.GetFontColor(DefaultTextColor))
r.Text(label, textX, textY) r.Text(label, textX, textY)
} }
// DrawBox draws a box with a given style.
func DrawBox(r Renderer, b Box, s Style) {
r.SetFillColor(s.GetFillColor())
r.SetStrokeColor(s.GetStrokeColor(DefaultStrokeColor))
r.SetStrokeWidth(s.GetStrokeWidth(DefaultStrokeWidth))
r.MoveTo(b.Left, b.Top)
r.LineTo(b.Right, b.Top)
r.LineTo(b.Right, b.Bottom)
r.LineTo(b.Left, b.Bottom)
r.LineTo(b.Left, b.Top)
r.FillStroke()
}

43
grid_line.go Normal file
View file

@ -0,0 +1,43 @@
package chart
// GridLine is a line on a graph canvas.
type GridLine struct {
IsMinor bool
IsVertical bool
Style Style
Value float64
}
// Major returns if the gridline is a `major` line.
func (gl GridLine) Major() bool {
return !gl.IsMinor
}
// Minor returns if the gridline is a `minor` line.
func (gl GridLine) Minor() bool {
return gl.IsMinor
}
// Vertical returns if the line is vertical line or not.
func (gl GridLine) Vertical() bool {
return gl.IsVertical
}
// Horizontal returns if the line is horizontal line or not.
func (gl GridLine) Horizontal() bool {
return !gl.IsVertical
}
// Render renders the gridline
func (gl GridLine) Render(r Renderer, canvasBox Box, ra Range) {
lineleft := canvasBox.Left
lineright := canvasBox.Right
lineheight := canvasBox.Bottom - ra.Translate(gl.Value)
r.SetStrokeColor(gl.Style.GetStrokeColor(DefaultAxisColor))
r.SetStrokeWidth(gl.Style.GetStrokeWidth(DefaultAxisLineWidth))
r.MoveTo(lineleft, lineheight)
r.LineTo(lineright, lineheight)
r.Stroke()
}

View file

@ -28,9 +28,15 @@ func (r Range) String() string {
} }
// Translate maps a given value into the range space. // Translate maps a given value into the range space.
// An example would be a 600 px image, with a min of 10 and a max of 100.
// Translate(50) would yield (50.0/90.0)*600 ~= 333.33
func (r Range) Translate(value float64) int { func (r Range) Translate(value float64) int {
finalValue := ((r.Max - value) / r.Delta()) * float64(r.Domain) normalized := value - r.Min
return int(math.Floor(finalValue)) ratio := normalized / r.Delta()
return int(math.Ceil(ratio * float64(r.Domain)))
}
// GetRoundedRangeBounds returns some `prettified` range bounds.
func (r Range) GetRoundedRangeBounds() (min, max float64) {
delta := r.Max - r.Min
roundTo := GetRoundToForDelta(delta)
return RoundDown(r.Min, roundTo), RoundUp(r.Max, roundTo)
} }

View file

@ -5,6 +5,7 @@ import (
"image/color" "image/color"
"image/png" "image/png"
"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"
@ -48,16 +49,21 @@ func (rr *rasterRenderer) SetStrokeColor(c drawing.Color) {
rr.gc.SetStrokeColor(c) rr.gc.SetStrokeColor(c)
} }
// SetFillColor implements the interface method.
func (rr *rasterRenderer) SetFillColor(c drawing.Color) {
rr.gc.SetFillColor(c)
}
// SetLineWidth implements the interface method. // SetLineWidth implements the interface method.
func (rr *rasterRenderer) SetStrokeWidth(width float64) { func (rr *rasterRenderer) SetStrokeWidth(width float64) {
rr.gc.SetLineWidth(width) rr.gc.SetLineWidth(width)
} }
// StrokeDashArray sets the stroke dash array.
func (rr *rasterRenderer) SetStrokeDashArray(dashArray []float64) {
rr.gc.SetLineDash(dashArray, 0.0)
}
// SetFillColor implements the interface method.
func (rr *rasterRenderer) SetFillColor(c drawing.Color) {
rr.gc.SetFillColor(c)
}
// MoveTo implements the interface method. // MoveTo implements the interface method.
func (rr *rasterRenderer) MoveTo(x, y int) { func (rr *rasterRenderer) MoveTo(x, y int) {
rr.gc.MoveTo(float64(x), float64(y)) rr.gc.MoveTo(float64(x), float64(y))
@ -127,14 +133,38 @@ func (rr *rasterRenderer) Text(body string, x, y int) {
} }
// MeasureText returns the height and width in pixels of a string. // MeasureText returns the height and width in pixels of a string.
func (rr *rasterRenderer) MeasureText(body string) (width int, height int) { func (rr *rasterRenderer) MeasureText(body string) Box {
l, t, r, b, err := rr.gc.GetStringBounds(body) l, t, r, b, err := rr.gc.GetStringBounds(body)
if err != nil { if err != nil {
return return Box{}
}
if l < 0 {
r = r - l // equivalent to r+(-1*l)
l = 0
}
if t < 0 {
b = b - t
t = 0
}
if l > 0 {
r = r + l
l = 0
}
if t > 0 {
b = b + t
t = 0
}
return Box{
Top: int(math.Ceil(t)),
Left: int(math.Ceil(l)),
Right: int(math.Ceil(r)),
Bottom: int(math.Ceil(b)),
Width: int(math.Ceil(r - l)),
Height: int(math.Ceil(b - t)),
} }
width = int(r - l)
height = int(b - t)
return
} }
// Save implements the interface method. // Save implements the interface method.

View file

@ -24,6 +24,9 @@ type Renderer interface {
// SetStrokeWidth sets the stroke width. // SetStrokeWidth sets the stroke width.
SetStrokeWidth(width float64) SetStrokeWidth(width float64)
// SetStrokeDashArray sets the stroke dash array.
SetStrokeDashArray(dashArray []float64)
// MoveTo moves the cursor to a given point. // MoveTo moves the cursor to a given point.
MoveTo(x, y int) MoveTo(x, y int)
@ -59,7 +62,7 @@ type Renderer interface {
Text(body string, x, y int) Text(body string, x, y int)
// MeasureText measures text. // MeasureText measures text.
MeasureText(body string) (width int, height int) MeasureText(body string) Box
// Save writes the image to the given writer. // Save writes the image to the given writer.
Save(w io.Writer) error Save(w io.Writer) error

View file

@ -13,12 +13,14 @@ type Style struct {
Show bool Show bool
Padding Box Padding Box
StrokeWidth float64 StrokeWidth float64
StrokeColor drawing.Color StrokeColor drawing.Color
FillColor drawing.Color StrokeDashArray []float64
FontSize float64
FontColor drawing.Color FillColor drawing.Color
Font *truetype.Font FontSize float64
FontColor drawing.Color
Font *truetype.Font
} }
// IsZero returns if the object is set or not. // IsZero returns if the object is set or not.
@ -59,6 +61,17 @@ func (s Style) GetStrokeWidth(defaults ...float64) float64 {
return s.StrokeWidth return s.StrokeWidth
} }
// GetStrokeDashArray returns the stroke dash array.
func (s Style) GetStrokeDashArray(defaults ...[]float64) []float64 {
if len(s.StrokeDashArray) == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return nil
}
return s.StrokeDashArray
}
// GetFontSize gets the font size. // GetFontSize gets the font size.
func (s Style) GetFontSize(defaults ...float64) float64 { func (s Style) GetFontSize(defaults ...float64) float64 {
if s.FontSize == 0 { if s.FontSize == 0 {

View file

@ -2,14 +2,16 @@ package main
import ( import (
"log" "log"
"math/rand"
"net/http" "net/http"
"time"
"github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
"github.com/wcharczuk/go-web" "github.com/wcharczuk/go-web"
) )
func chartHandler(rc *web.RequestContext) web.ControllerResult { func chartHandler(rc *web.RequestContext) web.ControllerResult {
rnd := rand.New(rand.NewSource(0))
format, err := rc.RouteParameter("format") format, err := rc.RouteParameter("format")
if err != nil { if err != nil {
format = "png" format = "png"
@ -21,11 +23,14 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
rc.Response.Header().Set("Content-Type", "image/svg+xml") rc.Response.Header().Set("Content-Type", "image/svg+xml")
} }
s1x := []float64{2.0, 3.0, 4.0, 5.0} var s1x []time.Time
s1y := []float64{2.5, 5.0, 2.0, 3.3} for x := 0; x < 100; x++ {
s1x = append([]time.Time{time.Now().AddDate(0, 0, -1*x)}, s1x...)
s2x := []float64{0.0, 0.5, 1.0, 1.5} }
s2y := []float64{1.1, 1.2, 1.0, 1.3} var s1y []float64
for x := 0; x < 100; x++ {
s1y = append(s1y, rnd.Float64()*1024)
}
c := chart.Chart{ c := chart.Chart{
Title: "A Test Chart", Title: "A Test Chart",
@ -43,32 +48,19 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
Style: chart.Style{ Style: chart.Style{
Show: true, Show: true,
}, },
Range: chart.Range{ Zero: chart.GridLine{
Min: 0.0, Style: chart.Style{
Max: 7.0, Show: true,
}, StrokeWidth: 1.0,
}, },
YAxisSecondary: chart.YAxis{
Style: chart.Style{
Show: true,
},
Range: chart.Range{
Min: 0.8,
Max: 1.5,
}, },
}, },
Series: []chart.Series{ Series: []chart.Series{
chart.ContinuousSeries{ chart.TimeSeries{
Name: "a", Name: "a",
XValues: s1x, XValues: s1x,
YValues: s1y, YValues: s1y,
}, },
chart.ContinuousSeries{
Name: "b",
YAxis: chart.YAxisSecondary,
XValues: s2x,
YValues: s2y,
},
}, },
} }
@ -84,56 +76,6 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
return nil return nil
} }
func measureTestHandler(rc *web.RequestContext) web.ControllerResult {
format, err := rc.RouteParameter("format")
if err != nil {
format = "png"
}
if format == "png" {
rc.Response.Header().Set("Content-Type", "image/png")
} else {
rc.Response.Header().Set("Content-Type", "image/svg+xml")
}
var r chart.Renderer
if format == "png" {
r, err = chart.PNG(512, 512)
} else {
r, err = chart.SVG(512, 512)
}
if err != nil {
return rc.API().InternalError(err)
}
f, err := chart.GetDefaultFont()
if err != nil {
return rc.API().InternalError(err)
}
r.SetDPI(96)
r.SetFont(f)
r.SetFontSize(10.0)
r.SetFontColor(drawing.ColorBlack)
r.SetStrokeColor(drawing.ColorBlack)
label := "goog - 702.23"
tx, ty := 64, 64
tw, th := r.MeasureText(label)
r.MoveTo(tx, ty)
r.LineTo(tx+tw, ty)
r.LineTo(tx+tw, ty-th)
r.LineTo(tx, ty-th)
r.LineTo(tx, ty)
r.Stroke()
r.Text(label, tx, ty)
r.Save(rc.Response)
return nil
}
func main() { func main() {
app := web.New() app := web.New()
app.SetName("Chart Test Server") app.SetName("Chart Test Server")
@ -143,6 +85,5 @@ func main() {
app.GET("/favico.ico", func(rc *web.RequestContext) web.ControllerResult { app.GET("/favico.ico", func(rc *web.RequestContext) web.ControllerResult {
return rc.Raw([]byte{}) return rc.Raw([]byte{})
}) })
app.GET("/measure", measureTestHandler)
log.Fatal(app.Start()) log.Fatal(app.Start())
} }

View file

@ -3,7 +3,8 @@ package chart
// GenerateTicksWithStep generates a set of ticks. // GenerateTicksWithStep generates a set of ticks.
func GenerateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick { func GenerateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick {
var ticks []Tick var ticks []Tick
for cursor := ra.Min; cursor < ra.Max; cursor += step { min, max := ra.GetRoundedRangeBounds()
for cursor := min; cursor <= max; cursor += step {
ticks = append(ticks, Tick{ ticks = append(ticks, Tick{
Value: cursor, Value: cursor,
Label: vf(cursor), Label: vf(cursor),

57
util.go
View file

@ -2,9 +2,18 @@ package chart
import ( import (
"fmt" "fmt"
"math"
"time" "time"
) )
// Float is an alias for float64 that provides a better .String() method.
type Float float64
// String returns the string representation of a float.
func (f Float) String() string {
return fmt.Sprintf("%.2f", f)
}
// TimeToFloat64 returns a float64 representation of a time. // TimeToFloat64 returns a float64 representation of a time.
func TimeToFloat64(t time.Time) float64 { func TimeToFloat64(t time.Time) float64 {
return float64(t.Unix()) return float64(t.Unix())
@ -60,10 +69,48 @@ func Slices(count int, total float64) []float64 {
return values return values
} }
// Float is an alias for float64 that provides a better .String() method. // GetRoundToForDelta returns a `roundTo` value for a given delta.
type Float float64 func GetRoundToForDelta(delta float64) float64 {
startingDeltaBound := math.Pow(10.0, 10.0)
for cursor := startingDeltaBound; cursor > 0; cursor /= 10.0 {
if delta > cursor {
return cursor / 10.0
}
}
// String returns the string representation of a float. return 0.0
func (f Float) String() string { }
return fmt.Sprintf("%.2f", f)
// RoundUp rounds up to a given roundTo value.
func RoundUp(value, roundTo float64) float64 {
d1 := math.Ceil(value / roundTo)
return d1 * roundTo
}
// RoundDown rounds down to a given roundTo value.
func RoundDown(value, roundTo float64) float64 {
d1 := math.Floor(value / roundTo)
return d1 * roundTo
}
// MinInt returns the minimum of a set of integers.
func MinInt(values ...int) int {
min := math.MaxInt32
for _, v := range values {
if v < min {
min = v
}
}
return min
}
// MaxInt returns the maximum of a set of integers.
func MaxInt(values ...int) int {
max := math.MinInt32
for _, v := range values {
if v > max {
max = v
}
}
return max
} }

View file

@ -75,3 +75,11 @@ func TestSlices(t *testing.T) {
assert.Equal(20, s[2]) assert.Equal(20, s[2])
assert.Equal(90, s[9]) assert.Equal(90, s[9])
} }
func TestGetRoundToForDelta(t *testing.T) {
assert := assert.New(t)
assert.Equal(100.0, GetRoundToForDelta(1001.00))
assert.Equal(10.0, GetRoundToForDelta(101.00))
assert.Equal(1.0, GetRoundToForDelta(11.00))
}

View file

@ -61,6 +61,11 @@ func (vr *vectorRenderer) SetStrokeWidth(width float64) {
vr.s.StrokeWidth = width vr.s.StrokeWidth = width
} }
// StrokeDashArray sets the stroke dash array.
func (vr *vectorRenderer) SetStrokeDashArray(dashArray []float64) {
vr.s.StrokeDashArray = dashArray
}
// MoveTo implements the interface method. // MoveTo implements the interface method.
func (vr *vectorRenderer) MoveTo(x, y int) { func (vr *vectorRenderer) MoveTo(x, y int) {
vr.p = append(vr.p, fmt.Sprintf("M %d %d", x, y)) vr.p = append(vr.p, fmt.Sprintf("M %d %d", x, y))
@ -71,6 +76,7 @@ func (vr *vectorRenderer) LineTo(x, y int) {
vr.p = append(vr.p, fmt.Sprintf("L %d %d", x, y)) vr.p = append(vr.p, fmt.Sprintf("L %d %d", x, y))
} }
// Close closes a shape.
func (vr *vectorRenderer) Close() { func (vr *vectorRenderer) Close() {
vr.p = append(vr.p, fmt.Sprintf("Z")) vr.p = append(vr.p, fmt.Sprintf("Z"))
} }
@ -90,6 +96,7 @@ func (vr *vectorRenderer) FillStroke() {
vr.drawPath(vr.s.SVGFillAndStroke()) vr.drawPath(vr.s.SVGFillAndStroke())
} }
// drawPath draws a path.
func (vr *vectorRenderer) drawPath(s Style) { func (vr *vectorRenderer) drawPath(s Style) {
vr.c.Path(strings.Join(vr.p, "\n"), s.SVG(vr.dpi)) vr.c.Path(strings.Join(vr.p, "\n"), s.SVG(vr.dpi))
vr.p = []string{} // clear the path vr.p = []string{} // clear the path
@ -115,6 +122,7 @@ func (vr *vectorRenderer) SetFontSize(size float64) {
vr.s.FontSize = size vr.s.FontSize = size
} }
// svgFontFace returns the font face component of an svg element style.
func (vr *vectorRenderer) svgFontFace() string { func (vr *vectorRenderer) svgFontFace() string {
family := "sans-serif" family := "sans-serif"
if vr.f != nil { if vr.f != nil {
@ -133,7 +141,7 @@ func (vr *vectorRenderer) Text(body string, x, y int) {
} }
// MeasureText uses the truetype font drawer to measure the width of text. // MeasureText uses the truetype font drawer to measure the width of text.
func (vr *vectorRenderer) MeasureText(body string) (width, height int) { func (vr *vectorRenderer) MeasureText(body string) (box Box) {
if vr.f != nil { if vr.f != nil {
vr.fc = &font.Drawer{ vr.fc = &font.Drawer{
Face: truetype.NewFace(vr.f, &truetype.Options{ Face: truetype.NewFace(vr.f, &truetype.Options{
@ -143,12 +151,15 @@ func (vr *vectorRenderer) MeasureText(body string) (width, height int) {
} }
w := vr.fc.MeasureString(body).Ceil() w := vr.fc.MeasureString(body).Ceil()
width = w box.Right = w
height = int(drawing.PointsToPixels(vr.dpi, vr.s.FontSize)) box.Bottom = int(drawing.PointsToPixels(vr.dpi, vr.s.FontSize))
box.Width = w
box.Height = int(drawing.PointsToPixels(vr.dpi, vr.s.FontSize))
} }
return return
} }
// Save saves the renderer's contents to a writer.
func (vr *vectorRenderer) Save(w io.Writer) error { func (vr *vectorRenderer) Save(w io.Writer) error {
vr.c.End() vr.c.End()
_, err := w.Write(vr.b.Bytes()) _, err := w.Write(vr.b.Bytes())

View file

@ -3,8 +3,6 @@ package chart
import ( import (
"math" "math"
"sort" "sort"
"github.com/wcharczuk/go-chart/drawing"
) )
// XAxis represents the horizontal axis. // XAxis represents the horizontal axis.
@ -57,13 +55,46 @@ func (xa XAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
if len(ln) > len(l0) { if len(ln) > len(l0) {
ll = ln ll = ln
} }
llw, _ := r.MeasureText(ll) llb := r.MeasureText(ll)
textWidth := drawing.PointsToPixels(r.GetDPI(), float64(llw)) textWidth := llb.Width
width := textWidth + DefaultMinimumTickHorizontalSpacing width := textWidth + DefaultMinimumTickHorizontalSpacing
count := int(math.Ceil(float64(ra.Domain) / float64(width))) count := int(math.Ceil(float64(ra.Domain) / float64(width)))
return count return count
} }
// Measure returns the bounds of the axis.
func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, ticks []Tick) Box {
defaultFont, _ := GetDefaultFont()
r.SetFont(xa.Style.GetFont(defaultFont))
r.SetFontSize(xa.Style.GetFontSize(DefaultFontSize))
sort.Sort(Ticks(ticks))
var left, right, top, bottom = math.MaxInt32, 0, math.MaxInt32, 0
for _, t := range ticks {
v := t.Value
lx := ra.Translate(v)
tb := r.MeasureText(t.Label)
tx := canvasBox.Left + lx
ty := canvasBox.Bottom + DefaultXAxisMargin + tb.Height
top = MinInt(top, canvasBox.Bottom)
left = MinInt(left, tx-(tb.Width>>1))
right = MaxInt(right, tx+(tb.Width>>1))
bottom = MaxInt(bottom, ty)
}
return Box{
Top: top,
Left: left,
Right: right,
Bottom: bottom,
Width: right - left,
Height: bottom - top,
}
}
// Render renders the axis // Render renders the axis
func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, ticks []Tick) { func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, ticks []Tick) {
tickFontSize := xa.Style.GetFontSize(DefaultFontSize) tickFontSize := xa.Style.GetFontSize(DefaultFontSize)
@ -80,18 +111,16 @@ func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, ticks []Tick) {
sort.Sort(Ticks(ticks)) sort.Sort(Ticks(ticks))
var textHeight int
for _, t := range ticks {
_, th := r.MeasureText(t.Label)
if th > textHeight {
textHeight = th
}
}
ty := canvasBox.Bottom + DefaultXAxisMargin + int(textHeight)
for _, t := range ticks { for _, t := range ticks {
v := t.Value v := t.Value
x := ra.Translate(v) lx := ra.Translate(v)
tx := canvasBox.Right - x tb := r.MeasureText(t.Label)
r.Text(t.Label, tx, ty) tx := canvasBox.Left + lx
ty := canvasBox.Bottom + DefaultXAxisMargin + tb.Height
r.Text(t.Label, tx-tb.Width>>1, ty)
r.MoveTo(tx, canvasBox.Bottom)
r.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
r.Stroke()
} }
} }

152
yaxis.go
View file

@ -8,8 +8,13 @@ import (
// YAxis is a veritcal rule of the range. // YAxis is a veritcal rule of the range.
// There can be (2) y-axes; a primary and secondary. // There can be (2) y-axes; a primary and secondary.
type YAxis struct { type YAxis struct {
Name string Name string
Style Style Style Style
Zero GridLine
AxisType YAxisType
ValueFormatter ValueFormatter ValueFormatter ValueFormatter
Range Range Range Range
Ticks []Tick Ticks []Tick
@ -28,31 +33,81 @@ func (ya YAxis) GetStyle() Style {
// GetTicks returns the ticks for a series. It coalesces between user provided ticks and // GetTicks returns the ticks for a series. It coalesces between user provided ticks and
// generated ticks. // generated ticks.
func (ya YAxis) GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick { func (ya YAxis) GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
var ticks []Tick
if len(ya.Ticks) > 0 { if len(ya.Ticks) > 0 {
return ya.Ticks ticks = ya.Ticks
} else {
ticks = ya.generateTicks(r, ra, vf)
} }
return ya.generateTicks(r, ra, vf)
return ticks
} }
func (ya YAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick { func (ya YAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
step := ya.getTickStep(r, ra, vf) step := ya.getTickStep(r, ra, vf)
return GenerateTicksWithStep(ra, step, vf) ticks := GenerateTicksWithStep(ra, step, vf)
return ticks
} }
func (ya YAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 { func (ya YAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
tickCount := ya.getTickCount(r, ra, vf) tickCount := ya.getTickCount(r, ra, vf)
return ra.Delta() / float64(tickCount) step := ra.Delta() / float64(tickCount)
return step
} }
func (ya YAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int { func (ya YAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
textHeight := int(ya.Style.GetFontSize(DefaultFontSize)) return DefaultTickCount
height := textHeight + DefaultMinimumTickVerticalSpacing }
count := int(math.Ceil(float64(ra.Domain) / float64(height)))
return count // Measure returns the bounds of the axis.
func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, ticks []Tick) Box {
defaultFont, _ := GetDefaultFont()
r.SetFont(ya.Style.GetFont(defaultFont))
r.SetFontSize(ya.Style.GetFontSize(DefaultFontSize))
sort.Sort(Ticks(ticks))
var tx int
if ya.AxisType == YAxisPrimary {
tx = canvasBox.Right + DefaultYAxisMargin
} else if ya.AxisType == YAxisSecondary {
tx = canvasBox.Left - DefaultYAxisMargin
}
var minx, maxx, miny, maxy = math.MaxInt32, 0, math.MaxInt32, 0
for _, t := range ticks {
v := t.Value
ly := canvasBox.Bottom - ra.Translate(v)
tb := r.MeasureText(t.Label)
finalTextX := tx
if ya.AxisType == YAxisSecondary {
finalTextX = tx - tb.Width
}
if ya.AxisType == YAxisPrimary {
minx = canvasBox.Right
maxx = MaxInt(maxx, tx+tb.Width)
} else if ya.AxisType == YAxisSecondary {
minx = MinInt(minx, finalTextX)
maxx = MaxInt(maxx, tx)
}
miny = MinInt(miny, ly-tb.Height>>1)
maxy = MaxInt(maxy, ly+tb.Height>>1)
}
return Box{
Top: miny,
Left: minx,
Right: maxx,
Bottom: maxy,
Width: maxx - minx,
Height: maxy - miny,
}
} }
// Render renders the axis. // Render renders the axis.
func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType, ticks []Tick) { func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, ticks []Tick) {
r.SetStrokeColor(ya.Style.GetStrokeColor(DefaultAxisColor)) r.SetStrokeColor(ya.Style.GetStrokeColor(DefaultAxisColor))
r.SetStrokeWidth(ya.Style.GetStrokeWidth(DefaultAxisLineWidth)) r.SetStrokeWidth(ya.Style.GetStrokeWidth(DefaultAxisLineWidth))
@ -65,51 +120,42 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType,
var lx int var lx int
var tx int var tx int
if axisType == YAxisPrimary { if ya.AxisType == YAxisPrimary {
lx = canvasBox.Right lx = canvasBox.Right
tx = canvasBox.Right + DefaultYAxisMargin tx = canvasBox.Right + DefaultYAxisMargin
} else if ya.AxisType == YAxisSecondary {
r.MoveTo(lx, canvasBox.Bottom)
r.LineTo(lx, canvasBox.Top)
r.Stroke()
for _, t := range ticks {
v := t.Value
ly := ra.Translate(v) + canvasBox.Top
_, pth := r.MeasureText(t.Label)
ty := ly + pth>>1
r.Text(t.Label, tx, ty)
r.MoveTo(lx, ly)
r.LineTo(lx+DefaultVerticalTickWidth, ly)
r.Stroke()
}
} else if axisType == YAxisSecondary {
lx = canvasBox.Left lx = canvasBox.Left
tx = canvasBox.Left - DefaultYAxisMargin
r.MoveTo(lx, canvasBox.Bottom)
r.LineTo(lx, canvasBox.Top)
r.Stroke()
for _, t := range ticks {
v := t.Value
ly := ra.Translate(v) + canvasBox.Top
ptw, pth := r.MeasureText(t.Label)
tw := ptw
tx = lx - (tw + DefaultYAxisMargin)
ty := ly + pth>>1
r.Text(t.Label, tx, ty)
r.MoveTo(lx, ly)
r.LineTo(lx-DefaultVerticalTickWidth, ly)
r.Stroke()
}
} }
r.MoveTo(lx, canvasBox.Bottom)
r.LineTo(lx, canvasBox.Top)
r.Stroke()
for _, t := range ticks {
v := t.Value
ly := canvasBox.Bottom - ra.Translate(v)
tb := r.MeasureText(t.Label)
finalTextX := tx
finalTextY := ly + tb.Height>>1
if ya.AxisType == YAxisSecondary {
finalTextX = tx - tb.Width
}
r.Text(t.Label, finalTextX, finalTextY)
r.MoveTo(lx, ly)
if ya.AxisType == YAxisPrimary {
r.LineTo(lx+DefaultHorizontalTickWidth, ly)
} else if ya.AxisType == YAxisSecondary {
r.LineTo(lx-DefaultHorizontalTickWidth, ly)
}
r.Stroke()
}
if ya.Zero.Style.Show {
ya.Zero.Render(r, canvasBox, ra)
}
} }