added 'drawed' bool to dataset points

pull/701/head
Samuel Voss 1 year ago
parent 6ffde0fb0c
commit eac9526d9b

@ -18,15 +18,21 @@ use tui::{
Frame, Terminal,
};
const DATA: [(f64, f64); 5] = [(0.0, 0.0), (1.0, 1.0), (2.0, 2.0), (3.0, 3.0), (4.0, 4.0)];
const DATA2: [(f64, f64); 7] = [
(0.0, 0.0),
(10.0, 1.0),
(20.0, 0.5),
(30.0, 1.5),
(40.0, 1.0),
(50.0, 2.5),
(60.0, 3.0),
const DATA: [(f64, f64, bool); 5] = [
(0.0, 0.0, true),
(1.0, 1.0, true),
(2.0, 2.0, true),
(3.0, 3.0, true),
(4.0, 4.0, true)
];
const DATA2: [(f64, f64, bool); 7] = [
(0.0, 0.0, true),
(10.0, 1.0, true),
(20.0, 0.5, true),
(30.0, 1.5, true),
(40.0, 1.0, true),
(50.0, 2.5, true),
(60.0, 3.0, true),
];
#[derive(Clone)]
@ -49,9 +55,9 @@ impl SinSignal {
}
impl Iterator for SinSignal {
type Item = (f64, f64);
type Item = (f64, f64, bool);
fn next(&mut self) -> Option<Self::Item> {
let point = (self.x, (self.x * 1.0 / self.period).sin() * self.scale);
let point = (self.x, (self.x * 1.0 / self.period).sin() * self.scale, true);
self.x += self.interval;
Some(point)
}
@ -59,9 +65,9 @@ impl Iterator for SinSignal {
struct App {
signal1: SinSignal,
data1: Vec<(f64, f64)>,
data1: Vec<(f64, f64, bool)>,
signal2: SinSignal,
data2: Vec<(f64, f64)>,
data2: Vec<(f64, f64, bool)>,
window: [f64; 2],
}
@ -69,8 +75,8 @@ impl App {
fn new() -> App {
let mut signal1 = SinSignal::new(0.2, 3.0, 18.0);
let mut signal2 = SinSignal::new(0.1, 2.0, 10.0);
let data1 = signal1.by_ref().take(200).collect::<Vec<(f64, f64)>>();
let data2 = signal2.by_ref().take(200).collect::<Vec<(f64, f64)>>();
let data1 = signal1.by_ref().take(200).collect::<Vec<(f64, f64, bool)>>();
let data2 = signal2.by_ref().take(200).collect::<Vec<(f64, f64, bool)>>();
App {
signal1,
data1,

@ -108,9 +108,9 @@ impl SinSignal {
}
impl Iterator for SinSignal {
type Item = (f64, f64);
type Item = (f64, f64, bool);
fn next(&mut self) -> Option<Self::Item> {
let point = (self.x, (self.x * 1.0 / self.period).sin() * self.scale);
let point = (self.x, (self.x * 1.0 / self.period).sin() * self.scale, true);
self.x += self.interval;
Some(point)
}

@ -6,16 +6,21 @@ use crate::{
/// A shape to draw a group of points with the given color
#[derive(Debug, Clone)]
pub struct Points<'a> {
pub coords: &'a [(f64, f64)],
pub coords: &'a [(f64, f64, bool)],
pub color: Color,
}
impl<'a> Shape for Points<'a> {
fn draw(&self, painter: &mut Painter) {
for (x, y) in self.coords {
if let Some((x, y)) = painter.get_point(*x, *y) {
painter.paint(x, y, self.color);
for (x, y, drawed) in self.coords {
if *drawed {
if let Some((x, y)) = painter.get_point(*x, *y) {
painter.paint(x, y, self.color);
}
} else if let Some((x, y)) = painter.get_point(*x, *y) {
painter.paint(x, y, Color::Reset);
}
}
}
}

@ -95,6 +95,8 @@ pub enum GraphType {
Scatter,
/// Draw each point and lines between each point using the same marker
Line,
/// Draw each point and lines between each point using the same marker, with a boolean if the line is rendered
Svg,
}
/// A group of data points
@ -103,7 +105,7 @@ pub struct Dataset<'a> {
/// Name of the dataset (used in the legend if shown)
name: Cow<'a, str>,
/// A reference to the actual data
data: &'a [(f64, f64)],
data: &'a [(f64, f64, bool)],
/// Symbol used for each points of this dataset
marker: symbols::Marker,
/// Determines graph type used for drawing points
@ -133,7 +135,7 @@ impl<'a> Dataset<'a> {
self
}
pub fn data(mut self, data: &'a [(f64, f64)]) -> Dataset<'a> {
pub fn data(mut self, data: &'a [(f64, f64, bool)]) -> Dataset<'a> {
self.data = data;
self
}
@ -629,7 +631,7 @@ mod tests {
#[test]
fn it_should_hide_the_legend() {
let data = [(0.0, 5.0), (1.0, 6.0), (3.0, 7.0)];
let data = [(0.0, 5.0, true), (1.0, 6.0, true), (3.0, 7.0, true)];
let cases = [
LegendTestCase {
chart_area: Rect::new(0, 0, 100, 100),

@ -40,7 +40,7 @@ fn widgets_chart_can_render_on_small_areas() {
let datasets = vec![Dataset::default()
.marker(symbols::Marker::Braille)
.style(Style::default().fg(Color::Magenta))
.data(&[(0.0, 0.0)])];
.data(&[(0.0, 0.0, true)])];
let chart = Chart::new(datasets)
.block(Block::default().title("Plot").borders(Borders::ALL))
.x_axis(
@ -265,7 +265,7 @@ fn widgets_chart_can_have_axis_with_zero_length_bounds() {
let datasets = vec![Dataset::default()
.marker(symbols::Marker::Braille)
.style(Style::default().fg(Color::Magenta))
.data(&[(0.0, 0.0)])];
.data(&[(0.0, 0.0, true)])];
let chart = Chart::new(datasets)
.block(Block::default().title("Plot").borders(Borders::ALL))
.x_axis(
@ -302,9 +302,9 @@ fn widgets_chart_handles_overflows() {
.marker(symbols::Marker::Braille)
.style(Style::default().fg(Color::Magenta))
.data(&[
(1_588_298_471.0, 1.0),
(1_588_298_473.0, 0.0),
(1_588_298_496.0, 1.0),
(1_588_298_471.0, 1.0, true),
(1_588_298_473.0, 0.0, true),
(1_588_298_496.0, 1.0, true),
])];
let chart = Chart::new(datasets)
.block(Block::default().title("Plot").borders(Borders::ALL))
@ -379,34 +379,34 @@ fn widgets_chart_can_have_a_legend() {
.name("Dataset 1")
.style(Style::default().fg(Color::Blue))
.data(&[
(0.0, 0.0),
(10.0, 1.0),
(20.0, 2.0),
(30.0, 3.0),
(40.0, 4.0),
(50.0, 5.0),
(60.0, 6.0),
(70.0, 7.0),
(80.0, 8.0),
(90.0, 9.0),
(100.0, 10.0),
(0.0, 0.0, true),
(10.0, 1.0, true),
(20.0, 2.0, true),
(30.0, 3.0, true),
(40.0, 4.0, true),
(50.0, 5.0, true),
(60.0, 6.0, true),
(70.0, 7.0, true),
(80.0, 8.0, true),
(90.0, 9.0, true),
(100.0, 10.0, true),
])
.graph_type(Line),
Dataset::default()
.name("Dataset 2")
.style(Style::default().fg(Color::Green))
.data(&[
(0.0, 10.0),
(10.0, 9.0),
(20.0, 8.0),
(30.0, 7.0),
(40.0, 6.0),
(50.0, 5.0),
(60.0, 4.0),
(70.0, 3.0),
(80.0, 2.0),
(90.0, 1.0),
(100.0, 0.0),
(0.0, 10.0, true),
(10.0, 9.0, true),
(20.0, 8.0, true),
(30.0, 7.0, true),
(40.0, 6.0, true),
(50.0, 5.0, true),
(60.0, 4.0, true),
(70.0, 3.0, true),
(80.0, 2.0, true),
(90.0, 1.0, true),
(100.0, 0.0, true),
])
.graph_type(Line),
];

Loading…
Cancel
Save