go-chart/range.go

48 lines
1.1 KiB
Go
Raw Normal View History

2016-07-07 03:54:00 +02:00
package chart
import (
2016-07-07 23:44:03 +02:00
"fmt"
2016-07-07 03:54:00 +02:00
"math"
2016-07-08 05:26:07 +02:00
"github.com/blendlabs/go-util"
2016-07-07 03:54:00 +02:00
)
2016-07-07 23:44:03 +02:00
// Range represents a continuous range,
type Range struct {
2016-07-08 05:26:07 +02:00
Min float64
Max float64
Domain int
Formatter Formatter
2016-07-07 03:54:00 +02:00
}
2016-07-07 23:44:03 +02: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-07 03:54:00 +02:00
}
2016-07-07 23:44:03 +02:00
// Delta returns the difference between the min and max value.
func (r Range) Delta() float64 {
return r.Max - r.Min
2016-07-07 03:54:00 +02:00
}
2016-07-07 23:44:03 +02: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-07 03:54:00 +02:00
}
2016-07-08 05:26:07 +02:00
// Format formats the value based on the range's formatter.
func (r Range) Format(v interface{}) string {
if r.Formatter != nil {
return r.Formatter(v)
}
return util.StringEmpty
}
2016-07-07 03:54:00 +02:00
// Translate maps a given value into the range space.
// An example would be a 600 px image, with a min of 10 and a max of 100.
// Translate(50) would yield (50.0/90.0)*600 ~= 333.33
2016-07-07 23:44:03 +02:00
func (r Range) Translate(value float64) int {
finalValue := ((r.Max - value) / r.Delta()) * float64(r.Domain)
return int(math.Floor(finalValue))
2016-07-07 03:54:00 +02:00
}