From d038b283dbb83ced141d7d93129cd8b147f0e9a2 Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Tue, 1 Nov 2016 22:54:16 +0100 Subject: [PATCH] Fix chart and canvas widgets --- examples/prototype.rs | 6 +++--- src/widgets/canvas/mod.rs | 21 ++++++++++++--------- src/widgets/chart.rs | 13 ++++++------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/examples/prototype.rs b/examples/prototype.rs index bf1d508b..2b28658f 100644 --- a/examples/prototype.rs +++ b/examples/prototype.rs @@ -149,7 +149,7 @@ fn main() { show_chart: true, progress: 0, data: rand_signal.clone().take(200).collect(), - data2: sin_signal.clone().take(20).collect(), + data2: sin_signal.clone().take(30).collect(), data3: sin_signal2.clone().take(200).collect(), data4: vec![("B1", 9), ("B2", 12), @@ -173,7 +173,7 @@ fn main() { let (tx, rx) = mpsc::channel(); let input_tx = tx.clone(); - for _ in 0..20 { + for _ in 0..30 { sin_signal.next(); } for _ in 0..200 { @@ -301,7 +301,7 @@ fn draw(t: &mut Terminal, app: &App) { Canvas::default() .block(Block::default().title("World").borders(border::ALL)) .layers(&[&[Map::default().resolution(MapResolution::High)]]) - .x_bounds([180.0, -180.0]) + .x_bounds([-180.0, 180.0]) .y_bounds([-90.0, 90.0]) .render(&chunks[1], t); }) diff --git a/src/widgets/canvas/mod.rs b/src/widgets/canvas/mod.rs index 9d0a1248..55418ae7 100644 --- a/src/widgets/canvas/mod.rs +++ b/src/widgets/canvas/mod.rs @@ -74,13 +74,13 @@ impl<'a> Widget for Canvas<'a> { let width = canvas_area.width as usize; let height = canvas_area.height as usize; - let mut x_bounds = self.x_bounds; x_bounds.sort_by(|a, b| a.partial_cmp(b).unwrap()); let mut y_bounds = self.y_bounds; y_bounds.sort_by(|a, b| a.partial_cmp(b).unwrap()); for layer in self.layers { + let mut grid: Vec = vec![BRAILLE_OFFSET; width * height + 1]; let mut colors: Vec = vec![Color::Reset; width * height + 1]; @@ -91,7 +91,7 @@ impl<'a> Widget for Canvas<'a> { let dy = ((self.y_bounds[1] - y) * canvas_area.height as f64 * 4.0 / (self.y_bounds[1] - self.y_bounds[0])) as usize; - let dx = ((self.x_bounds[1] - x) * canvas_area.width as f64 * 2.0 / + let dx = ((x - self.x_bounds[0]) * canvas_area.width as f64 * 2.0 / (self.x_bounds[1] - self.x_bounds[0])) as usize; let index = dy / 4 * width + dx / 2; @@ -100,16 +100,19 @@ impl<'a> Widget for Canvas<'a> { } } - let string = String::from_utf16(&grid).unwrap(); + let mut string = String::from_utf16(&grid).unwrap(); + string.pop(); for (i, (ch, color)) in string.chars().zip(colors.into_iter()).enumerate() { if ch != BRAILLE_BLANK { let (x, y) = (i % width, i / width); - buf.update_cell(x as u16 + canvas_area.left(), y as u16 + area.top(), |c| { - c.symbol.clear(); - c.symbol.push(ch); - c.fg = color; - c.bg = Color::Reset; - }); + buf.update_cell(x as u16 + canvas_area.left(), + y as u16 + canvas_area.top(), + |c| { + c.symbol.clear(); + c.symbol.push(ch); + c.fg = color; + c.bg = Color::Reset; + }); } } } diff --git a/src/widgets/chart.rs b/src/widgets/chart.rs index 92a0197c..71273ebc 100644 --- a/src/widgets/chart.rs +++ b/src/widgets/chart.rs @@ -304,15 +304,14 @@ impl<'a> Widget for Chart<'a> { for dataset in self.datasets { match dataset.marker { Marker::Dot => { - for &(x, y) in dataset.data.iter() { - if x < self.x_axis.bounds[0] || x > self.x_axis.bounds[1] || - y < self.y_axis.bounds[0] || - y > self.y_axis.bounds[1] { - continue; - } + for &(x, y) in dataset.data.iter().filter(|&&(x, y)| { + !(x < self.x_axis.bounds[0] || x > self.x_axis.bounds[1] || + y < self.y_axis.bounds[0] || + y > self.y_axis.bounds[1]) + }) { let dy = (self.y_axis.bounds[1] - y) * graph_area.height as f64 / (self.y_axis.bounds[1] - self.y_axis.bounds[0]); - let dx = (self.x_axis.bounds[1] - x) * graph_area.width as f64 / + let dx = (x - self.x_axis.bounds[0]) * graph_area.width as f64 / (self.x_axis.bounds[1] - self.x_axis.bounds[0]); buf.set_cell(dx as u16 + graph_area.left(),