Debian Codegraph @

continuous_range.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 "math"
6 )
7
8 // ContinuousRange represents a boundary for a set of numbers.
9 type ContinuousRange struct {
10 Min float64
11 Max float64
12 Domain int
13 Descending bool
14 }
15
16 // IsDescending returns if the range is descending.
17 func (r ContinuousRange) IsDescending() bool {
18 return r.Descending
19 }
20
21 // IsZero returns if the ContinuousRange has been set or not.
22 func (r ContinuousRange) IsZero() bool {
23 return (r.Min == 0 || math.IsNaN(r.Min)) &&
24 (r.Max == 0 || math.IsNaN(r.Max)) &&
25 r.Domain == 0
26 }
27
28 // GetMin gets the min value for the continuous range.
29 func (r ContinuousRange) GetMin() float64 {
30 return r.Min
31 }
32
33 // SetMin sets the min value for the continuous range.
34 func (r *ContinuousRange) SetMin(min float64) {
35 r.Min = min
36 }
37
38 // GetMax returns the max value for the continuous range.
39 func (r ContinuousRange) GetMax() float64 {
40 return r.Max
41 }
42
43 // SetMax sets the max value for the continuous range.
44 func (r *ContinuousRange) SetMax(max float64) {
45 r.Max = max
46 }
47
48 // GetDelta returns the difference between the min and max value.
49 func (r ContinuousRange) GetDelta() float64 {
50 return r.Max - r.Min
51 }
52
53 // GetDomain returns the range domain.
54 func (r ContinuousRange) GetDomain() int {
55 return r.Domain
56 }
57
58 // SetDomain sets the range domain.
59 func (r *ContinuousRange) SetDomain(domain int) {
60 r.Domain = domain
61 }
62
63 // String returns a simple string for the ContinuousRange.
64 func (r ContinuousRange) String() string {
65 if r.GetDelta() == 0 {
66 return "ContinuousRange [empty]"
67 }
68 return fmt.Sprintf("ContinuousRange [%.2f,%.2f] => %d", r.Min, r.Max, r.Domain)
69 }
70
71 // Translate maps a given value into the ContinuousRange space.
72 func (r ContinuousRange) Translate(value float64) int {
73 normalized := value - r.Min
74 ratio := normalized / r.GetDelta()
75
76 if r.IsDescending() {
77 return r.Domain - int(math.Ceil(ratio*float64(r.Domain)))
78 }
79
80 return int(math.Ceil(ratio * float64(r.Domain)))
81 }