Fix chart and canvas widgets

This commit is contained in:
Florian Dehau 2016-11-01 22:54:16 +01:00
parent cd919e69f5
commit d038b283db
3 changed files with 21 additions and 19 deletions

View file

@ -149,7 +149,7 @@ fn main() {
show_chart: true, show_chart: true,
progress: 0, progress: 0,
data: rand_signal.clone().take(200).collect(), 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(), data3: sin_signal2.clone().take(200).collect(),
data4: vec![("B1", 9), data4: vec![("B1", 9),
("B2", 12), ("B2", 12),
@ -173,7 +173,7 @@ fn main() {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let input_tx = tx.clone(); let input_tx = tx.clone();
for _ in 0..20 { for _ in 0..30 {
sin_signal.next(); sin_signal.next();
} }
for _ in 0..200 { for _ in 0..200 {
@ -301,7 +301,7 @@ fn draw(t: &mut Terminal, app: &App) {
Canvas::default() Canvas::default()
.block(Block::default().title("World").borders(border::ALL)) .block(Block::default().title("World").borders(border::ALL))
.layers(&[&[Map::default().resolution(MapResolution::High)]]) .layers(&[&[Map::default().resolution(MapResolution::High)]])
.x_bounds([180.0, -180.0]) .x_bounds([-180.0, 180.0])
.y_bounds([-90.0, 90.0]) .y_bounds([-90.0, 90.0])
.render(&chunks[1], t); .render(&chunks[1], t);
}) })

View file

@ -74,13 +74,13 @@ impl<'a> Widget for Canvas<'a> {
let width = canvas_area.width as usize; let width = canvas_area.width as usize;
let height = canvas_area.height as usize; let height = canvas_area.height as usize;
let mut x_bounds = self.x_bounds; let mut x_bounds = self.x_bounds;
x_bounds.sort_by(|a, b| a.partial_cmp(b).unwrap()); x_bounds.sort_by(|a, b| a.partial_cmp(b).unwrap());
let mut y_bounds = self.y_bounds; let mut y_bounds = self.y_bounds;
y_bounds.sort_by(|a, b| a.partial_cmp(b).unwrap()); y_bounds.sort_by(|a, b| a.partial_cmp(b).unwrap());
for layer in self.layers { for layer in self.layers {
let mut grid: Vec<u16> = vec![BRAILLE_OFFSET; width * height + 1]; let mut grid: Vec<u16> = vec![BRAILLE_OFFSET; width * height + 1];
let mut colors: Vec<Color> = vec![Color::Reset; width * height + 1]; let mut colors: Vec<Color> = 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 / let dy = ((self.y_bounds[1] - y) * canvas_area.height as f64 * 4.0 /
(self.y_bounds[1] - (self.y_bounds[1] -
self.y_bounds[0])) as usize; 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[1] -
self.x_bounds[0])) as usize; self.x_bounds[0])) as usize;
let index = dy / 4 * width + dx / 2; 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() { for (i, (ch, color)) in string.chars().zip(colors.into_iter()).enumerate() {
if ch != BRAILLE_BLANK { if ch != BRAILLE_BLANK {
let (x, y) = (i % width, i / width); let (x, y) = (i % width, i / width);
buf.update_cell(x as u16 + canvas_area.left(), y as u16 + area.top(), |c| { buf.update_cell(x as u16 + canvas_area.left(),
c.symbol.clear(); y as u16 + canvas_area.top(),
c.symbol.push(ch); |c| {
c.fg = color; c.symbol.clear();
c.bg = Color::Reset; c.symbol.push(ch);
}); c.fg = color;
c.bg = Color::Reset;
});
} }
} }
} }

View file

@ -304,15 +304,14 @@ impl<'a> Widget for Chart<'a> {
for dataset in self.datasets { for dataset in self.datasets {
match dataset.marker { match dataset.marker {
Marker::Dot => { Marker::Dot => {
for &(x, y) in dataset.data.iter() { for &(x, y) in dataset.data.iter().filter(|&&(x, y)| {
if x < self.x_axis.bounds[0] || x > self.x_axis.bounds[1] || !(x < self.x_axis.bounds[0] || x > self.x_axis.bounds[1] ||
y < self.y_axis.bounds[0] || y < self.y_axis.bounds[0] ||
y > self.y_axis.bounds[1] { y > self.y_axis.bounds[1])
continue; }) {
}
let dy = (self.y_axis.bounds[1] - y) * graph_area.height as f64 / let dy = (self.y_axis.bounds[1] - y) * graph_area.height as f64 /
(self.y_axis.bounds[1] - self.y_axis.bounds[0]); (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]); (self.x_axis.bounds[1] - self.x_axis.bounds[0]);
buf.set_cell(dx as u16 + graph_area.left(), buf.set_cell(dx as u16 + graph_area.left(),