Debian Codegraph @

logarithmic_range.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 "math"
6 )
7
8 // LogarithmicRange represents a boundary for a set of numbers.
9 type LogarithmicRange 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 LogarithmicRange) IsDescending() bool {
18 return r.Descending
19 }
20
21 // IsZero returns if the LogarithmicRange has been set or not.
22 func (r LogarithmicRange) 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 LogarithmicRange) GetMin() float64 {
30 return r.Min
31 }
32
33 // SetMin sets the min value for the continuous range.
34 func (r *LogarithmicRange) SetMin(min float64) {
35 r.Min = min
36 }
37
38 // GetMax returns the max value for the continuous range.
39 func (r LogarithmicRange) GetMax() float64 {
40 return r.Max
41 }
42
43 // SetMax sets the max value for the continuous range.
44 func (r *LogarithmicRange) SetMax(max float64) {
45 r.Max = max
46 }
47
48 // GetDelta returns the difference between the min and max value.
49 func (r LogarithmicRange) GetDelta() float64 {
50 return r.Max - r.Min
51 }
52
53 // GetDomain returns the range domain.
54 func (r LogarithmicRange) GetDomain() int {
55 return r.Domain
56 }
57
58 // SetDomain sets the range domain.
59 func (r *LogarithmicRange) SetDomain(domain int) {
60 r.Domain = domain
61 }
62
63 // String returns a simple string for the LogarithmicRange.
64 func (r LogarithmicRange) String() string {
65 return fmt.Sprintf("LogarithmicRange [%.2f,%.2f] => %d", r.Min, r.Max, r.Domain)
66 }
67
68 // Translate maps a given value into the LogarithmicRange space. Modified version from ContinuousRange.
69 func (r LogarithmicRange) Translate(value float64) int {
70 if value < 1 {
71 return 0
72 }
73 normalized := math.Max(value-r.Min, 1)
74 ratio := math.Log10(normalized) / math.Log10(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 }
82
83 // GetTicks calculates the needed ticks for the axis, in log scale. Only supports Y values > 0.
84 func (r LogarithmicRange) GetTicks(render Renderer, defaults Style, vf ValueFormatter) []Tick {
85 var ticks []Tick
86 exponentStart := int64(math.Max(0, math.Floor(math.Log10(r.Min)))) // one below min
87 exponentEnd := int64(math.Max(0, math.Ceil(math.Log10(r.Max)))) // one above max
88 for exp:=exponentStart; exp<=exponentEnd; exp++ {
89 tickVal := math.Pow(10, float64(exp))
90 ticks = append(ticks, Tick{Value: tickVal, Label: vf(tickVal)})
91 }
92
93 return ticks
94 }