From 35379214e90370b4e7a6ff4da4f59265e7a26c82 Mon Sep 17 00:00:00 2001 From: thatscringebro Date: Tue, 31 Mar 2026 13:59:58 -0400 Subject: [PATCH] chart by tr_type --- src/app.rs | 29 +++++++++++++++++++++++++---- src/main.rs | 12 +++++++++++- src/ui.rs | 30 +++++++++++++++++++++++++++--- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/app.rs b/src/app.rs index 2715b9a..7589212 100644 --- a/src/app.rs +++ b/src/app.rs @@ -18,6 +18,8 @@ pub struct App { pub tabs: Vec, pub error: String, pub error_showing: bool, + pub selected_tr_type_chart: i64, + pub selected_tr_type_chart_desc: String, pub connection: Connection, } @@ -32,6 +34,7 @@ impl App { }; data_layer::setup(&con); + let tr_types = data_layer::get_transaction_types(&con); return App { current_screen: CurrentScreen::Main, @@ -43,17 +46,24 @@ impl App { selected_transaction_input: TransactionInput::Type, selected_account_input: AccountInput::Asset, current_input: String::new(), - tr_types: data_layer::get_transaction_types(&con), + tr_types: tr_types.clone(), selected_tab: 0, tabs: [ "Transactions".to_string(), - "Épicerie".to_string(), + "Amnt by Type".to_string(), "Chart 2".to_string(), "Chart 3".to_string(), ] .to_vec(), error: String::new(), error_showing: false, + selected_tr_type_chart: 1, + selected_tr_type_chart_desc: tr_types + .iter() + .filter(|x| x.get_id() == 1) + .next() + .unwrap() + .get_description(), connection: con, }; } @@ -295,11 +305,22 @@ impl App { } } - pub fn get_trtype_data(&self, tr_type_id: i64) -> Vec<(f64, f64)> { + pub fn get_trtype_data(&self) -> Vec<(f64, f64)> { return data_layer::get_tr_type_price_over_time( &self.connection, self.acc_list.get_selected_id(), - tr_type_id, + self.selected_tr_type_chart, ); } + + pub fn select_tr_type_chart(&mut self, id: i64) { + self.selected_tr_type_chart = id; + self.selected_tr_type_chart_desc = self + .tr_types + .iter() + .filter(|x| x.get_id() == id) + .next() + .unwrap() + .get_description(); + } } diff --git a/src/main.rs b/src/main.rs index 398f57b..16463d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -132,7 +132,17 @@ where app.set_current_input_tr(); } }, - _ => {} + _ => { + if let CurrentWidget::TrxList = app.current_widget + && app.selected_tab == 1 + { + let char_u = key.code.to_string().parse::(); + match char_u { + Ok(c) => app.select_tr_type_chart(c), + _ => {} + } + } + } }, CurrentScreen::NewAccount => match key.code { KeyCode::Down => { diff --git a/src/ui.rs b/src/ui.rs index 42ea8b6..0012f9d 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -106,7 +106,7 @@ pub fn ui(frame: &mut Frame, app: &mut App) { &mut app.trx_table.state, ); } else if app.selected_tab == 1 { - let slice: &[(f64, f64)] = &app.get_trtype_data(1); + let slice: &[(f64, f64)] = &app.get_trtype_data(); let dataset = Dataset::default() .marker(Marker::Braille) .graph_type(GraphType::Line) @@ -151,10 +151,34 @@ pub fn ui(frame: &mut Frame, app: &mut App) { .labels([min_y.to_string(), max_y.to_string()]); let chart = Chart::new(vec![dataset]) - .block(trx_block) + .block(trx_block.clone()) .x_axis(x_axis) .y_axis(y_axis); frame.render_widget(chart, right_layout[1]); + + let p = Paragraph::new(app.selected_tr_type_chart_desc.clone()) + .block(trx_block) + .style(Style::default()) + .alignment(ratatui::layout::HorizontalAlignment::Center); + frame.render_widget(p, right_layout[1]); + + let helper = Block::default() + .title("Helper") + .borders(Borders::all()) + .style(Style::default()); + + let helper_string: String = app + .tr_types + .iter() + .map(|tr_type| tr_type.get_id().to_string() + " " + &tr_type.get_description() + "\n") + .collect(); + let helper_text = Text::styled(helper_string, Style::default()); + let helper_paragraph = Paragraph::new(helper_text) + .block(helper) + .wrap(Wrap { trim: false }); + let area = helper_rect(frame.area()); + frame.render_widget(Clear, area); + frame.render_widget(helper_paragraph, area); } else { let p = Paragraph::new("Tab not implemented") .block(trx_block) @@ -346,7 +370,7 @@ fn helper_rect(r: Rect) -> Rect { return Layout::default() .direction(Direction::Horizontal) - .constraints([Constraint::Percentage(30), Constraint::Percentage(70)]) + .constraints([Constraint::Percentage(20), Constraint::Percentage(70)]) .split(popup_layout[1])[0]; }