mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-23 05:03:13 +00:00
docs(Sparkline): add documentation (#648)
This commit is contained in:
parent
c862aa5e9e
commit
fe632d70cb
1 changed files with 47 additions and 1 deletions
|
@ -1,3 +1,4 @@
|
|||
#![warn(missing_docs)]
|
||||
use std::cmp::min;
|
||||
|
||||
use strum::{Display, EnumString};
|
||||
|
@ -12,6 +13,18 @@ use crate::{
|
|||
|
||||
/// Widget to render a sparkline over one or more lines.
|
||||
///
|
||||
/// You can create a `Sparkline` using [`Sparkline::default`].
|
||||
///
|
||||
/// `Sparkline` can be styled either using [`Sparkline::style`] or preferably using the methods
|
||||
/// provided by the [`Stylize`](crate::style::Stylize) trait.
|
||||
///
|
||||
/// # Builder methods
|
||||
///
|
||||
/// - [`Sparkline::block`] wraps the sparkline in a [`Block`]
|
||||
/// - [`Sparkline::data`] defines the dataset, you'll almost always want to use it
|
||||
/// - [`Sparkline::max`] sets the maximum value of bars
|
||||
/// - [`Sparkline::direction`] sets the render direction
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
|
@ -21,7 +34,8 @@ use crate::{
|
|||
/// .block(Block::default().title("Sparkline").borders(Borders::ALL))
|
||||
/// .data(&[0, 2, 3, 4, 1, 4, 10])
|
||||
/// .max(5)
|
||||
/// .style(Style::default().fg(Color::Red).bg(Color::White));
|
||||
/// .direction(RenderDirection::RightToLeft)
|
||||
/// .style(Style::default().red().on_white());
|
||||
/// ```
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct Sparkline<'a> {
|
||||
|
@ -40,10 +54,15 @@ pub struct Sparkline<'a> {
|
|||
direction: RenderDirection,
|
||||
}
|
||||
|
||||
/// Defines the direction in which sparkline will be rendered.
|
||||
///
|
||||
/// See [`Sparkline::direction`].
|
||||
#[derive(Debug, Default, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)]
|
||||
pub enum RenderDirection {
|
||||
/// The first value is on the left, going to the right
|
||||
#[default]
|
||||
LeftToRight,
|
||||
/// The first value is on the right, going to the left
|
||||
RightToLeft,
|
||||
}
|
||||
|
||||
|
@ -61,31 +80,58 @@ impl<'a> Default for Sparkline<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Sparkline<'a> {
|
||||
/// Wraps the sparkline with the given `block`.
|
||||
pub fn block(mut self, block: Block<'a>) -> Sparkline<'a> {
|
||||
self.block = Some(block);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the style of the entire widget.
|
||||
///
|
||||
/// The foreground corresponds to the bars while the background is everything else.
|
||||
pub fn style(mut self, style: Style) -> Sparkline<'a> {
|
||||
self.style = style;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the dataset for the sparkline.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use ratatui::{prelude::*, widgets::*};
|
||||
/// # fn ui(frame: &mut Frame) {
|
||||
/// # let area = Rect::default();
|
||||
/// let sparkline = Sparkline::default().data(&[1, 2, 3]);
|
||||
/// frame.render_widget(sparkline, area);
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn data(mut self, data: &'a [u64]) -> Sparkline<'a> {
|
||||
self.data = data;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the maximum value of bars.
|
||||
///
|
||||
/// Every bar will be scaled accordingly. If no max is given, this will be the max in the
|
||||
/// dataset.
|
||||
pub fn max(mut self, max: u64) -> Sparkline<'a> {
|
||||
self.max = Some(max);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the characters used to display the bars.
|
||||
///
|
||||
/// Can be [`symbols::bar::THREE_LEVELS`], [`symbols::bar::NINE_LEVELS`] (default) or a custom
|
||||
/// [`Set`](symbols::bar::Set).
|
||||
pub fn bar_set(mut self, bar_set: symbols::bar::Set) -> Sparkline<'a> {
|
||||
self.bar_set = bar_set;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the direction of the sparkline.
|
||||
///
|
||||
/// [`RenderDirection::LeftToRight`] by default.
|
||||
pub fn direction(mut self, direction: RenderDirection) -> Sparkline<'a> {
|
||||
self.direction = direction;
|
||||
self
|
||||
|
|
Loading…
Reference in a new issue