can rotate text + add y axis names

This commit is contained in:
Will Charczuk 2016-08-06 21:59:46 -07:00
parent 3607d732d9
commit 718678b421
34 changed files with 102 additions and 20 deletions

View file

@ -8,7 +8,9 @@ import (
// YAxis is a veritcal rule of the range.
// There can be (2) y-axes; a primary and secondary.
type YAxis struct {
Name string
Name string
NameStyle Style
Style Style
Zero GridLine
@ -30,6 +32,11 @@ func (ya YAxis) GetName() string {
return ya.Name
}
// GetNameStyle returns the name style.
func (ya YAxis) GetNameStyle() Style {
return ya.NameStyle
}
// GetStyle returns the style.
func (ya YAxis) GetStyle() Style {
return ya.Style
@ -73,6 +80,7 @@ func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, tic
}
var minx, maxx, miny, maxy = math.MaxInt32, 0, math.MaxInt32, 0
var maxTextHeight int
for _, t := range ticks {
v := t.Value
ly := canvasBox.Bottom - ra.Translate(v)
@ -83,6 +91,10 @@ func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, tic
finalTextX = tx - tb.Width()
}
if tb.Height() > maxTextHeight {
maxTextHeight = tb.Height()
}
if ya.AxisType == YAxisPrimary {
minx = canvasBox.Right
maxx = Math.MaxInt(maxx, tx+tb.Width())
@ -94,6 +106,10 @@ func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, tic
maxy = Math.MaxInt(maxy, ly+tb.Height()>>1)
}
if ya.NameStyle.Show && len(ya.Name) > 0 {
maxx += (DefaultYAxisMargin + maxTextHeight)
}
return Box{
Top: miny,
Left: minx,
@ -124,12 +140,17 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick
r.LineTo(lx, canvasBox.Top)
r.Stroke()
var maxTextWidth int
for _, t := range ticks {
v := t.Value
ly := canvasBox.Bottom - ra.Translate(v)
tb := r.MeasureText(t.Label)
if tb.Width() > maxTextWidth {
maxTextWidth = tb.Width()
}
finalTextX := tx
finalTextY := ly + tb.Height()>>1
if ya.AxisType == YAxisSecondary {
@ -147,8 +168,18 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick
r.Stroke()
}
if ya.Zero.Style.Show {
ya.Zero.Render(r, canvasBox, ra, Style{})
nameStyle := ya.NameStyle.InheritFrom(defaults)
if ya.NameStyle.Show && len(ya.Name) > 0 {
nameStyle.GetTextOptions().WriteToRenderer(r)
r.SetTextRotation(Math.DegreesToRadians(90))
tb := r.MeasureText(ya.Name)
tx := canvasBox.Right + int(sw) + DefaultYAxisMargin + maxTextWidth + DefaultYAxisMargin
ty := canvasBox.Bottom - (canvasBox.Height()>>1 + tb.Width()>>1)
r.Text(ya.Name, tx, ty)
r.ClearTextRotation()
}
if ya.GridMajorStyle.Show || ya.GridMinorStyle.Show {