Skip to content

Commit 6fefd2b

Browse files
committed
Fix issue #186
Fix the code that may map the vlaue out of drawing area. However, the root cause of this issue is the ambiguity of the backend coordinate system. To properly address this issue, we need to clean up the backend coordinate system ambiguities in the future.
1 parent e29840c commit 6fefd2b

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

plotters/src/chart/builder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ impl<'a, 'b, DB: DrawingBackend> ChartBuilder<'a, 'b, DB> {
411411
}
412412

413413
let mut pixel_range = drawing_area.get_pixel_range();
414-
pixel_range.1 = (pixel_range.1.end - 1)..(pixel_range.1.start - 1);
414+
pixel_range.0.end -= 1;
415+
pixel_range.1.end -= 1;
416+
pixel_range.1 = pixel_range.1.end..pixel_range.1.start;
415417

416418
let mut x_label_area = [None, None];
417419
let mut y_label_area = [None, None];

plotters/src/chart/context/cartesian2d/draw_impl.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged> ChartContext<'a, DB, Cartesia
7878
self.drawing_area.get_y_axis_pixel_range()
7979
};
8080

81-
/* At this point, the coordinate system tells us the pixel range
82-
* after the translation.
83-
* However, we need to use the logic coordinate system for drawing. */
81+
// At this point, the coordinate system tells us the pixel range after the translation.
82+
// However, we need to use the logic coordinate system for drawing.
8483
if orientation.0 == 0 {
8584
axis_range.start -= x0;
8685
axis_range.end -= x0;

plotters/src/coord/ranged1d/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub trait Ranged {
213213
if limit.0 < limit.1 {
214214
limit.0..limit.1
215215
} else {
216-
(limit.1 + 1)..(limit.0 + 1)
216+
limit.1..limit.0
217217
}
218218
}
219219
}

plotters/src/coord/ranged1d/types/numeric.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ macro_rules! make_numeric_coord {
8787
return limit.1;
8888
}
8989

90-
return limit.0 + (actual_length as f64 * logic_length + 1e-3).floor() as i32;
90+
if actual_length > 0 {
91+
return limit.0 + (actual_length as f64 * logic_length + 1e-3).floor() as i32;
92+
} else {
93+
return limit.0 + (actual_length as f64 * logic_length - 1e-3).ceil() as i32;
94+
}
9195
}
9296
fn key_points<Hint: KeyPointHint>(&self, hint: Hint) -> Vec<$type> {
9397
$key_points((self.0, self.1), hint.max_num_points())
@@ -165,7 +169,7 @@ macro_rules! gen_key_points_comp {
165169
}
166170

167171
let mut ret = vec![];
168-
// In some extreme cases, left might be too big, so that (left + scale) - left == 0 due to
172+
// In some extreme cases, left might be too big, so that (left + scale) - left == 0 due to
169173
// floating point error.
170174
// In this case, we may loop forever. To avoid this, we need to use two variables to store
171175
// the current left value. So we need keep a left_base and a left_relative.
@@ -180,7 +184,8 @@ macro_rules! gen_key_points_comp {
180184
let mut left_relative = left - left_base;
181185
let right = range.1 - rem_euclid(range.1, scale);
182186
while (right - left_relative - left_base) >= -std::f64::EPSILON {
183-
let new_left_relative = (left_relative / value_granularity).round() * value_granularity;
187+
let new_left_relative =
188+
(left_relative / value_granularity).round() * value_granularity;
184189
if new_left_relative < 0.0 {
185190
left_relative += value_granularity;
186191
}

0 commit comments

Comments
 (0)