go-chart/xaxis.go

204 lines
5.5 KiB
Go
Raw Normal View History

2016-07-10 04:11:47 -04:00
package chart
import (
"math"
util "github.com/wcharczuk/go-chart/util"
)
2016-07-10 04:11:47 -04:00
// XAxis represents the horizontal axis.
type XAxis struct {
2016-09-05 16:26:12 -04:00
Name string
NameStyle Style
2016-07-10 13:43:04 -04:00
Style Style
ValueFormatter ValueFormatter
Range Range
2016-07-12 22:14:14 -04:00
2016-09-05 16:26:12 -04:00
TickStyle Style
Ticks []Tick
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
}
2017-04-26 02:35:07 -04:00
// GetValueFormatter returns the value formatter for the axis.
func (xa XAxis) GetValueFormatter() ValueFormatter {
if xa.ValueFormatter != nil {
return xa.ValueFormatter
}
return FloatValueFormatter
}
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-08-11 23:42:25 -04:00
return GenerateGridLines(ticks, xa.GridMajorStyle, xa.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 (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box {
2016-10-21 15:44:37 -04:00
tickStyle := xa.TickStyle.InheritFrom(xa.Style.InheritFrom(defaults))
2016-07-11 21:48:51 -04:00
2016-07-31 00:34:41 -04:00
tp := xa.GetTickPosition()
2016-10-21 15:44:37 -04:00
var ltx, rtx int
var tx, ty int
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-10-21 15:44:37 -04:00
tb := Draw.MeasureText(r, t.Label, tickStyle.GetTextOptions())
2016-07-11 21:48:51 -04:00
2016-10-21 15:44:37 -04:00
tx = canvasBox.Left + ra.Translate(v)
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
left = util.Math.MinInt(left, ltx)
right = util.Math.MaxInt(right, rtx)
bottom = util.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 {
2016-10-21 15:44:37 -04:00
tb := Draw.MeasureText(r, xa.Name, xa.NameStyle.InheritFrom(defaults))
2016-08-07 01:27:26 -04:00
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-10-21 15:44:37 -04:00
tickStyle := xa.TickStyle.InheritFrom(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()
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-10-21 15:44:37 -04:00
tickWithAxisStyle := xa.TickStyle.InheritFrom(xa.Style.InheritFrom(defaults))
tb := Draw.MeasureText(r, t.Label, tickWithAxisStyle)
2016-07-30 13:01:16 -04:00
2016-07-30 12:12:03 -04:00
switch tp {
2016-07-30 13:01:16 -04:00
case TickPositionUnderTick, TickPositionUnset:
2016-10-21 15:44:37 -04:00
if tickStyle.TextRotationDegrees == 0 {
tx = tx - tb.Width()>>1
ty = canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
} else {
ty = canvasBox.Bottom + (1.5 * DefaultXAxisMargin)
2016-10-21 15:44:37 -04:00
}
Draw.Text(r, t.Label, tx, ty, tickWithAxisStyle)
maxTextHeight = util.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-10-21 15:44:37 -04:00
finalTickStyle := tickWithAxisStyle.InheritFrom(Style{TextHorizontalAlign: TextHorizontalAlignCenter})
2016-10-21 15:50:40 -04:00
2016-07-30 12:12:03 -04:00
Draw.TextWithin(r, t.Label, Box{
Left: ltx,
Right: tx,
Top: canvasBox.Bottom + DefaultXAxisMargin,
2016-10-21 15:44:37 -04:00
Bottom: canvasBox.Bottom + DefaultXAxisMargin,
2016-08-07 01:27:26 -04:00
}, finalTickStyle)
ftb := Text.MeasureLines(r, Text.WrapFit(r, t.Label, tx-ltx, finalTickStyle), finalTickStyle)
maxTextHeight = util.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 {
2016-10-21 15:44:37 -04:00
tb := Draw.MeasureText(r, xa.Name, nameStyle)
2016-08-07 01:27:26 -04:00
tx := canvasBox.Right - (canvasBox.Width()>>1 + tb.Width()>>1)
ty := canvasBox.Bottom + DefaultXAxisMargin + maxTextHeight + DefaultXAxisMargin + tb.Height()
2016-10-21 15:44:37 -04:00
Draw.Text(r, xa.Name, tx, ty, nameStyle)
2016-08-07 01:27:26 -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
}
2016-08-11 23:42:25 -04:00
gl.Render(r, canvasBox, ra, true, gl.Style.InheritFrom(defaults))
2016-07-12 22:14:14 -04:00
}
}
}
2016-07-10 04:11:47 -04:00
}