-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.decorators import login_required | ||
from django.contrib.gis.measure import D | ||
from django.http import JsonResponse | ||
from .models import Trip | ||
|
||
|
||
def use_units(value, units): | ||
if units == get_user_model().IMPERIAL: | ||
return value.ft | ||
else: | ||
return value.m | ||
|
||
|
||
@login_required | ||
def stats_over_time(request): | ||
"""JSON data for a chart showing stats over time""" | ||
qs = Trip.objects.filter(user=request.user).order_by("start") | ||
labels, duration, vert_down, vert_up, surveyed, resurveyed = [], [], [], [], [], [] | ||
accum_duration = 0 | ||
accum_vert_up = D(m=0) | ||
accum_vert_down = D(m=0) | ||
accum_surveyed = D(m=0) | ||
accum_resurveyed = D(m=0) | ||
|
||
for trip in qs: | ||
labels.append(trip.start.strftime("%Y-%m-%d")) | ||
if trip.duration: | ||
accum_duration += trip.duration.total_seconds() / 60 / 60 | ||
duration.append(accum_duration) | ||
|
||
if trip.vert_dist_up: | ||
accum_vert_up += trip.vert_dist_up | ||
vert_up.append(use_units(accum_vert_up, request.user.units)) | ||
|
||
if trip.vert_dist_down: | ||
accum_vert_down += trip.vert_dist_down | ||
vert_down.append(use_units(accum_vert_down, request.user.units)) | ||
|
||
if trip.surveyed_dist: | ||
accum_surveyed += trip.surveyed_dist | ||
surveyed.append(use_units(accum_surveyed, request.user.units)) | ||
|
||
if trip.resurveyed_dist: | ||
accum_resurveyed += trip.resurveyed_dist | ||
resurveyed.append(use_units(accum_resurveyed, request.user.units)) | ||
|
||
return JsonResponse( | ||
data={ | ||
"labels": labels, | ||
"duration": duration, | ||
"vert_up": vert_up, | ||
"vert_down": vert_down, | ||
"surveyed": surveyed, | ||
"resurveyed": resurveyed, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.urls import path | ||
from . import charts | ||
|
||
app_name = "charts" | ||
|
||
urlpatterns = [ | ||
path("duration/", charts.stats_over_time, name="stats_over_time"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,12 @@ | |
{% block title %}Your statistics{% endblock %} | ||
{% block sidebar %}{% include "sidebar_trips.html" %}{% endblock %} | ||
|
||
{% block header_scripts %} | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js"></script> | ||
<script src="https://code.jquery.com/jquery-3.6.4.min.js" | ||
integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=" crossorigin="anonymous"></script> | ||
{% endblock%} | ||
|
||
{% block main %} | ||
|
||
{% if trips %} | ||
|
@@ -397,12 +403,17 @@ <h5>Horizontal distance</h5> | |
</div> | ||
</div> | ||
</div> | ||
{% else %} | ||
|
||
<div class="card"> | ||
<div class="card-header fs-4"> | ||
Statistics | ||
<div class="card mt-4"> | ||
<div class="card-header"> | ||
Statistics over time | ||
</div> | ||
|
||
<div class="card-body"> | ||
<canvas id="stats-over-time" data-url="{% url 'log:charts:stats_over_time' %}"></canvas> | ||
</div> | ||
</div> | ||
{% else %} | ||
|
||
<div class="card-body"> | ||
<p class="m-0"> | ||
|
@@ -414,3 +425,66 @@ <h5>Horizontal distance</h5> | |
{% endif %} | ||
|
||
{% endblock main %} | ||
|
||
{% block footer_scripts %} | ||
<script> | ||
$(function () { | ||
var $statsTimeChart = $("#stats-over-time"); | ||
$.ajax({ | ||
url: $statsTimeChart.data("url"), | ||
success: function (data) { | ||
|
||
var ctx = $statsTimeChart[0].getContext("2d"); | ||
|
||
new Chart(ctx, { | ||
type: 'line', | ||
data: { | ||
labels: data.labels, | ||
datasets: [{ | ||
label: 'Hours', | ||
borderColor: '#008CBA', | ||
backgroundColor: '#008CBA', | ||
data: data.duration, | ||
}, | ||
{ | ||
label: 'Rope climbed ({{ chart_units }})', | ||
borderColor: 'orange', | ||
backgroundColor: 'orange', | ||
data: data.vert_up, | ||
}, | ||
{ | ||
label: 'Rope descended ({{ chart_units }})', | ||
borderColor: 'red', | ||
backgroundColor: 'red', | ||
data: data.vert_down, | ||
}, | ||
{ | ||
label: 'Surveyed ({{ chart_units }})', | ||
borderColor: 'green', | ||
backgroundColor: 'green', | ||
data: data.surveyed, | ||
}, | ||
{ | ||
label: 'Resurveyed ({{ chart_units }})', | ||
borderColor: 'purple', | ||
backgroundColor: 'purple', | ||
data: data.resurveyed, | ||
}], | ||
}, | ||
options: { | ||
responsive: true, | ||
elements: { | ||
point: { | ||
pointStyle: false, | ||
}, | ||
line: { | ||
tension: 0.4, | ||
} | ||
}, | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
</script> | ||
{% endblock footer_scripts %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters