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
|
2016-08-07 01:27:26 -04:00
|
|
|
NameStyle Style
|
2016-07-10 13:43:04 -04:00
|
|
|
Style Style
|
|
|
|
ValueFormatter ValueFormatter
|
|
|
|
Range Range
|
|
|
|
Ticks []Tick
|
2016-07-12 22:14:14 -04:00
|
|
|
|
2016-07-31 19:54:09 -04:00
|
|
|
TickPosition TickPosition
|
2016-07-30 12:12:03 -04:00
|
|
|
|
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.
|
2016-07-31 19:54:09 -04:00
|
|
|
func (xa XAxis) GetTickPosition(defaults ...TickPosition) TickPosition {
|
2016-07-30 12:12:03 -04:00
|
|
|
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 {
|
2016-07-31 19:54:09 -04:00
|
|
|
return tp.GetTicks(r, defaults, 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-31 00:34:41 -04:00
|
|
|
tickStyle := xa.Style.InheritFrom(defaults)
|
2016-07-11 21:48:51 -04:00
|
|
|
sort.Sort(Ticks(ticks))
|
|
|
|
|
2016-07-31 00:34:41 -04:00
|
|
|
tp := xa.GetTickPosition()
|
|
|
|
|
2016-08-01 03:50:32 -04:00
|
|
|
var left, right, bottom = math.MaxInt32, 0, 0
|
2016-07-31 00:34:41 -04:00
|
|
|
for index, t := range ticks {
|
2016-07-11 21:48:51 -04:00
|
|
|
v := t.Value
|
2016-07-31 00:34:41 -04:00
|
|
|
tickStyle.GetTextOptions().WriteToRenderer(r)
|
2016-07-11 21:48:51 -04:00
|
|
|
tb := r.MeasureText(t.Label)
|
|
|
|
|
2016-07-31 00:34:41 -04:00
|
|
|
var ltx, rtx int
|
2016-08-07 02:58:44 -04:00
|
|
|
tx := canvasBox.Left + ra.Translate(v)
|
2016-07-12 19:47:52 -04:00
|
|
|
ty := canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
|
2016-07-31 00:34:41 -04:00
|
|
|
switch tp {
|
|
|
|
case TickPositionUnderTick, TickPositionUnset:
|
|
|
|
ltx = tx - tb.Width()>>1
|
|
|
|
rtx = tx + tb.Width()>>1
|
|
|
|
break
|
|
|
|
case TickPositionBetweenTicks:
|
|
|
|
if index > 0 {
|
|
|
|
ltx = ra.Translate(ticks[index-1].Value)
|
|
|
|
rtx = tx
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2016-07-11 21:48:51 -04:00
|
|
|
|
2016-07-31 00:34:41 -04:00
|
|
|
left = Math.MinInt(left, ltx)
|
|
|
|
right = Math.MaxInt(right, rtx)
|
2016-07-29 21:24:25 -04:00
|
|
|
bottom = Math.MaxInt(bottom, ty)
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
|
|
|
|
2016-08-07 01:27:26 -04:00
|
|
|
if xa.NameStyle.Show && len(xa.Name) > 0 {
|
|
|
|
tb := r.MeasureText(xa.Name)
|
|
|
|
bottom += DefaultXAxisMargin + tb.Height()
|
|
|
|
}
|
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
return Box{
|
2016-08-01 03:50:32 -04:00
|
|
|
Top: canvasBox.Bottom,
|
2016-07-11 21:48:51 -04:00
|
|
|
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
|
2016-08-07 01:27:26 -04:00
|
|
|
var maxTextHeight int
|
2016-07-30 12:12:03 -04:00
|
|
|
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-08-07 01:27:26 -04:00
|
|
|
maxTextHeight = Math.MaxInt(maxTextHeight, tb.Height())
|
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
|
2016-08-07 01:27:26 -04:00
|
|
|
finalTickStyle := tickStyle.InheritFrom(Style{TextHorizontalAlign: TextHorizontalAlignCenter})
|
2016-07-30 12:12:03 -04:00
|
|
|
Draw.TextWithin(r, t.Label, Box{
|
|
|
|
Left: ltx,
|
|
|
|
Right: tx,
|
|
|
|
Top: canvasBox.Bottom + DefaultXAxisMargin,
|
|
|
|
Bottom: canvasBox.Bottom + DefaultXAxisMargin + tb.Height(),
|
2016-08-07 01:27:26 -04:00
|
|
|
}, finalTickStyle)
|
|
|
|
|
|
|
|
ftb := Text.MeasureLines(r, Text.WrapFit(r, t.Label, tx-ltx, finalTickStyle), finalTickStyle)
|
|
|
|
maxTextHeight = Math.MaxInt(maxTextHeight, ftb.Height())
|
2016-07-30 12:12:03 -04:00
|
|
|
}
|
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
|
|
|
|
2016-08-07 01:27:26 -04:00
|
|
|
nameStyle := xa.NameStyle.InheritFrom(defaults)
|
|
|
|
if xa.NameStyle.Show && len(xa.Name) > 0 {
|
|
|
|
nameStyle.GetTextOptions().WriteToRenderer(r)
|
|
|
|
tb := r.MeasureText(xa.Name)
|
|
|
|
tx := canvasBox.Right - (canvasBox.Width()>>1 + tb.Width()>>1)
|
|
|
|
ty := canvasBox.Bottom + DefaultXAxisMargin + maxTextHeight + DefaultXAxisMargin + tb.Height()
|
|
|
|
r.Text(xa.Name, tx, ty)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2016-08-11 18:51:49 -04:00
|
|
|
gl.Render(r, canvasBox, ra, gl.Style.InheritFrom(defaults))
|
2016-07-12 22:14:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-10 04:11:47 -04:00
|
|
|
}
|