premier graphique ish

This commit is contained in:
thatscringebro
2026-03-20 11:40:39 -04:00
parent a162118fce
commit c9d31f853d
3 changed files with 91 additions and 78 deletions

View File

@@ -45,7 +45,7 @@ impl App {
selected_tab: 0, selected_tab: 0,
tabs: [ tabs: [
"Transactions".to_string(), "Transactions".to_string(),
"Chart".to_string(), "Épicerie".to_string(),
"Chart 2".to_string(), "Chart 2".to_string(),
"Chart 3".to_string(), "Chart 3".to_string(),
] ]
@@ -278,4 +278,8 @@ impl App {
self.selected_tab -= 1; self.selected_tab -= 1;
} }
} }
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);
}
} }

View File

@@ -76,26 +76,26 @@ pub fn upsert_account(con: &Connection, ac: Account) -> Account {
); );
} }
pub fn get_account(con: &Connection, id: i64) -> Account { // pub fn get_account(con: &Connection, id: i64) -> Account {
let query = "SELECT * FROM Accounts WHERE id = ?"; // let query = "SELECT * FROM Accounts WHERE id = ?";
let mut statement = con.prepare(query).unwrap(); // let mut statement = con.prepare(query).unwrap();
statement.bind((1, id)).unwrap(); // statement.bind((1, id)).unwrap();
if let Ok(State::Row) = statement.next() { // if let Ok(State::Row) = statement.next() {
return Account::new( // return Account::new(
statement.read::<i64, _>("id").unwrap(), // statement.read::<i64, _>("id").unwrap(),
statement.read::<String, _>("name").unwrap(), // statement.read::<String, _>("name").unwrap(),
statement.read::<f64, _>("asset_price").unwrap(), // statement.read::<f64, _>("asset_price").unwrap(),
statement // statement
.read::<i64, _>("type") // .read::<i64, _>("type")
.unwrap() // .unwrap()
.try_into() // .try_into()
.unwrap(), // .unwrap(),
); // );
} else { // } else {
return Account::new(0, "".to_string(), 0.0, AccountType::Cash); // return Account::new(0, "".to_string(), 0.0, AccountType::Cash);
} // }
} // }
pub fn get_account_total(id: i64, con: &Connection) -> f64 { pub fn get_account_total(id: i64, con: &Connection) -> f64 {
let mut query = " let mut query = "
@@ -149,30 +149,30 @@ pub fn get_accounts(con: &Connection) -> Vec<Account> {
return vec; return vec;
} }
pub fn upsert_transaction_type(con: &Connection, tt: TransactionType) -> TransactionType { // pub fn upsert_transaction_type(con: &Connection, tt: TransactionType) -> TransactionType {
let query; // let query;
if tt.get_id() == 0 { // if tt.get_id() == 0 {
query = "INSERT INTO TransactionTypes (description) VALUES (?) RETURNING id;"; // query = "INSERT INTO TransactionTypes (description) VALUES (?) RETURNING id;";
} else { // } else {
query = "UPDATE TransactionTypes SET description = ? WHERE id = ? RETURNING id;"; // query = "UPDATE TransactionTypes SET description = ? WHERE id = ? RETURNING id;";
} // }
let mut statement = con.prepare(query).unwrap(); // let mut statement = con.prepare(query).unwrap();
statement.bind((1, &tt.get_description() as &str)).unwrap(); // statement.bind((1, &tt.get_description() as &str)).unwrap();
if tt.get_id() != 0 { // if tt.get_id() != 0 {
statement.bind((2, tt.get_id())).unwrap(); // statement.bind((2, tt.get_id())).unwrap();
} // }
let id; // let id;
if let Ok(State::Row) = statement.next() { // if let Ok(State::Row) = statement.next() {
id = statement.read::<i64, _>("id").unwrap(); // id = statement.read::<i64, _>("id").unwrap();
} else { // } else {
id = 0; // id = 0;
} // }
return TransactionType::new(id, tt.get_description()); // return TransactionType::new(id, tt.get_description());
} // }
pub fn get_transaction_type(con: &Connection, id: i64) -> TransactionType { pub fn get_transaction_type(con: &Connection, id: i64) -> TransactionType {
let query = "SELECT * FROM TransactionTypes WHERE id = ?"; let query = "SELECT * FROM TransactionTypes WHERE id = ?";
@@ -254,26 +254,26 @@ pub fn upsert_transaction(con: &Connection, tr: Transaction) -> Transaction {
); );
} }
pub fn get_transaction(con: &Connection, id: i64) -> Transaction { // pub fn get_transaction(con: &Connection, id: i64) -> Transaction {
let query = "SELECT * FROM Transactions WHERE id = :id"; // let query = "SELECT * FROM Transactions WHERE id = :id";
let mut statement = con.prepare(query).unwrap(); // let mut statement = con.prepare(query).unwrap();
statement.bind((":id", id)).unwrap(); // statement.bind((":id", id)).unwrap();
if let Ok(State::Row) = statement.next() { // if let Ok(State::Row) = statement.next() {
return Transaction::new( // return Transaction::new(
statement.read::<i64, _>("id").unwrap(), // statement.read::<i64, _>("id").unwrap(),
statement.read::<i64, _>("account_id").unwrap(), // statement.read::<i64, _>("account_id").unwrap(),
statement.read::<f64, _>("amount").unwrap(), // statement.read::<f64, _>("amount").unwrap(),
DateTime::from_timestamp(statement.read::<i64, _>("date").unwrap(), 0).unwrap(), // DateTime::from_timestamp(statement.read::<i64, _>("date").unwrap(), 0).unwrap(),
statement.read::<String, _>("description").unwrap(), // statement.read::<String, _>("description").unwrap(),
statement.read::<i64, _>("type_id").unwrap(), // statement.read::<i64, _>("type_id").unwrap(),
statement.read::<f64, _>("asset_amount").unwrap(), // statement.read::<f64, _>("asset_amount").unwrap(),
con, // con,
); // );
} else { // } else {
return Transaction::new_empty(); // return Transaction::new_empty();
} // }
} // }
pub fn get_account_transactions(con: &Connection, ac_id: i64) -> Vec<Transaction> { pub fn get_account_transactions(con: &Connection, ac_id: i64) -> Vec<Transaction> {
if ac_id == 0 { if ac_id == 0 {
@@ -321,3 +321,23 @@ pub fn get_transactions(con: &Connection) -> Vec<Transaction> {
return vec; return vec;
} }
pub fn get_tr_type_price_over_time(con: &Connection, tr_type_id: i64) -> Vec<(f64, f64)> {
let query = "
SELECT strftime('%Y%m', datetime(date, 'unixepoch')) as x, SUM(amount) as y
FROM Transactions
WHERE type_id = :tr_type
GROUP BY x;
";
let mut statement = con.prepare(query).unwrap();
statement.bind((":tr_type", tr_type_id)).unwrap();
let mut data = Vec::<(f64, f64)>::new();
while let Ok(State::Row) = statement.next() {
let x = statement.read::<f64, _>("x").unwrap();
let y = statement.read::<f64, _>("y").unwrap() * -1.0; // Pour passer de dépense dans le négatif à un graphique qui monte
data.push((x, y));
}
return data;
}

View File

@@ -106,33 +106,22 @@ pub fn ui(frame: &mut Frame, app: &mut App) {
&mut app.trx_table.state, &mut app.trx_table.state,
); );
} else if app.selected_tab == 1 { } else if app.selected_tab == 1 {
let slice: &[(f64, f64)] = &app.get_trtype_data(1);
let dataset = Dataset::default() let dataset = Dataset::default()
.name("Stonks")
.marker(Marker::Braille) .marker(Marker::Braille)
.graph_type(GraphType::Line) .graph_type(GraphType::Line)
.style(Color::Blue) .style(Color::Blue)
.data(&[ .data(slice);
(0.0, 1.0),
(1.0, 3.0),
(2.0, 0.5),
(3.0, 2.0),
(4.0, 0.8),
(5.0, 4.0),
(6.0, 1.0),
(7.0, 6.0),
(8.0, 3.0),
(10.0, 10.0),
]);
let x_axis = Axis::default() let x_axis = Axis::default()
.title("Hustle") .title("Temps")
.bounds([0.0, 10.0]) .bounds([202501.0, 202606.0])
.labels(["0%", "50%", "100%"]); .labels(["2025-01", "2026-06"]);
let y_axis = Axis::default() let y_axis = Axis::default()
.title("Profit") .title("Coûts")
.bounds([0.0, 10.0]) .bounds([0.0, 750.0])
.labels(["0", "5", "10"]); .labels(["0", "50", "100", "750"]);
let chart = Chart::new(vec![dataset]) let chart = Chart::new(vec![dataset])
.block(trx_block) .block(trx_block)