Skip to content

Commit

Permalink
coverage at 63.8%
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Charczuk committed Jul 13, 2016
1 parent ee34d53 commit 989aa59
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 21 deletions.
6 changes: 3 additions & 3 deletions chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ func (c Chart) hasAxes() bool {

func (c Chart) getAxesTicks(r Renderer, xr, yr, yar Range, xf, yf, yfa ValueFormatter) (xticks, yticks, yticksAlt []Tick) {
if c.XAxis.Style.Show {
xticks = c.XAxis.GetTicks(r, xr, xf)
xticks = c.XAxis.GetTicks(r, xr, c.styleDefaultsAxis(), xf)
}
if c.YAxis.Style.Show {
yticks = c.YAxis.GetTicks(r, yr, yf)
yticks = c.YAxis.GetTicks(r, yr, c.styleDefaultsAxis(), yf)
}
if c.YAxisSecondary.Style.Show {
yticksAlt = c.YAxisSecondary.GetTicks(r, yar, yf)
yticksAlt = c.YAxisSecondary.GetTicks(r, yar, c.styleDefaultsAxis(), yfa)
}
return
}
Expand Down
118 changes: 117 additions & 1 deletion chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,123 @@ func TestChartGetRanges(t *testing.T) {
assert.Equal(19.7, yra2.Max)
}

// TestChartSingleSeries is more of a sanity check than anything.
func TestChartGetDefaultCanvasBox(t *testing.T) {
assert := assert.New(t)

c := Chart{}
canvasBoxDefault := c.getDefaultCanvasBox()
assert.False(canvasBoxDefault.IsZero())
assert.Equal(DefaultBackgroundPadding.Top, canvasBoxDefault.Top)
assert.Equal(DefaultBackgroundPadding.Left, canvasBoxDefault.Left)
assert.Equal(c.GetWidth()-DefaultBackgroundPadding.Right, canvasBoxDefault.Right)
assert.Equal(c.GetHeight()-DefaultBackgroundPadding.Bottom, canvasBoxDefault.Bottom)

custom := Chart{
Background: Style{
Padding: Box{
Top: DefaultBackgroundPadding.Top + 1,
Left: DefaultBackgroundPadding.Left + 1,
Right: DefaultBackgroundPadding.Right + 1,
Bottom: DefaultBackgroundPadding.Bottom + 1,
},
},
}
canvasBoxCustom := custom.getDefaultCanvasBox()
assert.False(canvasBoxCustom.IsZero())
assert.Equal(DefaultBackgroundPadding.Top+1, canvasBoxCustom.Top)
assert.Equal(DefaultBackgroundPadding.Left+1, canvasBoxCustom.Left)
assert.Equal(c.GetWidth()-(DefaultBackgroundPadding.Right+1), canvasBoxCustom.Right)
assert.Equal(c.GetHeight()-(DefaultBackgroundPadding.Bottom+1), canvasBoxCustom.Bottom)
}

func TestChartGetValueFormatters(t *testing.T) {
assert := assert.New(t)

c := Chart{
Series: []Series{
ContinuousSeries{
XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
},
ContinuousSeries{
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{-2.1, -1.0, 0, 1.0, 2.0},
},
ContinuousSeries{
YAxis: YAxisSecondary,
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{10.0, 11.0, 12.0, 13.0, 14.0},
},
},
}

dxf, dyf, dyaf := c.getValueFormatters()
assert.NotNil(dxf)
assert.NotNil(dyf)
assert.NotNil(dyaf)
}

func TestChartHasAxes(t *testing.T) {
assert := assert.New(t)

assert.False(Chart{}.hasAxes())

x := Chart{
XAxis: XAxis{
Style: Style{
Show: true,
},
},
}
assert.True(x.hasAxes())

y := Chart{
YAxis: YAxis{
Style: Style{
Show: true,
},
},
}
assert.True(y.hasAxes())

ya := Chart{
YAxisSecondary: YAxis{
Style: Style{
Show: true,
},
},
}
assert.True(ya.hasAxes())
}

func TestChartGetAxesTicks(t *testing.T) {
assert := assert.New(t)

r, err := PNG(1024, 1024)
assert.Nil(err)

c := Chart{
XAxis: XAxis{
Style: Style{Show: true},
Range: Range{Min: 9.8, Max: 19.8},
},
YAxis: YAxis{
Style: Style{Show: true},
Range: Range{Min: 9.9, Max: 19.9},
},
YAxisSecondary: YAxis{
Style: Style{Show: true},
Range: Range{Min: 9.7, Max: 19.7},
},
}
xr, yr, yar := c.getRanges()

xt, yt, yat := c.getAxesTicks(r, xr, yr, yar, FloatValueFormatter, FloatValueFormatter, FloatValueFormatter)
assert.NotEmpty(xt)
assert.NotEmpty(yt)
assert.NotEmpty(yat)
}

func TestChartSingleSeries(t *testing.T) {
assert := assert.New(t)
now := time.Now()
Expand Down
18 changes: 9 additions & 9 deletions xaxis.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ func (xa XAxis) GetStyle() 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 {
func (xa XAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
if len(xa.Ticks) > 0 {
return xa.Ticks
}
return xa.generateTicks(r, ra, vf)
return xa.generateTicks(r, ra, defaults, vf)
}

func (xa XAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
step := xa.getTickStep(r, ra, vf)
func (xa XAxis) generateTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
step := xa.getTickStep(r, ra, defaults, vf)
return GenerateTicksWithStep(ra, step, vf)
}

func (xa XAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
tickCount := xa.getTickCount(r, ra, vf)
func (xa XAxis) getTickStep(r Renderer, ra Range, defaults Style, vf ValueFormatter) float64 {
tickCount := xa.getTickCount(r, ra, defaults, vf)
step := ra.Delta() / float64(tickCount)
return step
}

func (xa XAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
fontSize := xa.Style.GetFontSize(DefaultFontSize)
r.SetFontSize(fontSize)
func (xa XAxis) getTickCount(r Renderer, ra Range, defaults Style, vf ValueFormatter) int {
r.SetFont(xa.Style.GetFont(defaults.GetFont()))
r.SetFontSize(xa.Style.GetFontSize(defaults.GetFontSize(DefaultFontSize)))

// take a cut at determining the 'widest' value.
l0 := vf(ra.Min)
Expand Down
89 changes: 89 additions & 0 deletions xaxis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package chart

import (
"testing"

"github.com/blendlabs/go-assert"
)

func TestXAxisGetTickCount(t *testing.T) {
assert := assert.New(t)

r, err := PNG(1024, 1024)
assert.Nil(err)

f, err := GetDefaultFont()
assert.Nil(err)

xa := XAxis{}
xr := Range{Min: 10, Max: 100, Domain: 1024}
styleDefaults := Style{
Font: f,
FontSize: 10.0,
}
vf := FloatValueFormatter
count := xa.getTickCount(r, xr, styleDefaults, vf)
assert.Equal(16, count)
}

func TestXAxisGetTickStep(t *testing.T) {
assert := assert.New(t)

r, err := PNG(1024, 1024)
assert.Nil(err)

f, err := GetDefaultFont()
assert.Nil(err)

xa := XAxis{}
xr := Range{Min: 10, Max: 100, Domain: 1024}
styleDefaults := Style{
Font: f,
FontSize: 10.0,
}
vf := FloatValueFormatter
step := xa.getTickStep(r, xr, styleDefaults, vf)
assert.Equal(xr.Delta()/16.0, step)
}

func TestXAxisGetTicks(t *testing.T) {
assert := assert.New(t)

r, err := PNG(1024, 1024)
assert.Nil(err)

f, err := GetDefaultFont()
assert.Nil(err)

xa := XAxis{}
xr := Range{Min: 10, Max: 100, Domain: 1024}
styleDefaults := Style{
Font: f,
FontSize: 10.0,
}
vf := FloatValueFormatter
ticks := xa.GetTicks(r, xr, styleDefaults, vf)
assert.Len(ticks, 17)
}

func TestXAxisGetTicksWithUserDefaults(t *testing.T) {
assert := assert.New(t)

r, err := PNG(1024, 1024)
assert.Nil(err)

f, err := GetDefaultFont()
assert.Nil(err)

xa := XAxis{
Ticks: []Tick{Tick{Value: 1.0, Label: "1.0"}},
}
xr := Range{Min: 10, Max: 100, Domain: 1024}
styleDefaults := Style{
Font: f,
FontSize: 10.0,
}
vf := FloatValueFormatter
ticks := xa.GetTicks(r, xr, styleDefaults, vf)
assert.Len(ticks, 1)
}
18 changes: 10 additions & 8 deletions yaxis.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,32 @@ func (ya YAxis) GetStyle() Style {

// GetTicks returns the ticks for a series. It coalesces between user provided ticks and
// generated ticks.
func (ya YAxis) GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
func (ya YAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
var ticks []Tick
if len(ya.Ticks) > 0 {
ticks = ya.Ticks
} else {
ticks = ya.generateTicks(r, ra, vf)
ticks = ya.generateTicks(r, ra, defaults, vf)
}

return ticks
}

func (ya YAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
step := ya.getTickStep(r, ra, vf)
func (ya YAxis) generateTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
step := ya.getTickStep(r, ra, defaults, vf)
ticks := GenerateTicksWithStep(ra, step, vf)
return ticks
}

func (ya YAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
tickCount := ya.getTickCount(r, ra, vf)
func (ya YAxis) getTickStep(r Renderer, ra Range, defaults Style, vf ValueFormatter) float64 {
tickCount := ya.getTickCount(r, ra, defaults, vf)
step := ra.Delta() / float64(tickCount)
return step
}

func (ya YAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
func (ya YAxis) getTickCount(r Renderer, ra Range, defaults Style, vf ValueFormatter) int {
r.SetFont(ya.Style.GetFont(defaults.GetFont()))
r.SetFontSize(ya.Style.GetFontSize(defaults.GetFontSize(DefaultFontSize)))
//given the domain, figure out how many ticks we can draw ...
label := vf(ra.Min)
tb := r.MeasureText(label)
Expand Down Expand Up @@ -127,7 +129,7 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick
r.SetStrokeWidth(ya.Style.GetStrokeWidth(defaults.StrokeWidth))
r.SetFont(ya.Style.GetFont(defaults.GetFont()))
r.SetFontColor(ya.Style.GetFontColor(DefaultAxisColor))
r.SetFontSize(ya.Style.GetFontSize(defaults.GetFontSize()))
r.SetFontSize(ya.Style.GetFontSize(defaults.GetFontSize(DefaultFontSize)))

sort.Sort(Ticks(ticks))

Expand Down
Loading

0 comments on commit 989aa59

Please sign in to comment.