mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-22 12:43:16 +00:00
Fix chart and canvas widgets
This commit is contained in:
parent
cd919e69f5
commit
d038b283db
3 changed files with 21 additions and 19 deletions
|
@ -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);
|
||||
})
|
||||
|
|
|
@ -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<u16> = vec![BRAILLE_OFFSET; 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 /
|
||||
(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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(),
|
||||
|
|
Loading…
Reference in a new issue