Skip to content

Commit 9d99ed6

Browse files
committed
Set default axis bounds when all values are equal
Fixes #36
1 parent 56a81e0 commit 9d99ed6

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## Unreleased
4+
### Fixed
5+
- Set default axis bounds for case where all values are equal (Issue #36)
46

57
## 0.5.0 - 2020-03-14
68
### Added

src/utils.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ pub fn range(s: &[f64]) -> (f64, f64) {
4949
(min, max)
5050
}
5151

52+
/**
53+
Floor or ceiling the min or max to zero to avoid them both having the same value
54+
*/
55+
pub fn pad_range_to_zero(min: f64, max: f64) -> (f64, f64) {
56+
if (min - max).abs() < std::f64::EPSILON {
57+
(if min > 0. {0.} else {min}, if max < 0. {0.} else {max})
58+
} else {
59+
(min, max)
60+
}
61+
}
62+
5263
#[cfg(test)]
5364
mod tests {
5465
use super::*;
@@ -97,4 +108,11 @@ mod tests {
97108
assert_eq!(quartiles(&[1., 2., 4.]), (1., 2., 4.));
98109
assert_eq!(quartiles(&[1., 2., 3., 4.]), (1.5, 2.5, 3.5));
99110
}
111+
112+
#[test]
113+
fn test_pad_range_to_zero() {
114+
assert_eq!(pad_range_to_zero(2.0, 2.0), (0.0, 2.0));
115+
assert_eq!(pad_range_to_zero(-2.0, 2.0), (-2.0, 2.0));
116+
assert_eq!(pad_range_to_zero(-2.0, -2.0), (-2.0, 0.0));
117+
}
100118
}

src/view.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::grid::{Grid, GridType};
1818
use crate::repr::{CategoricalRepresentation, ContinuousRepresentation};
1919
use crate::svg_render;
2020
use crate::text_render;
21+
use crate::utils;
2122

2223
pub trait View {
2324
fn to_svg(&self, face_width: f64, face_height: f64) -> Result<svg::node::element::Group>;
@@ -108,6 +109,7 @@ impl ContinuousView {
108109
x_min = x_min.min(this_x_min);
109110
x_max = x_max.max(this_x_max);
110111
}
112+
let (x_min, x_max) = utils::pad_range_to_zero(x_min, x_max);
111113
axis::Range::new(x_min, x_max)
112114
}
113115

@@ -119,6 +121,7 @@ impl ContinuousView {
119121
y_min = y_min.min(this_y_min);
120122
y_max = y_max.max(this_y_max);
121123
}
124+
let (y_min, y_max) = utils::pad_range_to_zero(y_min, y_max);
122125
axis::Range::new(y_min, y_max)
123126
}
124127

0 commit comments

Comments
 (0)