diff --git a/src/app.rs b/src/app.rs index 6be0c2a..36091d7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -280,6 +280,10 @@ impl App { } pub fn get_trtype_data(&self, tr_type_id: i64) -> Vec<(f64, f64)> { - return data_layer::get_tr_type_price_over_time(&self.connection, tr_type_id); + return data_layer::get_tr_type_price_over_time( + &self.connection, + self.acc_list.get_selected_id(), + tr_type_id, + ); } } diff --git a/src/data_layer.rs b/src/data_layer.rs index a350bab..c5365e3 100644 --- a/src/data_layer.rs +++ b/src/data_layer.rs @@ -322,15 +322,27 @@ pub fn get_transactions(con: &Connection) -> Vec { return vec; } -pub fn get_tr_type_price_over_time(con: &Connection, tr_type_id: i64) -> Vec<(f64, f64)> { - let query = " +pub fn get_tr_type_price_over_time( + con: &Connection, + ac_id: i64, + tr_type_id: i64, +) -> Vec<(f64, f64)> { + let mut query = " SELECT strftime('%Y%m', datetime(date, 'unixepoch')) as x, SUM(amount) as y FROM Transactions WHERE type_id = :tr_type -GROUP BY x; - "; + " + .to_owned(); + if ac_id != 0 { + query.push_str(" AND account_id = :ac_id"); + } + query.push_str(" GROUP BY x;"); + let mut statement = con.prepare(query).unwrap(); statement.bind((":tr_type", tr_type_id)).unwrap(); + if ac_id != 0 { + statement.bind((":ac_id", ac_id)).unwrap(); + } let mut data = Vec::<(f64, f64)>::new(); while let Ok(State::Row) = statement.next() { diff --git a/src/ui.rs b/src/ui.rs index c2d6c07..5bc7fb1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -113,15 +113,43 @@ pub fn ui(frame: &mut Frame, app: &mut App) { .style(Color::Blue) .data(slice); + let min_x = slice + .into_iter() + .map(|&(x, _)| x) + .min_by(|a, b| a.partial_cmp(b).unwrap()) + .unwrap() + .round() + - 1.0; + let max_x = slice + .into_iter() + .map(|&(x, _)| x) + .max_by(|a, b| a.partial_cmp(b).unwrap()) + .unwrap() + .round() + + 1.0; let x_axis = Axis::default() .title("Temps") - .bounds([202501.0, 202606.0]) - .labels(["2025-01", "2026-06"]); + .bounds([min_x, max_x]) + .labels([min_x.to_string(), max_x.to_string()]); + let min_y = slice + .into_iter() + .map(|&(_, y)| y) + .min_by(|a, b| a.partial_cmp(b).unwrap()) + .unwrap() + .round() + - 1.0; + let max_y = slice + .into_iter() + .map(|&(_, y)| y) + .max_by(|a, b| a.partial_cmp(b).unwrap()) + .unwrap() + .round() + + 1.0; let y_axis = Axis::default() .title("Coƻts") - .bounds([0.0, 750.0]) - .labels(["0", "50", "100", "750"]); + .bounds([min_y, max_y]) + .labels([min_y.to_string(), max_y.to_string()]); let chart = Chart::new(vec![dataset]) .block(trx_block)