go-chart/xaxis.go

191 lines
5.0 KiB
Go
Raw Normal View History

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