Add linechart support

Closes #73

This commit only adds support for linecharts for the braille marker.
pull/225/head
Caleb Bassi 4 years ago committed by Florian Dehau
parent 7aae9b380e
commit 262bf441ce

@ -6,7 +6,7 @@ use crate::buffer::Buffer;
use crate::layout::Rect; use crate::layout::Rect;
use crate::style::Style; use crate::style::Style;
use crate::symbols; use crate::symbols;
use crate::widgets::canvas::{Canvas, Points}; use crate::widgets::canvas::{Canvas, Line, Points};
use crate::widgets::{Block, Borders, Widget}; use crate::widgets::{Block, Borders, Widget};
/// An X or Y axis for the chart widget /// An X or Y axis for the chart widget
@ -87,6 +87,14 @@ pub enum Marker {
Braille, Braille,
} }
/// Used to determine which style of graphing to use
pub enum GraphType {
/// Draw each point
Scatter,
/// Draw each point and lines between each point using the same marker
Line,
}
/// A group of data points /// A group of data points
pub struct Dataset<'a> { pub struct Dataset<'a> {
/// Name of the dataset (used in the legend if shown) /// Name of the dataset (used in the legend if shown)
@ -95,6 +103,8 @@ pub struct Dataset<'a> {
data: &'a [(f64, f64)], data: &'a [(f64, f64)],
/// Symbol used for each points of this dataset /// Symbol used for each points of this dataset
marker: Marker, marker: Marker,
/// Determines graph type used for drawing points
graph_type: GraphType,
/// Style used to plot this dataset /// Style used to plot this dataset
style: Style, style: Style,
} }
@ -105,6 +115,7 @@ impl<'a> Default for Dataset<'a> {
name: "", name: "",
data: &[], data: &[],
marker: Marker::Dot, marker: Marker::Dot,
graph_type: GraphType::Scatter,
style: Style::default(), style: Style::default(),
} }
} }
@ -126,6 +137,11 @@ impl<'a> Dataset<'a> {
self self
} }
pub fn graph_type(mut self, graph_type: GraphType) -> Dataset<'a> {
self.graph_type = graph_type;
self
}
pub fn style(mut self, style: Style) -> Dataset<'a> { pub fn style(mut self, style: Style) -> Dataset<'a> {
self.style = style; self.style = style;
self self
@ -166,7 +182,7 @@ impl Default for ChartLayout {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// # use tui::widgets::{Block, Borders, Chart, Axis, Dataset, Marker}; /// # use tui::widgets::{Block, Borders, Chart, Axis, Dataset, Marker, GraphType};
/// # use tui::style::{Style, Color}; /// # use tui::style::{Style, Color};
/// # fn main() { /// # fn main() {
/// Chart::default() /// Chart::default()
@ -186,11 +202,13 @@ impl Default for ChartLayout {
/// .datasets(&[Dataset::default() /// .datasets(&[Dataset::default()
/// .name("data1") /// .name("data1")
/// .marker(Marker::Dot) /// .marker(Marker::Dot)
/// .graph_type(GraphType::Scatter)
/// .style(Style::default().fg(Color::Cyan)) /// .style(Style::default().fg(Color::Cyan))
/// .data(&[(0.0, 5.0), (1.0, 6.0), (1.5, 6.434)]), /// .data(&[(0.0, 5.0), (1.0, 6.0), (1.5, 6.434)]),
/// Dataset::default() /// Dataset::default()
/// .name("data2") /// .name("data2")
/// .marker(Marker::Braille) /// .marker(Marker::Braille)
/// .graph_type(GraphType::Line)
/// .style(Style::default().fg(Color::Magenta)) /// .style(Style::default().fg(Color::Magenta))
/// .data(&[(4.0, 5.0), (5.0, 8.0), (7.66, 13.5)])]); /// .data(&[(4.0, 5.0), (5.0, 8.0), (7.66, 13.5)])]);
/// # } /// # }
@ -452,11 +470,24 @@ where
.background_color(self.style.bg) .background_color(self.style.bg)
.x_bounds(self.x_axis.bounds) .x_bounds(self.x_axis.bounds)
.y_bounds(self.y_axis.bounds) .y_bounds(self.y_axis.bounds)
.paint(|ctx| { .paint(|ctx| match dataset.graph_type {
ctx.draw(&Points { GraphType::Scatter => {
coords: dataset.data, ctx.draw(&Points {
color: dataset.style.fg, coords: dataset.data,
}); color: dataset.style.fg,
});
}
GraphType::Line => {
for i in 0..dataset.data.len() - 1 {
ctx.draw(&Line {
x1: dataset.data[i].0,
y1: dataset.data[i].1,
x2: dataset.data[i + 1].0,
y2: dataset.data[i + 1].1,
color: dataset.style.fg,
})
}
}
}) })
.draw(graph_area, buf); .draw(graph_area, buf);
} }

@ -14,9 +14,8 @@ mod table;
mod tabs; mod tabs;
pub use self::barchart::BarChart; pub use self::barchart::BarChart;
pub use self::block::Block; pub use self::block::{Block, BorderType};
pub use self::block::BorderType; pub use self::chart::{Axis, Chart, Dataset, GraphType, Marker};
pub use self::chart::{Axis, Chart, Dataset, Marker};
pub use self::gauge::Gauge; pub use self::gauge::Gauge;
pub use self::list::{List, SelectableList}; pub use self::list::{List, SelectableList};
pub use self::paragraph::Paragraph; pub use self::paragraph::Paragraph;

Loading…
Cancel
Save