can just supply inverted ranges.

This commit is contained in:
Will Charczuk 2017-01-09 17:57:45 -08:00
parent 21986dbce7
commit 78cbfa62bc
7 changed files with 66 additions and 29 deletions

View file

@ -25,12 +25,15 @@ const (
// Axis is a chart feature detailing what values happen where. // Axis is a chart feature detailing what values happen where.
type Axis interface { type Axis interface {
GetName() string GetName() string
SetName(name string)
GetStyle() Style GetStyle() Style
SetStyle(style Style)
GetTicks() []Tick GetTicks() []Tick
GenerateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick GenerateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick
// GetGridLines returns the gridlines for the axis. // GenerateGridLines returns the gridlines for the axis.
GetGridLines(ticks []Tick) []GridLine GetGridLines(ticks []Tick) []GridLine
// Measure should return an absolute box for the axis. // Measure should return an absolute box for the axis.

View file

@ -56,7 +56,7 @@ func (r *ContinuousRange) SetDomain(domain int) {
// String returns a simple string for the ContinuousRange. // String returns a simple string for the ContinuousRange.
func (r ContinuousRange) String() string { func (r ContinuousRange) String() string {
return fmt.Sprintf("ContinuousRange [%.2f,%.2f] => %d", r.Min, r.Max, r.Domain) return fmt.Sprintf("ContinuousRange [%.2f,%.2f] => %f", r.Min, r.Max, r.Domain)
} }
// Translate maps a given value into the ContinuousRange space. // Translate maps a given value into the ContinuousRange space.

View file

@ -20,7 +20,7 @@ type Stringable interface {
String() string String() string
} }
// Range is a // Range is a common interface for a range of values.
type Range interface { type Range interface {
Stringable Stringable
IsZeroable IsZeroable

11
tick.go
View file

@ -49,17 +49,18 @@ func GenerateContinuousTicks(r Renderer, ra Range, isVertical bool, style Style,
style.GetTextOptions().WriteToRenderer(r) style.GetTextOptions().WriteToRenderer(r)
labelBox := r.MeasureText(minLabel) labelBox := r.MeasureText(minLabel)
var tickSize int var tickSize float64
if isVertical { if isVertical {
tickSize = labelBox.Height() + DefaultMinimumTickVerticalSpacing tickSize = float64(labelBox.Height() + DefaultMinimumTickVerticalSpacing)
} else { } else {
tickSize = labelBox.Width() + DefaultMinimumTickHorizontalSpacing tickSize = float64(labelBox.Width() + DefaultMinimumTickHorizontalSpacing)
} }
domainRemainder := (ra.GetDomain()) - (tickSize * 2) domain := float64(ra.GetDomain())
domainRemainder := domain - (tickSize * 2)
intermediateTickCount := int(math.Floor(float64(domainRemainder) / float64(tickSize))) intermediateTickCount := int(math.Floor(float64(domainRemainder) / float64(tickSize)))
rangeDelta := max - min rangeDelta := math.Abs(max - min)
tickStep := rangeDelta / float64(intermediateTickCount) tickStep := rangeDelta / float64(intermediateTickCount)
roundTo := Math.GetRoundToForDelta(rangeDelta) / 10 roundTo := Math.GetRoundToForDelta(rangeDelta) / 10

View file

@ -1,12 +1,57 @@
package chart package chart
/* import (
func TestGenerateTicksWithStep(t *testing.T) { "testing"
assert "github.com/blendlabs/go-assert"
)
func TestGenerateContinuousTicks(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
f, err := GetDefaultFont()
assert.Nil(err)
r, err := PNG(1024, 1024)
assert.Nil(err)
r.SetFont(f)
ticks := GenerateContinuousTicksWithStep(&ContinuousRange{Min: 1.0, Max: 10.0, Domain: 100}, 1.0, FloatValueFormatter, false) ra := &ContinuousRange{
assert.Len(ticks, 10) Min: 0.0,
Max: 10.0,
Domain: 256,
}
vf := FloatValueFormatter
ticks := GenerateContinuousTicks(r, ra, false, Style{}, vf)
assert.NotEmpty(ticks)
assert.Len(ticks, 11)
assert.Equal(0.0, ticks[0].Value)
assert.Equal(10, ticks[len(ticks)-1].Value)
}
func TestGenerateContinuousTicksDescending(t *testing.T) {
assert := assert.New(t)
f, err := GetDefaultFont()
assert.Nil(err)
r, err := PNG(1024, 1024)
assert.Nil(err)
r.SetFont(f)
ra := &ContinuousRange{
Min: 10.0,
Max: 0.0,
Domain: 256,
}
vf := FloatValueFormatter
ticks := GenerateContinuousTicks(r, ra, false, Style{}, vf)
assert.NotEmpty(ticks)
assert.Len(ticks, 11)
assert.Equal(10.0, ticks[0].Value)
assert.Equal(0.0, ticks[len(ticks)-1].Value)
} }
*/

View file

@ -1,9 +1,6 @@
package chart package chart
import ( import "math"
"math"
"sort"
)
// XAxis represents the horizontal axis. // XAxis represents the horizontal axis.
type XAxis struct { type XAxis struct {
@ -71,7 +68,6 @@ func (xa XAxis) GetGridLines(ticks []Tick) []GridLine {
// Measure returns the bounds of the axis. // Measure returns the bounds of the axis.
func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box { func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box {
tickStyle := xa.TickStyle.InheritFrom(xa.Style.InheritFrom(defaults)) tickStyle := xa.TickStyle.InheritFrom(xa.Style.InheritFrom(defaults))
sort.Sort(Ticks(ticks))
tp := xa.GetTickPosition() tp := xa.GetTickPosition()
@ -124,8 +120,6 @@ func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick
r.LineTo(canvasBox.Right, canvasBox.Bottom) r.LineTo(canvasBox.Right, canvasBox.Bottom)
r.Stroke() r.Stroke()
sort.Sort(Ticks(ticks))
tp := xa.GetTickPosition() tp := xa.GetTickPosition()
var tx, ty int var tx, ty int

View file

@ -1,9 +1,6 @@
package chart package chart
import ( import "math"
"math"
"sort"
)
// YAxis is a veritcal rule of the range. // YAxis is a veritcal rule of the range.
// There can be (2) y-axes; a primary and secondary. // There can be (2) y-axes; a primary and secondary.
@ -16,6 +13,7 @@ type YAxis struct {
Zero GridLine Zero GridLine
AxisType YAxisType AxisType YAxisType
Ascending bool
ValueFormatter ValueFormatter ValueFormatter ValueFormatter
Range Range Range Range
@ -74,8 +72,6 @@ func (ya YAxis) GetGridLines(ticks []Tick) []GridLine {
// Measure returns the bounds of the axis. // Measure returns the bounds of the axis.
func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box { func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box {
sort.Sort(Ticks(ticks))
var tx int var tx int
if ya.AxisType == YAxisPrimary { if ya.AxisType == YAxisPrimary {
tx = canvasBox.Right + DefaultYAxisMargin tx = canvasBox.Right + DefaultYAxisMargin
@ -128,8 +124,6 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick
tickStyle := ya.TickStyle.InheritFrom(ya.Style.InheritFrom(defaults)) tickStyle := ya.TickStyle.InheritFrom(ya.Style.InheritFrom(defaults))
tickStyle.WriteToRenderer(r) tickStyle.WriteToRenderer(r)
sort.Sort(Ticks(ticks))
sw := tickStyle.GetStrokeWidth(defaults.StrokeWidth) sw := tickStyle.GetStrokeWidth(defaults.StrokeWidth)
var lx int var lx int