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,
tabs: [
"Transactions".to_string(),
"Chart".to_string(),
"Épicerie".to_string(),
"Chart 2".to_string(),
"Chart 3".to_string(),
]
@@ -278,4 +278,8 @@ impl App {
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 {
let query = "SELECT * FROM Accounts WHERE id = ?";
let mut statement = con.prepare(query).unwrap();
statement.bind((1, id)).unwrap();
// pub fn get_account(con: &Connection, id: i64) -> Account {
// let query = "SELECT * FROM Accounts WHERE id = ?";
// let mut statement = con.prepare(query).unwrap();
// statement.bind((1, id)).unwrap();
if let Ok(State::Row) = statement.next() {
return Account::new(
statement.read::<i64, _>("id").unwrap(),
statement.read::<String, _>("name").unwrap(),
statement.read::<f64, _>("asset_price").unwrap(),
statement
.read::<i64, _>("type")
.unwrap()
.try_into()
.unwrap(),
);
} else {
return Account::new(0, "".to_string(), 0.0, AccountType::Cash);
}
}
// if let Ok(State::Row) = statement.next() {
// return Account::new(
// statement.read::<i64, _>("id").unwrap(),
// statement.read::<String, _>("name").unwrap(),
// statement.read::<f64, _>("asset_price").unwrap(),
// statement
// .read::<i64, _>("type")
// .unwrap()
// .try_into()
// .unwrap(),
// );
// } else {
// return Account::new(0, "".to_string(), 0.0, AccountType::Cash);
// }
// }
pub fn get_account_total(id: i64, con: &Connection) -> f64 {
let mut query = "
@@ -149,30 +149,30 @@ pub fn get_accounts(con: &Connection) -> Vec<Account> {
return vec;
}
pub fn upsert_transaction_type(con: &Connection, tt: TransactionType) -> TransactionType {
let query;
if tt.get_id() == 0 {
query = "INSERT INTO TransactionTypes (description) VALUES (?) RETURNING id;";
} else {
query = "UPDATE TransactionTypes SET description = ? WHERE id = ? RETURNING id;";
}
// pub fn upsert_transaction_type(con: &Connection, tt: TransactionType) -> TransactionType {
// let query;
// if tt.get_id() == 0 {
// query = "INSERT INTO TransactionTypes (description) VALUES (?) RETURNING id;";
// } else {
// query = "UPDATE TransactionTypes SET description = ? WHERE id = ? RETURNING id;";
// }
let mut statement = con.prepare(query).unwrap();
statement.bind((1, &tt.get_description() as &str)).unwrap();
// let mut statement = con.prepare(query).unwrap();
// statement.bind((1, &tt.get_description() as &str)).unwrap();
if tt.get_id() != 0 {
statement.bind((2, tt.get_id())).unwrap();
}
// if tt.get_id() != 0 {
// statement.bind((2, tt.get_id())).unwrap();
// }
let id;
if let Ok(State::Row) = statement.next() {
id = statement.read::<i64, _>("id").unwrap();
} else {
id = 0;
}
// let id;
// if let Ok(State::Row) = statement.next() {
// id = statement.read::<i64, _>("id").unwrap();
// } else {
// 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 {
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 {
let query = "SELECT * FROM Transactions WHERE id = :id";
let mut statement = con.prepare(query).unwrap();
statement.bind((":id", id)).unwrap();
// pub fn get_transaction(con: &Connection, id: i64) -> Transaction {
// let query = "SELECT * FROM Transactions WHERE id = :id";
// let mut statement = con.prepare(query).unwrap();
// statement.bind((":id", id)).unwrap();
if let Ok(State::Row) = statement.next() {
return Transaction::new(
statement.read::<i64, _>("id").unwrap(),
statement.read::<i64, _>("account_id").unwrap(),
statement.read::<f64, _>("amount").unwrap(),
DateTime::from_timestamp(statement.read::<i64, _>("date").unwrap(), 0).unwrap(),
statement.read::<String, _>("description").unwrap(),
statement.read::<i64, _>("type_id").unwrap(),
statement.read::<f64, _>("asset_amount").unwrap(),
con,
);
} else {
return Transaction::new_empty();
}
}
// if let Ok(State::Row) = statement.next() {
// return Transaction::new(
// statement.read::<i64, _>("id").unwrap(),
// statement.read::<i64, _>("account_id").unwrap(),
// statement.read::<f64, _>("amount").unwrap(),
// DateTime::from_timestamp(statement.read::<i64, _>("date").unwrap(), 0).unwrap(),
// statement.read::<String, _>("description").unwrap(),
// statement.read::<i64, _>("type_id").unwrap(),
// statement.read::<f64, _>("asset_amount").unwrap(),
// con,
// );
// } else {
// return Transaction::new_empty();
// }
// }
pub fn get_account_transactions(con: &Connection, ac_id: i64) -> Vec<Transaction> {
if ac_id == 0 {
@@ -321,3 +321,23 @@ pub fn get_transactions(con: &Connection) -> Vec<Transaction> {
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,
);
} else if app.selected_tab == 1 {
let slice: &[(f64, f64)] = &app.get_trtype_data(1);
let dataset = Dataset::default()
.name("Stonks")
.marker(Marker::Braille)
.graph_type(GraphType::Line)
.style(Color::Blue)
.data(&[
(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),
]);
.data(slice);
let x_axis = Axis::default()
.title("Hustle")
.bounds([0.0, 10.0])
.labels(["0%", "50%", "100%"]);
.title("Temps")
.bounds([202501.0, 202606.0])
.labels(["2025-01", "2026-06"]);
let y_axis = Axis::default()
.title("Profit")
.bounds([0.0, 10.0])
.labels(["0", "5", "10"]);
.title("Coûts")
.bounds([0.0, 750.0])
.labels(["0", "50", "100", "750"]);
let chart = Chart::new(vec![dataset])
.block(trx_block)