datarange

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2024 License: MIT Imports: 4 Imported by: 1

README

datarange package in go

DataRange represents a range bounded by a low and a high value.

Go Reference

stepsize property allows stepping througth the datarange. If stepsize is defined then boundaries are rounded at stepsize level to ensure an integer number of steps.

stepsize can be automatically calculated according to a requested maximum number of steps. 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 are calculated stepsize.

This is very usefull to build axis scale on a chart for example.

Usage

A datarange must be created with a factory

    // simple data range with values from 0 to 10 meters, without stepsize
	dr1 := Make(0, 10, 0, "meter")
    // simple data range with values from 0 to 10 meters, with 10 steps of 1 meter
	dr2 := Make(0, 10, 1, "meter")
    // data range with values from 1 to 10 meters, leaving the factory calculating the best stepsize to get a maximum number of 20 steps
	dr3 := Make(0, 10, -20, "meter")

see examples in the DataRange package documentation

Installing

go get -u github.com/sunraylab/datarange@latest

Changelog

  • v1.2.0: migration to larry868 & go v1.23
  • v1.1.0: fix some bugs and rename func Build to Make to follow go naming guidelines
  • v1.0.0: first release

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatData

func FormatData(f float64, stepsize float64) (str string)

FormatData f according to a stepsize

If stepsize == 0 then f is formatted without trailing zeros

Example

FormatData(11.2, 0.1) == "11.2"
FormatData(11.2, 1) == "11"
FormatData(11.2, 5) == "10"

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

func Make(a float64, b float64, stepsize float64, unit string) DataRange

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

func (dr DataRange) Delta() float64

Delta returns the value betwwen boundaries. Returns 0 if HighBoundary == LowBoundary. Rteurns <0 if HighBoundary is lower than LowBoundary.

func (*DataRange) Enlarge

func (pdr *DataRange) Enlarge(coef float64)

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

func (thisdr DataRange) Equal(another DataRange) bool

Equal checks if 2 ranges have the sames boundaries, the same stepsize, and the same unit

func (DataRange) High

func (thisdr DataRange) High() float64

func (DataRange) Low

func (thisdr DataRange) Low() float64

func (DataRange) Progress

func (dr DataRange) Progress(val float64) float64

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

func (pdr *DataRange) ResetBoundaries(a float64, b float64)

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) StepSize

func (thisdr DataRange) StepSize() float64

func (DataRange) Steps

func (dr DataRange) Steps() uint

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

func (DataRange) String

func (thisdr DataRange) String() string

default string formating: "unit[ low :stepsize: high ]"

func (DataRange) Unit

func (thisdr DataRange) Unit() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL