go-chart/xaxis.go

204 lines
5.5 KiB
Go
Raw Normal View History

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