Documentation
¶
Overview ¶
datarange represents a range bounded by two values low and high. Stepsize property allows Boundaries to be rounded and to calculate steps.
Index ¶
- func FormatData(f float64, stepsize float64) (str string)
- type DataRange
- func (dr DataRange) Delta() float64
- func (pdr *DataRange) Enlarge(coef float64)
- func (thisdr DataRange) Equal(another DataRange) bool
- func (thisdr DataRange) High() float64
- func (thisdr DataRange) Low() float64
- func (dr DataRange) Progress(val float64) float64
- func (pdr *DataRange) ResetBoundaries(a float64, b float64)
- func (thisdr DataRange) StepSize() float64
- func (dr DataRange) Steps() uint
- func (thisdr DataRange) String() string
- func (thisdr DataRange) Unit() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DataRange ¶
type DataRange struct {
// contains filtered or unexported fields
}
A DataRange bounded with two float64 values, rounded at stepsize level and expressed in a spcecific unit.
Use the DataRange factory to create a new one
Example ¶
// this example illustrate how to build an automatic scale appart from a dataset
// sample dataset
datasetMinValue := 25.56
datasetMaxValue := 10850.12
// build a range, with an automatic stepsize to get a max of 10 steps on the scale
dr := Make(datasetMinValue, datasetMaxValue, -10, "amount")
fmt.Printf("dataset = %v\n", dr)
// step througth the datarange
fmt.Println("scale")
for y := dr.Low(); y <= dr.High(); y += dr.StepSize() {
fmt.Printf(" %s\n", FormatData(y, dr.StepSize()))
}
Output: dataset = amount[ 0 :2500: 12500 ] scale 0 2500 5000 7500 10000 12500
func Make ¶
Returns a new Datarange, with boudaries rounded at stepsize level:
- if stepsize > 1 then stepsize is the rounding number of digit.
- if stepsize < 1 then stepsize is the rounding number of decimals.
- if stepsize == 0 then do not round
- if stepsize <= 0 then its absolute value is considered as the maxsteps and the stepsize is calculated automatically
The calculated StepSize is a power of 1.0 2.5 and 5.0, for example 100 250 500 5000 50000 or 0.25 0.1 ¶
lowboudaries and highboundaries are automatically determined according to a and b values
lowboundary is rounded down, highboundary is rounded up
Example ¶
type sample struct {
low float64
high float64
maxsteps float64
}
samples := []sample{{0, 10, 10}, {3, 10, 10}, {5, 10, 10}, {8, 10, 10}, {9, 10, 10}, {1000, 65000, 10}, {12456, 45789, 20}, {9925, 10401, 10}}
for _, s := range samples {
dr := Make(s.low, s.high, -s.maxsteps, "amount")
fmt.Printf("range[%v %v]/%v --> %v with %v intervals\n", s.low, s.high, s.maxsteps, dr.String(), dr.Steps())
}
Output: range[0 10]/10 --> amount[ 0 :1: 10 ] with 10 intervals range[3 10]/10 --> amount[ 3 :1: 10 ] with 7 intervals range[5 10]/10 --> amount[ 5.0 :0.5: 10.0 ] with 10 intervals range[8 10]/10 --> amount[ 8.00 :0.25: 10.00 ] with 8 intervals range[9 10]/10 --> amount[ 9.0 :0.1: 10.0 ] with 10 intervals range[1000 65000]/10 --> amount[ 0 :10000: 70000 ] with 7 intervals range[12456 45789]/20 --> amount[ 10000 :2500: 47500 ] with 15 intervals range[9925 10401]/10 --> amount[ 9900 :100: 10500 ] with 6 intervals
func (DataRange) Delta ¶
Delta returns the value betwwen boundaries. Returns 0 if HighBoundary == LowBoundary. Rteurns <0 if HighBoundary is lower than LowBoundary.
func (*DataRange) Enlarge ¶
Enlarge the datarange with boundaries extended by the coef. if coef is <1 then boudaries are reduced. if coef <= 0 then the boundaries are reset to 0
func (DataRange) Equal ¶
Equal checks if 2 ranges have the sames boundaries, the same stepsize, and the same unit
func (DataRange) Progress ¶
Progress returns the rate of val within the datarange, compared with the LowBoundary Return 0 if val is equal or under the lowboundary Return 1 if val is equal or greater the highboundary Return 0.5 if the datarange is composed of a single value and val is that value
func (*DataRange) ResetBoundaries ¶
ResetBoundaries sets new boundaries without changing the stepsize.
lowBoundaries and highBoundaries are automatically determined according to a and b values
lowBoundary is rounded down, highBoundary is rounded up
func (DataRange) Steps ¶
Returns the number of steps between the boundaries according to the stepsize.
returns +Inf if thists.stepsize == 0
Example ¶
var drs [8]DataRange
drs[0] = Make(1, 10, 1, "meter")
drs[1] = Make(-10, 10, 0.1, "meter")
drs[2] = Make(1.245, 2.4, 0, "meter")
drs[3] = Make(12448, 548983, 25, "meter")
drs[4] = Make(1, 2, 10, "meter")
drs[5] = Make(1.5, 1.5, 1, "meter")
drs[6] = Make(10, 10, 10, "meter")
drs[7] = Make(19421.8139685769, 20402.658509423058, -10.0, "test")
var stri string
for _, dr := range drs {
ui := dr.Steps()
if ui == uint(math.Inf(1)) {
stri = "infinite"
} else {
stri = fmt.Sprintf("%d", ui)
}
fmt.Printf("%s, intervals=%s\n", dr, stri)
}
Output: meter[ 1 :1: 10 ], intervals=9 meter[ -10.0 :0.1: 10.0 ], intervals=200 meter[ 1.245 :0: 2.4 ], intervals=infinite meter[ 12425 :25: 549000 ], intervals=21463 meter[ 0 :10: 10 ], intervals=1 meter[ 1 :1: 2 ], intervals=1 meter[ 10 :10: 10 ], intervals=0 meter[ 19250 :250: 20500 ], intervals=5