mostly works.
This commit is contained in:
parent
e9a36274ac
commit
abfdc3e0d9
14 changed files with 465 additions and 159 deletions
88
yaxis.go
88
yaxis.go
|
|
@ -1,28 +1,102 @@
|
|||
package chart
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// YAxis is a veritcal rule of the range.
|
||||
// There can be (2) y-axes; a primary and secondary.
|
||||
type YAxis struct {
|
||||
axis
|
||||
Name string
|
||||
Style Style
|
||||
ValueFormatter ValueFormatter
|
||||
Range Range
|
||||
Ticks []Tick
|
||||
}
|
||||
|
||||
// GetName returns the name.
|
||||
func (ya YAxis) GetName() string {
|
||||
return ya.Name
|
||||
}
|
||||
|
||||
// GetStyle returns the style.
|
||||
func (ya YAxis) GetStyle() Style {
|
||||
return ya.Style
|
||||
}
|
||||
|
||||
// GetTicks returns the ticks for a series. It coalesces between user provided ticks and
|
||||
// generated ticks.
|
||||
func (ya YAxis) GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
|
||||
if len(ya.Ticks) > 0 {
|
||||
return ya.Ticks
|
||||
}
|
||||
return ya.generateTicks(r, ra, vf)
|
||||
}
|
||||
|
||||
func (ya YAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
|
||||
step := ya.getTickStep(r, ra, vf)
|
||||
return ya.generateTicksWithStep(ra, step, vf)
|
||||
}
|
||||
|
||||
func (ya YAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
|
||||
textHeight := int(ya.Style.GetFontSize(DefaultFontSize))
|
||||
height := textHeight + DefaultMinimumTickVerticalSpacing
|
||||
count := int(math.Ceil(float64(ra.Domain) / float64(height)))
|
||||
return count
|
||||
}
|
||||
|
||||
func (ya YAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
|
||||
tickCount := ya.getTickCount(r, ra, vf)
|
||||
return ra.Delta() / float64(tickCount)
|
||||
}
|
||||
|
||||
func (ya YAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick {
|
||||
var ticks []Tick
|
||||
for cursor := ra.Min; cursor < ra.Max; cursor += step {
|
||||
ticks = append(ticks, Tick{
|
||||
Value: cursor,
|
||||
Label: vf(cursor),
|
||||
})
|
||||
}
|
||||
return ticks
|
||||
}
|
||||
|
||||
// Render renders the axis.
|
||||
func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType) {
|
||||
func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType, ticks []Tick) {
|
||||
var lx int
|
||||
var tx int
|
||||
if axisType == YAxisPrimary {
|
||||
lx = canvasBox.Right
|
||||
tx = canvasBox.Right + DefaultYAxisMargin
|
||||
} else if axisType == YAxisSecondary {
|
||||
lx = canvasBox.Left
|
||||
tx = canvasBox.Left - DefaultYAxisMargin
|
||||
}
|
||||
|
||||
r.SetFontColor(ya.Style.GetFontColor(DefaultAxisColor))
|
||||
r.SetFontSize(ya.Style.GetFontSize(DefaultFontSize))
|
||||
r.SetStrokeColor(ya.Style.GetStrokeColor(DefaultAxisColor))
|
||||
r.SetStrokeWidth(ya.Style.GetStrokeWidth(DefaultAxisLineWidth))
|
||||
|
||||
ticks := ya.getTicks(ra)
|
||||
r.MoveTo(lx, canvasBox.Bottom)
|
||||
r.LineTo(lx, canvasBox.Top)
|
||||
r.Stroke()
|
||||
|
||||
r.SetFontColor(ya.Style.GetFontColor(DefaultAxisColor))
|
||||
fontSize := ya.Style.GetFontSize(DefaultFontSize)
|
||||
r.SetFontSize(fontSize)
|
||||
|
||||
sort.Sort(Ticks(ticks))
|
||||
for _, t := range ticks {
|
||||
v := t.Value
|
||||
y := ra.Translate(v)
|
||||
ty := int(y)
|
||||
ly := ra.Translate(v) + canvasBox.Top
|
||||
|
||||
th := int(fontSize) >> 1
|
||||
ty := ly + th
|
||||
|
||||
r.Text(t.Label, tx, ty)
|
||||
|
||||
r.MoveTo(lx, ly)
|
||||
r.LineTo(lx+DefaultVerticalTickWidth, ly)
|
||||
r.Stroke()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue