-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathlinear.rs
More file actions
154 lines (133 loc) · 4.29 KB
/
Copy pathlinear.rs
File metadata and controls
154 lines (133 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::scales::{Scale, ScaleType};
use std::cmp::{max, Ordering};
/// The scale to represent categorical data.
#[derive(Debug, Clone)]
pub struct ScaleLinear {
/// The domain limits of the dataset that the scale is going to represent.
pub domain: Vec<f32>,
/// The range limits of the drawable area on the chart.
pub range: Vec<isize>,
/// The amount of ticks to display.
pub tick_count: usize,
}
impl ScaleLinear {
/// Create a new linear scale with default values.
pub fn new() -> Self {
Self {
domain: Vec::new(),
range: vec![0, 1],
tick_count: 10,
}
}
/// Set the domain limits for the scale band.
pub fn set_domain(mut self, range: Vec<f32>) -> Self {
self.domain = range;
self
}
/// Get the domain limits of the scale.
pub fn domain(&self) -> &Vec<f32> {
&self.domain
}
/// Set the range limits for the scale band.
pub fn set_range(mut self, range: Vec<isize>) -> Self {
self.range = range;
self
}
/// Get the range limits of the scale.
pub fn range(&self) -> &Vec<isize> {
&self.range
}
/// Takes a value x in [a, b] and returns the corresponding value in [0, 1].
fn normalize(&self, a: f32, b: f32, x: f32) -> f32 {
// If a == b then return 0.5
if a == b {
0.5
} else {
let b = b - a;
(x - a as f32) / b as f32
}
}
/// Takes a value t in [0, 1] and returns the corresponding range in [a, b].
fn interpolate(&self, a: f32, b: f32, t: f32) -> f32 {
(b - a) * t + a
}
/// Compute the distance between the ticks.
fn tick_step(&self, start: f32, stop: f32) -> f32 {
let e10 = 50_f32.sqrt();
let e5 = 10_f32.sqrt();
let e2 = 2_f32.sqrt();
let step = (stop - start) / max(0, self.tick_count) as f32;
let power = (step.ln() / 10_f32.ln()).trunc() as i32;
let error = step / 10_f32.powi(power);
let dynamic = if error >= e10 {
10
} else if error >= e5 {
5
} else if error >= e2 {
2
} else {
1
};
let step = match power.cmp(&0) {
Ordering::Less => -10_f32.powi(-power) / dynamic as f32,
_ => dynamic as f32 * 10_f32.powi(power),
};
step
}
}
impl Scale<f32> for ScaleLinear {
/// Get the type of the scale.
fn get_type(&self) -> ScaleType {
ScaleType::Linear
}
/// Get the range value for the given domain entry.
fn scale(&self, domain: &f32) -> f32 {
let a = self.domain[0];
let b = self.domain[1];
let normalized = self.normalize(a, b, *domain);
let a = self.range[0] as f32;
let b = self.range[1] as f32;
let scaled = self.interpolate(a, b, normalized);
scaled
}
/// Get the bandwidth (if present).
fn bandwidth(&self) -> Option<f32> {
Some(0_f32)
}
/// Get the start range value.
fn range_start(&self) -> f32 {
self.range[0] as f32
}
/// Get the end range value.
fn range_end(&self) -> f32 {
self.range[1] as f32
}
/// Get the list of ticks that represent the scale on a chart axis.
fn get_ticks(&self) -> Vec<f32> {
let mut ticks = Vec::new();
if self.domain[0] == self.domain[1] && self.tick_count > 0 {
ticks.push(self.domain[0] as f32);
return ticks;
}
let step = self.tick_step(self.domain[0] as f32, self.domain[1] as f32);
let mut i = 0;
if step > 0_f32 {
let start = (self.domain[0] as f32 / step).ceil();
let stop = (self.domain[1] as f32 / step).floor();
let nr_of_ticks = (stop - start + 1_f32).ceil() as i32;
while i < nr_of_ticks {
ticks.push((start + i as f32) * step);
i += 1;
}
} else {
let start = (self.domain[0] as f32 * step).floor();
let stop = (self.domain[1] as f32 * step).ceil();
let nr_of_ticks = (start - stop + 1_f32).ceil() as i32;
while i < nr_of_ticks {
ticks.push((start - i as f32) / step);
i += 1;
}
}
ticks
}
}