introduces the Range interface (instead of a concrete type).

This commit is contained in:
Will Charczuk 2016-07-21 14:11:27 -07:00
parent 8af50213c3
commit b0934ee2e3
27 changed files with 331 additions and 172 deletions

View file

@ -1,44 +1,41 @@
package chart
import (
"fmt"
"math"
)
// Range represents a boundary for a set of numbers.
type Range struct {
Min float64
Max float64
Domain int
// NameProvider is a type that returns a name.
type NameProvider interface {
GetName() string
}
// IsZero returns if the range has been set or not.
func (r Range) IsZero() bool {
return (r.Min == 0 || math.IsNaN(r.Min)) &&
(r.Max == 0 || math.IsNaN(r.Max)) &&
r.Domain == 0
// StyleProvider is a type that returns a style.
type StyleProvider interface {
GetStyle() Style
}
// Delta returns the difference between the min and max value.
func (r Range) Delta() float64 {
return r.Max - r.Min
// IsZeroable is a type that returns if it's been set or not.
type IsZeroable interface {
IsZero() bool
}
// String returns a simple string for the range.
func (r Range) String() string {
return fmt.Sprintf("Range [%.2f,%.2f] => %d", r.Min, r.Max, r.Domain)
// Stringable is a type that has a string representation.
type Stringable interface {
String() string
}
// Translate maps a given value into the range space.
func (r Range) Translate(value float64) int {
normalized := value - r.Min
ratio := normalized / r.Delta()
return int(math.Ceil(ratio * float64(r.Domain)))
}
// Range is a
type Range interface {
Stringable
IsZeroable
// GetRoundedRangeBounds returns some `prettified` range bounds.
func (r Range) GetRoundedRangeBounds() (min, max float64) {
delta := r.Max - r.Min
roundTo := GetRoundToForDelta(delta)
return RoundDown(r.Min, roundTo), RoundUp(r.Max, roundTo)
GetMin() float64
SetMin(min float64)
GetMax() float64
SetMax(max float64)
GetDelta() float64
GetDomain() int
SetDomain(domain int)
// Translate the range to the domain.
Translate(value float64) int
}