2024-03-02 09:06:53 +00:00
|
|
|
//! # [Ratatui] `BarChart` example
|
2024-01-24 19:50:18 +00:00
|
|
|
//!
|
|
|
|
//! The latest version of this example is available in the [examples] folder in the repository.
|
|
|
|
//!
|
|
|
|
//! Please note that the examples are designed to be run against the `main` branch of the Github
|
|
|
|
//! repository. This means that you may not be able to compile with the latest release version on
|
|
|
|
//! crates.io, or the one that you have installed locally.
|
|
|
|
//!
|
|
|
|
//! See the [examples readme] for more information on finding examples that match the version of the
|
|
|
|
//! library you are using.
|
|
|
|
//!
|
|
|
|
//! [Ratatui]: https://github.com/ratatui-org/ratatui
|
|
|
|
//! [examples]: https://github.com/ratatui-org/ratatui/blob/main/examples
|
|
|
|
//! [examples readme]: https://github.com/ratatui-org/ratatui/blob/main/examples/README.md
|
|
|
|
|
2023-06-12 05:07:15 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
io,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2024-03-02 09:06:53 +00:00
|
|
|
use ratatui::{
|
|
|
|
prelude::*,
|
2024-05-02 10:09:48 +00:00
|
|
|
widgets::{Bar, BarChart, BarGroup, Block, Paragraph},
|
2024-03-02 09:06:53 +00:00
|
|
|
};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2023-07-13 10:27:08 +00:00
|
|
|
struct Company<'a> {
|
|
|
|
revenue: [u64; 4],
|
|
|
|
label: &'a str,
|
|
|
|
bar_style: Style,
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App<'a> {
|
|
|
|
data: Vec<(&'a str, u64)>,
|
2023-07-13 10:27:08 +00:00
|
|
|
months: [&'a str; 4],
|
|
|
|
companies: [Company<'a>; 3],
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 10:27:08 +00:00
|
|
|
const TOTAL_REVENUE: &str = "Total Revenue";
|
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
impl<'a> App<'a> {
|
2024-03-02 09:06:53 +00:00
|
|
|
fn new() -> Self {
|
2016-11-07 23:35:46 +00:00
|
|
|
App {
|
2017-09-11 05:58:37 +00:00
|
|
|
data: vec![
|
|
|
|
("B1", 9),
|
|
|
|
("B2", 12),
|
|
|
|
("B3", 5),
|
|
|
|
("B4", 8),
|
|
|
|
("B5", 2),
|
|
|
|
("B6", 4),
|
|
|
|
("B7", 5),
|
|
|
|
("B8", 9),
|
|
|
|
("B9", 14),
|
|
|
|
("B10", 15),
|
|
|
|
("B11", 1),
|
|
|
|
("B12", 0),
|
|
|
|
("B13", 4),
|
|
|
|
("B14", 6),
|
|
|
|
("B15", 4),
|
|
|
|
("B16", 6),
|
|
|
|
("B17", 4),
|
|
|
|
("B18", 7),
|
|
|
|
("B19", 13),
|
|
|
|
("B20", 8),
|
|
|
|
("B21", 11),
|
|
|
|
("B22", 9),
|
|
|
|
("B23", 3),
|
|
|
|
("B24", 5),
|
|
|
|
],
|
2023-07-13 10:27:08 +00:00
|
|
|
companies: [
|
|
|
|
Company {
|
|
|
|
label: "Comp.A",
|
2023-07-14 04:38:54 +00:00
|
|
|
revenue: [9500, 12500, 5300, 8500],
|
2023-07-13 10:27:08 +00:00
|
|
|
bar_style: Style::default().fg(Color::Green),
|
|
|
|
},
|
|
|
|
Company {
|
|
|
|
label: "Comp.B",
|
2023-08-24 22:26:15 +00:00
|
|
|
revenue: [1500, 2500, 3000, 500],
|
2023-07-13 10:27:08 +00:00
|
|
|
bar_style: Style::default().fg(Color::Yellow),
|
|
|
|
},
|
|
|
|
Company {
|
|
|
|
label: "Comp.C",
|
2023-07-14 04:38:54 +00:00
|
|
|
revenue: [10500, 10600, 9000, 4200],
|
2023-07-13 10:27:08 +00:00
|
|
|
bar_style: Style::default().fg(Color::White),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
months: ["Mars", "Apr", "May", "Jun"],
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn on_tick(&mut self) {
|
2016-11-07 23:35:46 +00:00
|
|
|
let value = self.data.pop().unwrap();
|
|
|
|
self.data.insert(0, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let tick_rate = Duration::from_millis(250);
|
|
|
|
let app = App::new();
|
|
|
|
let res = run_app(&mut terminal, app, tick_rate);
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
2019-12-15 20:38:18 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2019-12-15 20:38:18 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-12-15 20:38:18 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn run_app<B: Backend>(
|
|
|
|
terminal: &mut Terminal<B>,
|
|
|
|
mut app: App,
|
|
|
|
tick_rate: Duration,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
let mut last_tick = Instant::now();
|
|
|
|
loop {
|
2021-12-23 17:46:25 +00:00
|
|
|
terminal.draw(|f| ui(f, &app))?;
|
2017-05-08 19:34:41 +00:00
|
|
|
|
2023-10-20 19:31:52 +00:00
|
|
|
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
|
2021-11-01 21:27:46 +00:00
|
|
|
if crossterm::event::poll(timeout)? {
|
|
|
|
if let Event::Key(key) = event::read()? {
|
2024-03-02 09:06:53 +00:00
|
|
|
if key.code == KeyCode::Char('q') {
|
2021-12-23 17:46:25 +00:00
|
|
|
return Ok(());
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
app.on_tick();
|
|
|
|
last_tick = Instant::now();
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2024-01-05 15:45:14 +00:00
|
|
|
fn ui(frame: &mut Frame, app: &App) {
|
|
|
|
let vertical = Layout::vertical([Constraint::Ratio(1, 3), Constraint::Ratio(2, 3)]);
|
|
|
|
let horizontal = Layout::horizontal([Constraint::Percentage(50), Constraint::Percentage(50)]);
|
2024-02-02 04:26:35 +00:00
|
|
|
let [top, bottom] = vertical.areas(frame.size());
|
|
|
|
let [left, right] = horizontal.areas(bottom);
|
2023-07-13 10:27:08 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
let barchart = BarChart::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Data1"))
|
2021-11-01 21:27:46 +00:00
|
|
|
.data(&app.data)
|
|
|
|
.bar_width(9)
|
|
|
|
.bar_style(Style::default().fg(Color::Yellow))
|
|
|
|
.value_style(Style::default().fg(Color::Black).bg(Color::Yellow));
|
|
|
|
|
2024-01-05 15:45:14 +00:00
|
|
|
frame.render_widget(barchart, top);
|
|
|
|
draw_bar_with_group_labels(frame, app, left);
|
|
|
|
draw_horizontal_bars(frame, app, right);
|
2023-07-13 10:27:08 +00:00
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::cast_precision_loss)]
|
2023-08-24 22:26:15 +00:00
|
|
|
fn create_groups<'a>(app: &'a App, combine_values_and_labels: bool) -> Vec<BarGroup<'a>> {
|
|
|
|
app.months
|
2023-07-13 10:27:08 +00:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, &month)| {
|
|
|
|
let bars: Vec<Bar> = app
|
|
|
|
.companies
|
|
|
|
.iter()
|
|
|
|
.map(|c| {
|
|
|
|
let mut bar = Bar::default()
|
|
|
|
.value(c.revenue[i])
|
|
|
|
.style(c.bar_style)
|
|
|
|
.value_style(
|
|
|
|
Style::default()
|
|
|
|
.bg(c.bar_style.fg.unwrap())
|
|
|
|
.fg(Color::Black),
|
2023-08-24 22:26:15 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if combine_values_and_labels {
|
|
|
|
bar = bar.text_value(format!(
|
|
|
|
"{} ({:.1} M)",
|
|
|
|
c.label,
|
|
|
|
(c.revenue[i] as f64) / 1000.
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
bar = bar
|
|
|
|
.text_value(format!("{:.1}", (c.revenue[i] as f64) / 1000.))
|
|
|
|
.label(c.label.into());
|
2023-07-13 10:27:08 +00:00
|
|
|
}
|
|
|
|
bar
|
|
|
|
})
|
|
|
|
.collect();
|
2023-08-24 22:26:15 +00:00
|
|
|
BarGroup::default()
|
2024-01-22 17:30:01 +00:00
|
|
|
.label(Line::from(month).centered())
|
2023-08-24 22:26:15 +00:00
|
|
|
.bars(&bars)
|
2023-07-13 10:27:08 +00:00
|
|
|
})
|
2023-08-24 22:26:15 +00:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::cast_possible_truncation)]
|
2023-09-26 05:30:36 +00:00
|
|
|
fn draw_bar_with_group_labels(f: &mut Frame, app: &App, area: Rect) {
|
2024-03-02 09:06:53 +00:00
|
|
|
const LEGEND_HEIGHT: u16 = 6;
|
|
|
|
|
2023-08-24 22:26:15 +00:00
|
|
|
let groups = create_groups(app, false);
|
2021-11-01 21:27:46 +00:00
|
|
|
|
2023-07-13 10:27:08 +00:00
|
|
|
let mut barchart = BarChart::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Data1"))
|
2021-11-01 21:27:46 +00:00
|
|
|
.bar_width(7)
|
2023-07-13 10:27:08 +00:00
|
|
|
.group_gap(3);
|
|
|
|
|
|
|
|
for group in groups {
|
2024-03-02 09:06:53 +00:00
|
|
|
barchart = barchart.data(group);
|
2023-07-13 10:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.render_widget(barchart, area);
|
|
|
|
|
|
|
|
if area.height >= LEGEND_HEIGHT && area.width >= TOTAL_REVENUE.len() as u16 + 2 {
|
2023-08-24 22:26:15 +00:00
|
|
|
let legend_width = TOTAL_REVENUE.len() as u16 + 2;
|
|
|
|
let legend_area = Rect {
|
|
|
|
height: LEGEND_HEIGHT,
|
|
|
|
width: legend_width,
|
|
|
|
y: area.y,
|
|
|
|
x: area.right() - legend_width,
|
|
|
|
};
|
|
|
|
draw_legend(f, legend_area);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::cast_possible_truncation)]
|
2023-09-26 05:30:36 +00:00
|
|
|
fn draw_horizontal_bars(f: &mut Frame, app: &App, area: Rect) {
|
2024-03-02 09:06:53 +00:00
|
|
|
const LEGEND_HEIGHT: u16 = 6;
|
|
|
|
|
2023-08-24 22:26:15 +00:00
|
|
|
let groups = create_groups(app, true);
|
|
|
|
|
|
|
|
let mut barchart = BarChart::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Data1"))
|
2023-08-24 22:26:15 +00:00
|
|
|
.bar_width(1)
|
|
|
|
.group_gap(1)
|
|
|
|
.bar_gap(0)
|
|
|
|
.direction(Direction::Horizontal);
|
|
|
|
|
|
|
|
for group in groups {
|
2024-03-02 09:06:53 +00:00
|
|
|
barchart = barchart.data(group);
|
2023-08-24 22:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.render_widget(barchart, area);
|
|
|
|
|
|
|
|
if area.height >= LEGEND_HEIGHT && area.width >= TOTAL_REVENUE.len() as u16 + 2 {
|
|
|
|
let legend_width = TOTAL_REVENUE.len() as u16 + 2;
|
2023-07-13 10:27:08 +00:00
|
|
|
let legend_area = Rect {
|
|
|
|
height: LEGEND_HEIGHT,
|
2023-08-24 22:26:15 +00:00
|
|
|
width: legend_width,
|
2023-07-13 10:27:08 +00:00
|
|
|
y: area.y,
|
2023-08-24 22:26:15 +00:00
|
|
|
x: area.right() - legend_width,
|
2023-07-13 10:27:08 +00:00
|
|
|
};
|
|
|
|
draw_legend(f, legend_area);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 05:30:36 +00:00
|
|
|
fn draw_legend(f: &mut Frame, area: Rect) {
|
2023-07-13 10:27:08 +00:00
|
|
|
let text = vec![
|
|
|
|
Line::from(Span::styled(
|
|
|
|
TOTAL_REVENUE,
|
2021-11-01 21:27:46 +00:00
|
|
|
Style::default()
|
2023-07-13 10:27:08 +00:00
|
|
|
.add_modifier(Modifier::BOLD)
|
|
|
|
.fg(Color::White),
|
|
|
|
)),
|
|
|
|
Line::from(Span::styled(
|
|
|
|
"- Company A",
|
|
|
|
Style::default().fg(Color::Green),
|
|
|
|
)),
|
|
|
|
Line::from(Span::styled(
|
|
|
|
"- Company B",
|
|
|
|
Style::default().fg(Color::Yellow),
|
|
|
|
)),
|
2024-05-14 01:13:46 +00:00
|
|
|
Line::from(Span::styled(
|
2023-07-13 10:27:08 +00:00
|
|
|
"- Company C",
|
|
|
|
Style::default().fg(Color::White),
|
2024-05-14 01:13:46 +00:00
|
|
|
)),
|
2023-07-13 10:27:08 +00:00
|
|
|
];
|
|
|
|
|
2024-05-02 10:09:48 +00:00
|
|
|
let block = Block::bordered().style(Style::default().fg(Color::White));
|
2023-07-13 10:27:08 +00:00
|
|
|
let paragraph = Paragraph::new(text).block(block);
|
|
|
|
f.render_widget(paragraph, area);
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|