Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Graphs to Statistics tab #1011

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ihatemoney/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def members_stats(self):
balance, spent, paid = self.full_balance
return [
{
"member": member,
"member": member._to_serialize,
"paid": paid[member.id],
"spent": spent[member.id],
"balance": balance[member.id],
Expand Down
7 changes: 7 additions & 0 deletions ihatemoney/static/js/chart.min.js

Large diffs are not rendered by default.

132 changes: 132 additions & 0 deletions ihatemoney/static/js/graphs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
function createGraphs(members_stats, monthly_stats) {
let names = [];
let balance = [];
let colors = [];
for (let i = 0; i < members_stats.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably use a for…in statement instead.

names = names.concat(members_stats[i].member.name)
balance = balance.concat(members_stats[i].balance)
if(members_stats[i].balance < 0){
colors = colors.concat('rgba(255, 0, 0, .5)')
}
else{
colors = colors.concat('rgba(0, 128, 0, .5)')
}
}

let spent = [];
let month = [];
for (const [year, month1] of Object.entries(monthly_stats)) {
for (const [month2, value] of Object.entries(month1)) {
spent = spent.concat(value);
month = month.concat(month2);
}
}
month = month.slice(-12);
spent = spent.slice(-12);
for (let i = 0; i < month.length; i++) {
if(month[i] == 1){
month[i] = 'Jan';
}
else if(month[i] == 2){
month[i] = 'Feb';
}
else if(month[i] == 3){
month[i] = 'Mar';
}
else if(month[i] == 4){
month[i] = 'Apr';
}
else if(month[i] == 5){
month[i] = 'May';
}
else if(month[i] == 6){
month[i] = 'June';
}
else if(month[i] == 7){
month[i] = 'July';
}
else if(month[i] == 8){
month[i] = 'Aug';
}
else if(month[i] == 9){
month[i] = 'Sept';
}
else if(month[i] == 10){
month[i] = 'Oct';
}
else if(month[i] == 11){
month[i] = 'Nov';
}
else if(month[i] == 12){
month[i] = 'Dec';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that it isn't translatable. Any idea on how we could achieve this?

}
}


const ctx = document.getElementById('balance').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: names,
datasets: [{
label: '',
data: balance,
backgroundColor: colors,
borderColor: colors,
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
},
title: {
display: true,
text: 'Balance',
fontSize: 25,
fontColor: 'rgb(0,0,0)'
},
legend:{
display: false
},
scales: {
xAxes: [{
ticks: {
fontSize: 15
}
}]
},
responsive: false

}
});

const chart2 = document.getElementById('monthlySpending').getContext('2d');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could rename this monthlyExpensesGraph

const myChart2 = new Chart(chart2, {
type: 'line',
data: {
labels: month,
datasets: [{
label: '',
data: spent,
fill: false,
lineTension: .1,
borderColor: 'rgb(128,128,128)'
}]
},
options: {
title: {
display: true,
text: 'Expenses Per Month in the Past Year',
fontSize: 25,
fontColor: 'rgb(0,0,0)'
},
legend:{
display: false
},
responsive: false
}
});
}
2 changes: 2 additions & 0 deletions ihatemoney/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel=stylesheet type=text/css href="{{ url_for("static", filename='css/main.css') }}">
<script src="{{ url_for("static", filename='js/chart.min.js')}}"></script>
{% block css %}{% endblock %}
<script src="{{ url_for("static", filename="js/jquery-3.6.0.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/ihatemoney.js") }}"></script>
<script src="{{ url_for("static", filename="js/tether.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/popper.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/tagsinput.js") }}"></script>
<script src="{{ url_for("static", filename="js/bootstrap.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/graphs.js") }}"></script>
{%- if request.path == "/dashboard" %}
<link rel=stylesheet type=text/css href="{{ url_for("static", filename='css/datatables.min.css') }}">
<script src="{{ url_for("static", filename="js/datatables.min.js") }}"></script>
Expand Down
6 changes: 6 additions & 0 deletions ihatemoney/templates/statistics.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ <h2>{{ _("Expenses by Month") }}</h2>
{% endfor %}
</tbody>
</table>
<h2>Graphs</h2>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make this translatable.

<canvas id="balance" height="400"></canvas>
<canvas id="monthlySpending" height="400"></canvas>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the fixed width: what happens when we view this from a mobile for instance?

<script>
createGraphs({{ members_stats|tojson }}, {{ monthly_stats|tojson }});
</script>
</div>

{% endblock %}