2016-07-06 21:54:00 -04:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
2016-07-07 17:44:03 -04:00
|
|
|
"fmt"
|
2016-07-06 21:54:00 -04:00
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2016-07-10 04:11:47 -04:00
|
|
|
// Range represents a boundary for a set of numbers.
|
2016-07-07 17:44:03 -04:00
|
|
|
type Range struct {
|
2016-07-10 04:11:47 -04:00
|
|
|
Min float64
|
|
|
|
Max float64
|
|
|
|
Domain int
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
|
|
|
|
2016-07-07 17:44:03 -04:00
|
|
|
// IsZero returns if the range has been set or not.
|
|
|
|
func (r Range) IsZero() bool {
|
|
|
|
return r.Min == 0 && r.Max == 0 && r.Domain == 0
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
|
|
|
|
2016-07-07 17:44:03 -04:00
|
|
|
// Delta returns the difference between the min and max value.
|
|
|
|
func (r Range) Delta() float64 {
|
|
|
|
return r.Max - r.Min
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
|
|
|
|
2016-07-07 17:44:03 -04:00
|
|
|
// 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)
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate maps a given value into the range space.
|
2016-07-07 17:44:03 -04:00
|
|
|
func (r Range) Translate(value float64) int {
|
2016-07-11 21:48:51 -04:00
|
|
|
normalized := value - r.Min
|
|
|
|
ratio := normalized / r.Delta()
|
|
|
|
return int(math.Ceil(ratio * float64(r.Domain)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|