2016-07-10 04:11:47 -04:00
|
|
|
package chart
|
|
|
|
|
2016-07-10 13:43:04 -04:00
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2016-07-10 04:11:47 -04:00
|
|
|
// YAxis is a veritcal rule of the range.
|
|
|
|
// There can be (2) y-axes; a primary and secondary.
|
|
|
|
type YAxis struct {
|
2016-07-11 21:48:51 -04:00
|
|
|
Name string
|
|
|
|
Style Style
|
|
|
|
|
|
|
|
Zero GridLine
|
|
|
|
|
|
|
|
AxisType YAxisType
|
|
|
|
|
2016-07-10 13:43:04 -04:00
|
|
|
ValueFormatter ValueFormatter
|
|
|
|
Range Range
|
|
|
|
Ticks []Tick
|
2016-07-12 22:14:14 -04:00
|
|
|
|
|
|
|
GridLines []GridLine
|
|
|
|
GridMajorStyle Style
|
|
|
|
GridMinorStyle Style
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-07-11 21:48:51 -04:00
|
|
|
var ticks []Tick
|
2016-07-10 13:43:04 -04:00
|
|
|
if len(ya.Ticks) > 0 {
|
2016-07-11 21:48:51 -04:00
|
|
|
ticks = ya.Ticks
|
|
|
|
} else {
|
|
|
|
ticks = ya.generateTicks(r, ra, vf)
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
2016-07-11 21:48:51 -04:00
|
|
|
|
|
|
|
return ticks
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ya YAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
|
|
|
|
step := ya.getTickStep(r, ra, vf)
|
2016-07-11 21:48:51 -04:00
|
|
|
ticks := GenerateTicksWithStep(ra, step, vf)
|
|
|
|
return ticks
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ya YAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
|
|
|
|
tickCount := ya.getTickCount(r, ra, vf)
|
2016-07-11 21:48:51 -04:00
|
|
|
step := ra.Delta() / float64(tickCount)
|
|
|
|
return step
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
|
|
|
|
2016-07-11 02:06:14 -04:00
|
|
|
func (ya YAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
|
2016-07-12 02:43:22 -04:00
|
|
|
//given the domain, figure out how many ticks we can draw ...
|
|
|
|
label := vf(ra.Min)
|
|
|
|
tb := r.MeasureText(label)
|
2016-07-12 19:47:52 -04:00
|
|
|
return int(math.Ceil(float64(ra.Domain) / float64(tb.Height()+DefaultMinimumTickVerticalSpacing)))
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
|
|
|
|
2016-07-12 22:14:14 -04:00
|
|
|
// GetGridLines returns the gridlines for the axis.
|
|
|
|
func (ya YAxis) GetGridLines(ticks []Tick) []GridLine {
|
|
|
|
if len(ya.GridLines) > 0 {
|
|
|
|
return ya.GridLines
|
|
|
|
}
|
2016-07-12 22:22:02 -04:00
|
|
|
return GenerateGridLines(ticks, false)
|
2016-07-12 22:14:14 -04:00
|
|
|
}
|
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
// Measure returns the bounds of the axis.
|
2016-07-12 23:34:59 -04:00
|
|
|
func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box {
|
|
|
|
r.SetStrokeColor(ya.Style.GetStrokeColor(defaults.StrokeColor))
|
|
|
|
r.SetStrokeWidth(ya.Style.GetStrokeWidth(defaults.StrokeWidth))
|
|
|
|
r.SetFont(ya.Style.GetFont(defaults.GetFont()))
|
|
|
|
r.SetFontColor(ya.Style.GetFontColor(DefaultAxisColor))
|
|
|
|
r.SetFontSize(ya.Style.GetFontSize(defaults.GetFontSize()))
|
2016-07-11 21:48:51 -04:00
|
|
|
|
|
|
|
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 {
|
2016-07-12 19:47:52 -04:00
|
|
|
finalTextX = tx - tb.Width()
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ya.AxisType == YAxisPrimary {
|
|
|
|
minx = canvasBox.Right
|
2016-07-12 19:47:52 -04:00
|
|
|
maxx = MaxInt(maxx, tx+tb.Width())
|
2016-07-11 21:48:51 -04:00
|
|
|
} else if ya.AxisType == YAxisSecondary {
|
|
|
|
minx = MinInt(minx, finalTextX)
|
|
|
|
maxx = MaxInt(maxx, tx)
|
|
|
|
}
|
2016-07-12 19:47:52 -04:00
|
|
|
miny = MinInt(miny, ly-tb.Height()>>1)
|
|
|
|
maxy = MaxInt(maxy, ly+tb.Height()>>1)
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Box{
|
|
|
|
Top: miny,
|
|
|
|
Left: minx,
|
|
|
|
Right: maxx,
|
|
|
|
Bottom: maxy,
|
|
|
|
}
|
2016-07-10 04:11:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders the axis.
|
2016-07-12 23:34:59 -04:00
|
|
|
func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) {
|
|
|
|
r.SetStrokeColor(ya.Style.GetStrokeColor(defaults.StrokeColor))
|
|
|
|
r.SetStrokeWidth(ya.Style.GetStrokeWidth(defaults.StrokeWidth))
|
|
|
|
r.SetFont(ya.Style.GetFont(defaults.GetFont()))
|
|
|
|
r.SetFontColor(ya.Style.GetFontColor(DefaultAxisColor))
|
|
|
|
r.SetFontSize(ya.Style.GetFontSize(defaults.GetFontSize()))
|
2016-07-10 14:19:56 -04:00
|
|
|
|
|
|
|
sort.Sort(Ticks(ticks))
|
|
|
|
|
2016-07-10 13:43:04 -04:00
|
|
|
var lx int
|
2016-07-10 04:11:47 -04:00
|
|
|
var tx int
|
2016-07-11 21:48:51 -04:00
|
|
|
if ya.AxisType == YAxisPrimary {
|
2016-07-10 13:43:04 -04:00
|
|
|
lx = canvasBox.Right
|
2016-07-12 02:32:31 -04:00
|
|
|
tx = lx + DefaultYAxisMargin
|
2016-07-11 21:48:51 -04:00
|
|
|
} else if ya.AxisType == YAxisSecondary {
|
2016-07-10 13:43:04 -04:00
|
|
|
lx = canvasBox.Left
|
2016-07-12 02:32:31 -04:00
|
|
|
tx = lx - DefaultYAxisMargin
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
2016-07-10 04:11:47 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
r.MoveTo(lx, canvasBox.Bottom)
|
|
|
|
r.LineTo(lx, canvasBox.Top)
|
|
|
|
r.Stroke()
|
2016-07-10 13:43:04 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
for _, t := range ticks {
|
|
|
|
v := t.Value
|
|
|
|
ly := canvasBox.Bottom - ra.Translate(v)
|
2016-07-10 04:11:47 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
tb := r.MeasureText(t.Label)
|
2016-07-10 13:43:04 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
finalTextX := tx
|
2016-07-12 19:47:52 -04:00
|
|
|
finalTextY := ly + tb.Height()>>1
|
2016-07-11 21:48:51 -04:00
|
|
|
if ya.AxisType == YAxisSecondary {
|
2016-07-12 19:47:52 -04:00
|
|
|
finalTextX = tx - tb.Width()
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
2016-07-10 13:43:04 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
r.Text(t.Label, finalTextX, finalTextY)
|
2016-07-10 13:43:04 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
r.MoveTo(lx, ly)
|
|
|
|
if ya.AxisType == YAxisPrimary {
|
|
|
|
r.LineTo(lx+DefaultHorizontalTickWidth, ly)
|
|
|
|
} else if ya.AxisType == YAxisSecondary {
|
|
|
|
r.LineTo(lx-DefaultHorizontalTickWidth, ly)
|
2016-07-10 14:19:56 -04:00
|
|
|
}
|
2016-07-11 21:48:51 -04:00
|
|
|
r.Stroke()
|
2016-07-10 04:11:47 -04:00
|
|
|
}
|
2016-07-10 14:19:56 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
if ya.Zero.Style.Show {
|
|
|
|
ya.Zero.Render(r, canvasBox, ra)
|
|
|
|
}
|
2016-07-12 22:14:14 -04:00
|
|
|
|
|
|
|
if ya.GridMajorStyle.Show || ya.GridMinorStyle.Show {
|
|
|
|
for _, gl := range ya.GetGridLines(ticks) {
|
|
|
|
if (gl.IsMinor && ya.GridMinorStyle.Show) ||
|
|
|
|
(!gl.IsMinor && ya.GridMajorStyle.Show) {
|
|
|
|
gl.Render(r, canvasBox, ra)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-10 04:11:47 -04:00
|
|
|
}
|