Skip to content

Commit fdbea9e

Browse files
committed
fix(widgets/canvas): avoid panic on zero-width bounds
1 parent 6204edd commit fdbea9e

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/widgets/canvas/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ impl<'a, 'b> Painter<'a, 'b> {
206206
}
207207
let width = (self.context.x_bounds[1] - self.context.x_bounds[0]).abs();
208208
let height = (self.context.y_bounds[1] - self.context.y_bounds[0]).abs();
209+
if width == 0.0 || height == 0.0 {
210+
return None;
211+
}
209212
let x = ((x - left) * self.resolution.0 / width) as usize;
210213
let y = ((top - y) * self.resolution.1 / height) as usize;
211214
Some((x, y))

tests/widgets_chart.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn widgets_chart_can_have_axis_with_zero_length_bounds() {
2121
let chart = Chart::default()
2222
.block(Block::default().title("Plot").borders(Borders::ALL))
2323
.x_axis(Axis::default().bounds([0.0, 0.0]).labels(&["0.0", "1.0"]))
24-
.y_axis(Axis::default().bounds([0.0, 1.0]).labels(&["0.0", "1.0"]))
24+
.y_axis(Axis::default().bounds([0.0, 0.0]).labels(&["0.0", "1.0"]))
2525
.datasets(&datasets);
2626
f.render_widget(
2727
chart,

0 commit comments

Comments
 (0)