plot(scale): Point scale support single point (#1185)
<img width="447" height="446" alt="image" src="https://github.com/user-attachments/assets/fff44252-ff5b-4d73-8b98-0eb4c0877563" />
This commit is contained in:
parent
709a888e15
commit
1267857a8c
4 changed files with 37 additions and 8 deletions
|
|
@ -29,4 +29,4 @@
|
|||
"desktop": 214.0,
|
||||
"color_alpha": 1.0
|
||||
}
|
||||
]
|
||||
]
|
||||
|
|
@ -115,7 +115,13 @@ where
|
|||
if (i + 1) % self.tick_margin == 0 {
|
||||
x.tick(&x_fn(d)).map(|x_tick| {
|
||||
let align = match i {
|
||||
0 => TextAlign::Left,
|
||||
0 => {
|
||||
if data_len == 1 {
|
||||
TextAlign::Center
|
||||
} else {
|
||||
TextAlign::Left
|
||||
}
|
||||
}
|
||||
i if i == data_len - 1 => TextAlign::Right,
|
||||
_ => TextAlign::Center,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -107,7 +107,13 @@ where
|
|||
if (i + 1) % self.tick_margin == 0 {
|
||||
x.tick(&x_fn(d)).map(|x_tick| {
|
||||
let align = match i {
|
||||
0 => TextAlign::Left,
|
||||
0 => {
|
||||
if data_len == 1 {
|
||||
TextAlign::Center
|
||||
} else {
|
||||
TextAlign::Left
|
||||
}
|
||||
}
|
||||
i if i == data_len - 1 => TextAlign::Right,
|
||||
_ => TextAlign::Center,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -27,7 +27,14 @@ where
|
|||
.into_option()
|
||||
.map_or(0., |(min, max)| max - min);
|
||||
|
||||
(range_diff / (len - 1) as f32, range_diff / len as f32)
|
||||
if len == 1 {
|
||||
(range_diff, range_diff)
|
||||
} else {
|
||||
(
|
||||
range_diff / len.saturating_sub(1) as f32,
|
||||
range_diff / len as f32,
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
Self {
|
||||
|
|
@ -43,8 +50,12 @@ where
|
|||
T: PartialEq,
|
||||
{
|
||||
fn tick(&self, value: &T) -> Option<f32> {
|
||||
let index = self.domain.iter().position(|v| v == value)?;
|
||||
Some(index as f32 * self.range_tick)
|
||||
if self.domain.len() == 1 {
|
||||
Some(self.range_tick / 2.)
|
||||
} else {
|
||||
let index = self.domain.iter().position(|v| v == value)?;
|
||||
Some(index as f32 * self.range_tick)
|
||||
}
|
||||
}
|
||||
|
||||
fn least_index(&self, tick: f32) -> usize {
|
||||
|
|
@ -62,7 +73,7 @@ mod tests {
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_scale_point_1() {
|
||||
fn test_scale_point() {
|
||||
let scale = ScalePoint::new(vec![1, 2, 3], vec![0., 100.]);
|
||||
assert_eq!(scale.tick(&1), Some(0.));
|
||||
assert_eq!(scale.tick(&2), Some(50.));
|
||||
|
|
@ -70,7 +81,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_scale_point_2() {
|
||||
fn test_scale_point_empty() {
|
||||
let scale = ScalePoint::new(vec![], vec![0., 100.]);
|
||||
assert_eq!(scale.tick(&1), None);
|
||||
assert_eq!(scale.tick(&2), None);
|
||||
|
|
@ -81,4 +92,10 @@ mod tests {
|
|||
assert_eq!(scale.tick(&2), Some(0.));
|
||||
assert_eq!(scale.tick(&3), Some(0.));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scale_point_single() {
|
||||
let scale = ScalePoint::new(vec![1], vec![0., 100.]);
|
||||
assert_eq!(scale.tick(&1), Some(50.));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue