go-chart/xaxis.go

157 lines
3.9 KiB
Go
Raw Normal View History

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
// XAxis represents the horizontal axis.
type XAxis struct {
2016-07-10 13:43:04 -04:00
Name string
Style Style
ValueFormatter ValueFormatter
Range Range
Ticks []Tick
2016-07-12 22:14:14 -04:00
2016-07-30 12:12:03 -04:00
TickPosition tickPosition
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 (xa XAxis) GetName() string {
return xa.Name
}
// GetStyle returns the style.
func (xa XAxis) GetStyle() Style {
return xa.Style
}
2016-07-30 12:12:03 -04:00
// GetTickPosition returns the tick position option for the axis.
func (xa XAxis) GetTickPosition(defaults ...tickPosition) tickPosition {
if xa.TickPosition == TickPositionUnset {
if len(defaults) > 0 {
return defaults[0]
}
return TickPositionUnderTick
}
return xa.TickPosition
}
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 (xa XAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
2016-07-10 13:43:04 -04:00
if len(xa.Ticks) > 0 {
return xa.Ticks
}
2016-07-23 18:35:49 -04:00
if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
return tp.GetTicks(vf)
2016-07-10 13:43:04 -04:00
}
2016-07-30 15:57:18 -04:00
tickStyle := xa.Style.InheritFrom(defaults)
return GenerateContinuousTicks(r, ra, false, tickStyle, vf)
2016-07-10 13:43:04 -04:00
}
2016-07-12 22:14:14 -04:00
// GetGridLines returns the gridlines for the axis.
func (xa XAxis) GetGridLines(ticks []Tick) []GridLine {
if len(xa.GridLines) > 0 {
return xa.GridLines
}
2016-07-24 23:27:19 -04:00
return GenerateGridLines(ticks, xa.GridMajorStyle, xa.GridMinorStyle, true)
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 (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box {
2016-07-29 19:36:29 -04:00
xa.Style.InheritFrom(defaults).WriteToRenderer(r)
2016-07-11 21:48:51 -04:00
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()
2016-07-11 21:48:51 -04:00
2016-07-29 21:24:25 -04:00
top = Math.MinInt(top, canvasBox.Bottom)
left = Math.MinInt(left, tx-(tb.Width()>>1))
right = Math.MaxInt(right, tx+(tb.Width()>>1))
bottom = Math.MaxInt(bottom, ty)
2016-07-11 21:48:51 -04:00
}
return Box{
Top: top,
Left: left,
Right: right,
Bottom: bottom,
}
}
2016-07-10 04:11:47 -04:00
// Render renders the axis
2016-07-12 23:34:59 -04:00
func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) {
2016-07-30 12:12:03 -04:00
tickStyle := xa.Style.InheritFrom(defaults)
2016-07-10 13:43:04 -04:00
2016-07-30 12:12:03 -04:00
tickStyle.GetStrokeOptions().WriteToRenderer(r)
2016-07-10 13:43:04 -04:00
r.MoveTo(canvasBox.Left, canvasBox.Bottom)
r.LineTo(canvasBox.Right, canvasBox.Bottom)
r.Stroke()
sort.Sort(Ticks(ticks))
2016-07-11 03:02:31 -04:00
2016-07-30 12:12:03 -04:00
tp := xa.GetTickPosition()
var tx, ty int
for index, t := range ticks {
2016-07-10 04:11:47 -04:00
v := t.Value
2016-07-11 21:48:51 -04:00
lx := ra.Translate(v)
2016-07-30 12:12:03 -04:00
tx = canvasBox.Left + lx
tickStyle.GetStrokeOptions().WriteToRenderer(r)
2016-07-11 21:48:51 -04:00
r.MoveTo(tx, canvasBox.Bottom)
r.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
r.Stroke()
2016-07-30 12:12:03 -04:00
2016-07-30 13:01:16 -04:00
tickStyle.GetTextOptions().WriteToRenderer(r)
tb := r.MeasureText(t.Label)
2016-07-30 12:12:03 -04:00
switch tp {
2016-07-30 13:01:16 -04:00
case TickPositionUnderTick, TickPositionUnset:
ty = canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
2016-07-30 12:12:03 -04:00
r.Text(t.Label, tx-tb.Width()>>1, ty)
2016-07-30 13:01:16 -04:00
break
2016-07-30 12:12:03 -04:00
case TickPositionBetweenTicks:
if index > 0 {
llx := ra.Translate(ticks[index-1].Value)
ltx := canvasBox.Left + llx
Draw.TextWithin(r, t.Label, Box{
Left: ltx,
Right: tx,
Top: canvasBox.Bottom + DefaultXAxisMargin,
Bottom: canvasBox.Bottom + DefaultXAxisMargin + tb.Height(),
}, tickStyle.InheritFrom(Style{TextHorizontalAlign: TextHorizontalAlignCenter}))
}
2016-07-30 13:01:16 -04:00
break
2016-07-30 12:12:03 -04:00
}
2016-07-10 04:11:47 -04:00
}
2016-07-12 22:14:14 -04:00
if xa.GridMajorStyle.Show || xa.GridMinorStyle.Show {
for _, gl := range xa.GetGridLines(ticks) {
2016-07-24 23:27:19 -04:00
if (gl.IsMinor && xa.GridMinorStyle.Show) || (!gl.IsMinor && xa.GridMajorStyle.Show) {
defaults := xa.GridMajorStyle
if gl.IsMinor {
defaults = xa.GridMinorStyle
}
gl.Render(r, canvasBox, ra, defaults)
2016-07-12 22:14:14 -04:00
}
}
}
2016-07-10 04:11:47 -04:00
}