Skip to content

Commit

Permalink
Merge pull request #561 from NorbertGarfield/issue-532
Browse files Browse the repository at this point in the history
Clamp infinite values in logarithmic coordinate system
  • Loading branch information
AaronErhardt authored Mar 28, 2024
2 parents af0b63c + c621f5a commit 8f14c1e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions plotters/src/chart/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,27 @@ mod test {
chart.caption("This is a test case", ("serif", 10));
assert_eq!(chart.title.as_ref().unwrap().1.font.get_name(), "serif");
}

#[test]
fn test_zero_limit_with_log_scale() {
let drawing_area = create_mocked_drawing_area(640, 480, |_| {});

let mut chart = ChartBuilder::on(&drawing_area)
.build_cartesian_2d(0f32..10f32, (1e-6f32..1f32).log_scale())
.unwrap();

let data = vec![
(2f32, 1e-4f32),
(4f32, 1e-3f32),
(6f32, 1e-2f32),
(8f32, 1e-1f32),
];

chart
.draw_series(
data.iter()
.map(|&(x, y)| Rectangle::new([(x - 0.5, 0.0), (x + 0.5, y)], RED.filled())),
)
.unwrap();
}
}
8 changes: 8 additions & 0 deletions plotters/src/coord/ranged1d/types/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ macro_rules! make_numeric_coord {
return limit.1;
}

if logic_length.is_infinite() {
if logic_length.is_sign_positive() {
return limit.1;
} else {
return limit.0;
}
}

if actual_length > 0 {
return limit.0 + (actual_length as f64 * logic_length + 1e-3).floor() as i32;
} else {
Expand Down

0 comments on commit 8f14c1e

Please sign in to comment.