go-chart/xaxis.go

173 lines
4 KiB
Go
Raw Normal View History

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
Style Style
ValueFormatter ValueFormatter
Range Range
Ticks []Tick
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
}
// GetTicks returns the ticks for a series. It coalesces between user provided ticks and
// generated ticks.
func (xa XAxis) GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
if len(xa.Ticks) > 0 {
return xa.Ticks
}
return xa.generateTicks(r, ra, vf)
}
func (xa XAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
step := xa.getTickStep(r, ra, vf)
2016-07-11 02:06:14 -04:00
return GenerateTicksWithStep(ra, step, vf)
}
func (xa XAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
tickCount := xa.getTickCount(r, ra, vf)
step := ra.Delta() / float64(tickCount)
return step
2016-07-10 13:43:04 -04:00
}
func (xa XAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
fontSize := xa.Style.GetFontSize(DefaultFontSize)
r.SetFontSize(fontSize)
// take a cut at determining the 'widest' value.
l0 := vf(ra.Min)
ln := vf(ra.Max)
ll := l0
if len(ln) > len(l0) {
ll = ln
}
2016-07-11 21:48:51 -04:00
llb := r.MeasureText(ll)
textWidth := llb.Width()
2016-07-10 13:43:04 -04:00
width := textWidth + DefaultMinimumTickHorizontalSpacing
count := int(math.Ceil(float64(ra.Domain) / float64(width)))
return count
}
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
}
return xa.generateGridLines(ticks)
}
func (xa XAxis) generateGridLines(ticks []Tick) []GridLine {
var gl []GridLine
isMinor := false
minorStyle := Style{
StrokeColor: DefaultGridLineColor.WithAlpha(100),
StrokeWidth: 1.0,
}
majorStyle := Style{
StrokeColor: DefaultGridLineColor,
StrokeWidth: 1.0,
}
for _, t := range ticks {
s := majorStyle
if isMinor {
s = minorStyle
}
gl = append(gl, GridLine{
Style: s,
IsMinor: isMinor,
IsVertical: true,
Value: t.Value,
})
isMinor = !isMinor
}
return gl
}
2016-07-11 21:48:51 -04:00
// Measure returns the bounds of the axis.
func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, ticks []Tick) Box {
defaultFont, _ := GetDefaultFont()
r.SetFont(xa.Style.GetFont(defaultFont))
r.SetFontSize(xa.Style.GetFontSize(DefaultFontSize))
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
ty := canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
2016-07-11 21:48:51 -04:00
top = MinInt(top, canvasBox.Bottom)
left = MinInt(left, tx-(tb.Width()>>1))
right = MaxInt(right, tx+(tb.Width()>>1))
2016-07-11 21:48:51 -04:00
bottom = MaxInt(bottom, ty)
}
return Box{
Top: top,
Left: left,
Right: right,
Bottom: bottom,
}
}
2016-07-10 04:11:47 -04:00
// Render renders the axis
2016-07-10 13:43:04 -04:00
func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, ticks []Tick) {
2016-07-10 04:11:47 -04:00
tickFontSize := xa.Style.GetFontSize(DefaultFontSize)
2016-07-10 13:43:04 -04:00
r.SetStrokeColor(xa.Style.GetStrokeColor(DefaultAxisColor))
r.SetStrokeWidth(xa.Style.GetStrokeWidth(DefaultAxisLineWidth))
r.MoveTo(canvasBox.Left, canvasBox.Bottom)
r.LineTo(canvasBox.Right, canvasBox.Bottom)
r.Stroke()
2016-07-10 04:11:47 -04:00
r.SetFontColor(xa.Style.GetFontColor(DefaultAxisColor))
r.SetFontSize(tickFontSize)
2016-07-10 13:43:04 -04:00
sort.Sort(Ticks(ticks))
2016-07-11 03:02:31 -04:00
2016-07-10 04:11:47 -04:00
for _, t := range ticks {
v := t.Value
2016-07-11 21:48:51 -04:00
lx := ra.Translate(v)
tb := r.MeasureText(t.Label)
tx := canvasBox.Left + lx
ty := canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
r.Text(t.Label, tx-tb.Width()>>1, ty)
2016-07-11 21:48:51 -04:00
r.MoveTo(tx, canvasBox.Bottom)
r.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
r.Stroke()
2016-07-10 04:11:47 -04:00
}
2016-07-12 22:14:14 -04:00
if xa.GridMajorStyle.Show || xa.GridMinorStyle.Show {
for _, gl := range xa.GetGridLines(ticks) {
if (gl.IsMinor && xa.GridMinorStyle.Show) ||
(!gl.IsMinor && xa.GridMajorStyle.Show) {
gl.Render(r, canvasBox, ra)
}
}
}
2016-07-10 04:11:47 -04:00
}