pub struct Chart<'a> { /* private fields */ }
Expand description
A widget to plot one or more Dataset
in a cartesian coordinate system
To use this widget, start by creating one or more Dataset
. With it, you can set the
data points, the name or the
chart type. See Dataset
for a complete documentation of what is
possible.
Then, you’ll usually want to configure the Axis
. Axis titles,
bounds and labels can be configured on both axis. See Axis
for a complete documentation of what is possible.
Finally, you can pass all of that to the Chart
via Chart::new
, Chart::x_axis
and
Chart::y_axis
.
Additionally, Chart
allows configuring the legend position and
hiding constraints.
§Examples
use ratatui::{
style::{Style, Stylize},
symbols,
widgets::{Axis, Block, Chart, Dataset, GraphType},
};
// Create the datasets to fill the chart with
let datasets = vec![
// Scatter chart
Dataset::default()
.name("data1")
.marker(symbols::Marker::Dot)
.graph_type(GraphType::Scatter)
.style(Style::default().cyan())
.data(&[(0.0, 5.0), (1.0, 6.0), (1.5, 6.434)]),
// Line chart
Dataset::default()
.name("data2")
.marker(symbols::Marker::Braille)
.graph_type(GraphType::Line)
.style(Style::default().magenta())
.data(&[(4.0, 5.0), (5.0, 8.0), (7.66, 13.5)]),
];
// Create the X axis and define its properties
let x_axis = Axis::default()
.title("X Axis".red())
.style(Style::default().white())
.bounds([0.0, 10.0])
.labels(["0.0", "5.0", "10.0"]);
// Create the Y axis and define its properties
let y_axis = Axis::default()
.title("Y Axis".red())
.style(Style::default().white())
.bounds([0.0, 10.0])
.labels(["0.0", "5.0", "10.0"]);
// Create the chart and link all the parts together
let chart = Chart::new(datasets)
.block(Block::new().title("Chart"))
.x_axis(x_axis)
.y_axis(y_axis);
Implementations§
Source§impl<'a> Chart<'a>
impl<'a> Chart<'a>
Sourcepub fn new(datasets: Vec<Dataset<'a>>) -> Self
pub fn new(datasets: Vec<Dataset<'a>>) -> Self
Creates a chart with the given datasets
A chart can render multiple datasets.
§Example
This creates a simple chart with one Dataset
use ratatui::widgets::{Chart, Dataset};
let data_points = vec![];
let chart = Chart::new(vec![Dataset::default().data(&data_points)]);
This creates a chart with multiple Dataset
s
use ratatui::widgets::{Chart, Dataset};
let data_points = vec![];
let data_points2 = vec![];
let chart = Chart::new(vec![
Dataset::default().data(&data_points),
Dataset::default().data(&data_points2),
]);
Sourcepub fn block(self, block: Block<'a>) -> Self
pub fn block(self, block: Block<'a>) -> Self
Wraps the chart with the given Block
This is a fluent setter method which must be chained or used as it consumes self
Sourcepub fn x_axis(self, axis: Axis<'a>) -> Self
pub fn x_axis(self, axis: Axis<'a>) -> Self
Sets the X Axis
The default is an empty Axis
, i.e. only a line.
This is a fluent setter method which must be chained or used as it consumes self
§Example
use ratatui::widgets::{Axis, Chart};
let chart = Chart::new(vec![]).x_axis(
Axis::default()
.title("X Axis")
.bounds([0.0, 20.0])
.labels(["0", "20"]),
);
Sourcepub fn y_axis(self, axis: Axis<'a>) -> Self
pub fn y_axis(self, axis: Axis<'a>) -> Self
Sets the Y Axis
The default is an empty Axis
, i.e. only a line.
This is a fluent setter method which must be chained or used as it consumes self
§Example
use ratatui::widgets::{Axis, Chart};
let chart = Chart::new(vec![]).y_axis(
Axis::default()
.title("Y Axis")
.bounds([0.0, 20.0])
.labels(["0", "20"]),
);
Sets the constraints used to determine whether the legend should be shown or not.
The tuple’s first constraint is used for the width and the second for the height. If the
legend takes more space than what is allowed by any constraint, the legend is hidden.
Constraint::Min
is an exception and will always show the legend.
If this is not set, the default behavior is to hide the legend if it is greater than 25% of the chart, either horizontally or vertically.
This is a fluent setter method which must be chained or used as it consumes self
§Examples
Hide the legend when either its width is greater than 33% of the total widget width or if its height is greater than 25% of the total widget height.
use ratatui::{layout::Constraint, widgets::Chart};
let constraints = (Constraint::Ratio(1, 3), Constraint::Ratio(1, 4));
let chart = Chart::new(vec![]).hidden_legend_constraints(constraints);
Always show the legend, note the second constraint doesn’t matter in this case since the first one is always true.
use ratatui::{layout::Constraint, widgets::Chart};
let constraints = (Constraint::Min(0), Constraint::Ratio(1, 4));
let chart = Chart::new(vec![]).hidden_legend_constraints(constraints);
Always hide the legend. Note this can be accomplished more explicitly by passing None
to
Chart::legend_position
.
use ratatui::{layout::Constraint, widgets::Chart};
let constraints = (Constraint::Length(0), Constraint::Ratio(1, 4));
let chart = Chart::new(vec![]).hidden_legend_constraints(constraints);
Sourcepub const fn legend_position(self, position: Option<LegendPosition>) -> Self
pub const fn legend_position(self, position: Option<LegendPosition>) -> Self
Sets the position of a legend or hide it
The default is LegendPosition::TopRight
.
If None
is given, hide the legend even if hidden_legend_constraints
determines it
should be shown. In contrast, if Some(...)
is given, hidden_legend_constraints
might
still decide whether to show the legend or not.
See LegendPosition
for all available positions.
This is a fluent setter method which must be chained or used as it consumes self
§Examples
Show the legend on the top left corner.
use ratatui::widgets::{Chart, LegendPosition};
let chart: Chart = Chart::new(vec![]).legend_position(Some(LegendPosition::TopLeft));
Hide the legend altogether
use ratatui::widgets::{Chart, LegendPosition};
let chart = Chart::new(vec![]).legend_position(None);
Trait Implementations§
Source§impl WidgetRef for Chart<'_>
impl WidgetRef for Chart<'_>
Source§fn render_ref(&self, area: Rect, buf: &mut Buffer)
fn render_ref(&self, area: Rect, buf: &mut Buffer)
impl<'a> StructuralPartialEq for Chart<'a>
Auto Trait Implementations§
impl<'a> Freeze for Chart<'a>
impl<'a> RefUnwindSafe for Chart<'a>
impl<'a> Send for Chart<'a>
impl<'a> Sync for Chart<'a>
impl<'a> Unpin for Chart<'a>
impl<'a> UnwindSafe for Chart<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<'a, T, U> Stylize<'a, T> for Uwhere
U: Styled<Item = T>,
impl<'a, T, U> Stylize<'a, T> for Uwhere
U: Styled<Item = T>,
fn bg<C>(self, color: C) -> T
fn fg<C>(self, color: C) -> T
fn add_modifier(self, modifier: Modifier) -> T
fn remove_modifier(self, modifier: Modifier) -> T
fn reset(self) -> T
Source§fn on_magenta(self) -> T
fn on_magenta(self) -> T
magenta
.Source§fn on_dark_gray(self) -> T
fn on_dark_gray(self) -> T
dark_gray
.Source§fn on_light_red(self) -> T
fn on_light_red(self) -> T
light_red
.Source§fn light_green(self) -> T
fn light_green(self) -> T
light_green
.Source§fn on_light_green(self) -> T
fn on_light_green(self) -> T
light_green
.Source§fn light_yellow(self) -> T
fn light_yellow(self) -> T
light_yellow
.Source§fn on_light_yellow(self) -> T
fn on_light_yellow(self) -> T
light_yellow
.Source§fn light_blue(self) -> T
fn light_blue(self) -> T
light_blue
.Source§fn on_light_blue(self) -> T
fn on_light_blue(self) -> T
light_blue
.Source§fn light_magenta(self) -> T
fn light_magenta(self) -> T
light_magenta
.Source§fn on_light_magenta(self) -> T
fn on_light_magenta(self) -> T
light_magenta
.Source§fn light_cyan(self) -> T
fn light_cyan(self) -> T
light_cyan
.Source§fn on_light_cyan(self) -> T
fn on_light_cyan(self) -> T
light_cyan
.Source§fn not_italic(self) -> T
fn not_italic(self) -> T
ITALIC
modifier.Source§fn underlined(self) -> T
fn underlined(self) -> T
UNDERLINED
modifier.Source§fn not_underlined(self) -> T
fn not_underlined(self) -> T
UNDERLINED
modifier.Source§fn slow_blink(self) -> T
fn slow_blink(self) -> T
SLOW_BLINK
modifier.Source§fn not_slow_blink(self) -> T
fn not_slow_blink(self) -> T
SLOW_BLINK
modifier.Source§fn rapid_blink(self) -> T
fn rapid_blink(self) -> T
RAPID_BLINK
modifier.Source§fn not_rapid_blink(self) -> T
fn not_rapid_blink(self) -> T
RAPID_BLINK
modifier.Source§fn not_reversed(self) -> T
fn not_reversed(self) -> T
REVERSED
modifier.HIDDEN
modifier.HIDDEN
modifier.Source§fn crossed_out(self) -> T
fn crossed_out(self) -> T
CROSSED_OUT
modifier.Source§fn not_crossed_out(self) -> T
fn not_crossed_out(self) -> T
CROSSED_OUT
modifier.