2016-07-10 04:11:47 -04:00
|
|
|
package chart
|
|
|
|
|
2017-01-09 20:57:45 -05:00
|
|
|
import "math"
|
2016-07-10 13:43:04 -04:00
|
|
|
|
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-08-07 00:59:46 -04:00
|
|
|
Name string
|
|
|
|
NameStyle Style
|
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
Style Style
|
|
|
|
|
|
|
|
Zero GridLine
|
|
|
|
|
2017-01-09 20:57:45 -05:00
|
|
|
AxisType YAxisType
|
|
|
|
Ascending bool
|
2016-07-11 21:48:51 -04:00
|
|
|
|
2016-07-10 13:43:04 -04:00
|
|
|
ValueFormatter ValueFormatter
|
|
|
|
Range Range
|
2016-07-12 22:14:14 -04:00
|
|
|
|
2016-09-01 01:11:52 -04:00
|
|
|
TickStyle Style
|
2016-07-21 17:11:27 -04:00
|
|
|
Ticks []Tick
|
|
|
|
|
2016-09-05 16:26:12 -04:00
|
|
|
GridLines []GridLine
|
2016-07-12 22:14:14 -04:00
|
|
|
GridMajorStyle Style
|
|
|
|
GridMinorStyle Style
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name.
|
|
|
|
func (ya YAxis) GetName() string {
|
|
|
|
return ya.Name
|
|
|
|
}
|
|
|
|
|
2016-08-07 00:59:46 -04:00
|
|
|
// GetNameStyle returns the name style.
|
|
|
|
func (ya YAxis) GetNameStyle() Style {
|
|
|
|
return ya.NameStyle
|
|
|
|
}
|
|
|
|
|
2016-07-10 13:43:04 -04:00
|
|
|
// GetStyle returns the style.
|
|
|
|
func (ya YAxis) GetStyle() Style {
|
|
|
|
return ya.Style
|
|
|
|
}
|
|
|
|
|
2017-04-26 02:35:07 -04:00
|
|
|
// GetValueFormatter returns the value formatter for the axis.
|
|
|
|
func (ya YAxis) GetValueFormatter() ValueFormatter {
|
|
|
|
if ya.ValueFormatter != nil {
|
|
|
|
return ya.ValueFormatter
|
|
|
|
}
|
|
|
|
return FloatValueFormatter
|
|
|
|
}
|
|
|
|
|
2016-09-01 01:11:52 -04:00
|
|
|
// GetTickStyle returns the tick style.
|
|
|
|
func (ya YAxis) GetTickStyle() Style {
|
|
|
|
return ya.TickStyle
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:34:15 -04:00
|
|
|
// GetTicks returns the ticks for a series.
|
|
|
|
// The coalesce priority is:
|
|
|
|
// - User Supplied Ticks (i.e. Ticks array on the axis itself).
|
|
|
|
// - Range ticks (i.e. if the range provides ticks).
|
|
|
|
// - Generating continuous ticks based on minimum spacing and canvas width.
|
2016-07-13 01:04:30 -04:00
|
|
|
func (ya YAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
|
2016-07-10 13:43:04 -04:00
|
|
|
if len(ya.Ticks) > 0 {
|
2016-07-13 01:55:46 -04:00
|
|
|
return ya.Ticks
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
2016-07-23 18:35:49 -04:00
|
|
|
if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
|
2016-07-31 19:54:09 -04:00
|
|
|
return tp.GetTicks(r, defaults, vf)
|
2016-07-23 18:35:49 -04:00
|
|
|
}
|
2016-07-30 15:57:18 -04:00
|
|
|
tickStyle := ya.Style.InheritFrom(defaults)
|
|
|
|
return GenerateContinuousTicks(r, ra, true, tickStyle, vf)
|
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-08-11 23:42:25 -04:00
|
|
|
return GenerateGridLines(ticks, ya.GridMajorStyle, ya.GridMinorStyle)
|
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 {
|
2016-07-11 21:48:51 -04:00
|
|
|
var tx int
|
|
|
|
if ya.AxisType == YAxisPrimary {
|
|
|
|
tx = canvasBox.Right + DefaultYAxisMargin
|
|
|
|
} else if ya.AxisType == YAxisSecondary {
|
|
|
|
tx = canvasBox.Left - DefaultYAxisMargin
|
|
|
|
}
|
|
|
|
|
2016-09-01 01:11:52 -04:00
|
|
|
ya.TickStyle.InheritFrom(ya.Style.InheritFrom(defaults)).WriteToRenderer(r)
|
2016-07-11 21:48:51 -04:00
|
|
|
var minx, maxx, miny, maxy = math.MaxInt32, 0, math.MaxInt32, 0
|
2016-08-07 00:59:46 -04:00
|
|
|
var maxTextHeight int
|
2016-07-11 21:48:51 -04:00
|
|
|
for _, t := range ticks {
|
|
|
|
v := t.Value
|
|
|
|
ly := canvasBox.Bottom - ra.Translate(v)
|
|
|
|
|
|
|
|
tb := r.MeasureText(t.Label)
|
2016-09-11 12:13:57 -04:00
|
|
|
tbh2 := tb.Height() >> 1
|
2016-07-11 21:48:51 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-11 12:13:57 -04:00
|
|
|
maxTextHeight = Math.MaxInt(tb.Height(), maxTextHeight)
|
2016-08-07 00:59:46 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
if ya.AxisType == YAxisPrimary {
|
|
|
|
minx = canvasBox.Right
|
2016-07-29 21:24:25 -04:00
|
|
|
maxx = Math.MaxInt(maxx, tx+tb.Width())
|
2016-07-11 21:48:51 -04:00
|
|
|
} else if ya.AxisType == YAxisSecondary {
|
2016-07-29 21:24:25 -04:00
|
|
|
minx = Math.MinInt(minx, finalTextX)
|
|
|
|
maxx = Math.MaxInt(maxx, tx)
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
2016-09-11 12:13:57 -04:00
|
|
|
|
|
|
|
miny = Math.MinInt(miny, ly-tbh2)
|
|
|
|
maxy = Math.MaxInt(maxy, ly+tbh2)
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
|
|
|
|
2016-08-07 00:59:46 -04:00
|
|
|
if ya.NameStyle.Show && len(ya.Name) > 0 {
|
|
|
|
maxx += (DefaultYAxisMargin + maxTextHeight)
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-10-21 15:50:40 -04:00
|
|
|
tickStyle := ya.TickStyle.InheritFrom(ya.Style.InheritFrom(defaults))
|
|
|
|
tickStyle.WriteToRenderer(r)
|
2016-07-10 14:19:56 -04:00
|
|
|
|
2016-10-21 15:50:40 -04:00
|
|
|
sw := tickStyle.GetStrokeWidth(defaults.StrokeWidth)
|
2016-07-18 20:15:24 -04:00
|
|
|
|
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-18 20:15:24 -04:00
|
|
|
lx = canvasBox.Right + int(sw)
|
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-18 20:15:24 -04:00
|
|
|
lx = canvasBox.Left - int(sw)
|
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-08-07 00:59:46 -04:00
|
|
|
var maxTextWidth int
|
2016-10-21 15:50:40 -04:00
|
|
|
var finalTextX, finalTextY int
|
2016-07-11 21:48:51 -04:00
|
|
|
for _, t := range ticks {
|
|
|
|
v := t.Value
|
|
|
|
ly := canvasBox.Bottom - ra.Translate(v)
|
2016-10-21 15:50:40 -04:00
|
|
|
|
|
|
|
tb := Draw.MeasureText(r, t.Label, tickStyle)
|
2016-07-10 13:43:04 -04:00
|
|
|
|
2016-08-07 00:59:46 -04:00
|
|
|
if tb.Width() > maxTextWidth {
|
|
|
|
maxTextWidth = tb.Width()
|
|
|
|
}
|
|
|
|
|
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-10-21 15:50:40 -04:00
|
|
|
} else {
|
|
|
|
finalTextX = tx
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
2016-07-10 13:43:04 -04:00
|
|
|
|
2016-10-21 15:50:40 -04:00
|
|
|
if tickStyle.TextRotationDegrees == 0 {
|
|
|
|
finalTextY = ly + tb.Height()>>1
|
|
|
|
} else {
|
|
|
|
finalTextY = ly
|
|
|
|
}
|
2016-09-05 16:26:12 -04:00
|
|
|
|
2016-10-21 15:50:40 -04:00
|
|
|
tickStyle.WriteToRenderer(r)
|
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-10-21 15:50:40 -04:00
|
|
|
|
|
|
|
Draw.Text(r, t.Label, finalTextX, finalTextY, tickStyle)
|
2016-07-10 04:11:47 -04:00
|
|
|
}
|
2016-07-10 14:19:56 -04:00
|
|
|
|
2016-09-01 01:11:52 -04:00
|
|
|
nameStyle := ya.NameStyle.InheritFrom(defaults.InheritFrom(Style{TextRotationDegrees: 90}))
|
2016-08-07 00:59:46 -04:00
|
|
|
if ya.NameStyle.Show && len(ya.Name) > 0 {
|
|
|
|
nameStyle.GetTextOptions().WriteToRenderer(r)
|
2016-10-21 15:44:37 -04:00
|
|
|
tb := Draw.MeasureText(r, ya.Name, nameStyle)
|
2016-08-07 01:27:26 -04:00
|
|
|
|
|
|
|
var tx int
|
|
|
|
if ya.AxisType == YAxisPrimary {
|
|
|
|
tx = canvasBox.Right + int(sw) + DefaultYAxisMargin + maxTextWidth + DefaultYAxisMargin
|
|
|
|
} else if ya.AxisType == YAxisSecondary {
|
|
|
|
tx = canvasBox.Left - (DefaultYAxisMargin + int(sw) + maxTextWidth + DefaultYAxisMargin)
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:40:27 -04:00
|
|
|
var ty int
|
|
|
|
if nameStyle.TextRotationDegrees == 0 {
|
|
|
|
ty = canvasBox.Top + (canvasBox.Height()>>1 - tb.Width()>>1)
|
|
|
|
} else {
|
|
|
|
ty = canvasBox.Top + (canvasBox.Height()>>1 - tb.Height()>>1)
|
|
|
|
}
|
2016-08-07 00:59:46 -04:00
|
|
|
|
2016-10-21 15:44:37 -04:00
|
|
|
Draw.Text(r, ya.Name, tx, ty, nameStyle)
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
2016-07-12 22:14:14 -04:00
|
|
|
|
2016-08-07 03:33:48 -04:00
|
|
|
if ya.Zero.Style.Show {
|
2016-08-11 23:42:25 -04:00
|
|
|
ya.Zero.Render(r, canvasBox, ra, false, Style{})
|
2016-08-07 03:33:48 -04:00
|
|
|
}
|
|
|
|
|
2016-07-12 22:14:14 -04:00
|
|
|
if ya.GridMajorStyle.Show || ya.GridMinorStyle.Show {
|
|
|
|
for _, gl := range ya.GetGridLines(ticks) {
|
2016-07-24 23:27:19 -04:00
|
|
|
if (gl.IsMinor && ya.GridMinorStyle.Show) || (!gl.IsMinor && ya.GridMajorStyle.Show) {
|
|
|
|
defaults := ya.GridMajorStyle
|
|
|
|
if gl.IsMinor {
|
|
|
|
defaults = ya.GridMinorStyle
|
|
|
|
}
|
2016-08-11 23:42:25 -04:00
|
|
|
gl.Render(r, canvasBox, ra, false, gl.Style.InheritFrom(defaults))
|
2016-07-12 22:14:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-10 04:11:47 -04:00
|
|
|
}
|