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
|
|
|
|
Style Style
|
|
|
|
ValueFormatter ValueFormatter
|
|
|
|
Range Range
|
|
|
|
Ticks []Tick
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTicks returns the ticks for a series. It coalesces between user provided ticks and
|
|
|
|
// generated ticks.
|
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 {
|
|
|
|
return tp.GetTicks(vf)
|
2016-07-10 19:43:04 +02:00
|
|
|
}
|
2016-07-24 00:35:49 +02:00
|
|
|
step := CalculateContinuousTickStep(r, ra, false, xa.Style.InheritFrom(defaults), vf)
|
|
|
|
return GenerateContinuousTicksWithStep(ra, step, 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-21 23:11:27 +02:00
|
|
|
xa.Style.InheritFrom(defaults).PersistToRenderer(r)
|
2016-07-12 03:48:51 +02: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
|
2016-07-13 01:47:52 +02:00
|
|
|
ty := canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
|
2016-07-12 03:48:51 +02:00
|
|
|
|
|
|
|
top = MinInt(top, canvasBox.Bottom)
|
2016-07-13 01:47:52 +02:00
|
|
|
left = MinInt(left, tx-(tb.Width()>>1))
|
|
|
|
right = MaxInt(right, tx+(tb.Width()>>1))
|
2016-07-12 03:48:51 +02:00
|
|
|
bottom = MaxInt(bottom, ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Box{
|
|
|
|
Top: top,
|
|
|
|
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-21 23:11:27 +02:00
|
|
|
xa.Style.InheritFrom(defaults).PersistToRenderer(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-10 10:11:47 +02:00
|
|
|
for _, t := range ticks {
|
|
|
|
v := t.Value
|
2016-07-12 03:48:51 +02:00
|
|
|
lx := ra.Translate(v)
|
|
|
|
tb := r.MeasureText(t.Label)
|
|
|
|
tx := canvasBox.Left + lx
|
2016-07-13 01:47:52 +02:00
|
|
|
ty := canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
|
|
|
|
r.Text(t.Label, tx-tb.Width()>>1, ty)
|
2016-07-12 03:48:51 +02:00
|
|
|
|
|
|
|
r.MoveTo(tx, canvasBox.Bottom)
|
|
|
|
r.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
|
|
|
|
r.Stroke()
|
2016-07-10 10:11:47 +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
|
|
|
|
}
|
|
|
|
gl.Render(r, canvasBox, ra, defaults)
|
2016-07-13 04:14:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|