adding percentages
This commit is contained in:
@@ -69,8 +69,14 @@ class TrainingZonesForm(forms.Form):
|
||||
('week', 'By Week'),
|
||||
)
|
||||
|
||||
yaxischoices = (
|
||||
('time','Time'),
|
||||
('percentage','Percentage of Time')
|
||||
)
|
||||
|
||||
zones = forms.ChoiceField(initial='hr',label='Training Zones',choices=zoneschoices)
|
||||
dates = forms.ChoiceField(initial='week',label='Date Aggregation', choices=datechoices)
|
||||
yaxis = forms.ChoiceField(initial='time',label='Y axis',choices=yaxischoices)
|
||||
startdate = forms.DateField(
|
||||
initial=timezone.now()-datetime.timedelta(days=42),
|
||||
widget=AdminDateWidget(), #format='%Y-%m-%d'),
|
||||
|
||||
@@ -6583,7 +6583,8 @@ def interactive_otw_advanced_pace_chart(id=0,promember=0):
|
||||
|
||||
return [script,div]
|
||||
|
||||
def get_zones_report(rower,startdate,enddate,trainingzones='hr',date_agg='week'):
|
||||
def get_zones_report(rower,startdate,enddate,trainingzones='hr',date_agg='week',
|
||||
yaxis='time'):
|
||||
duration = enddate-startdate
|
||||
|
||||
totaldays = duration.total_seconds()/(24*3600)
|
||||
@@ -6594,6 +6595,7 @@ def get_zones_report(rower,startdate,enddate,trainingzones='hr',date_agg='week')
|
||||
hours = []
|
||||
zones = []
|
||||
|
||||
|
||||
workouts = Workout.objects.filter(
|
||||
user=rower,
|
||||
startdatetime__gte=startdate,
|
||||
@@ -6790,7 +6792,8 @@ def get_zones_report(rower,startdate,enddate,trainingzones='hr',date_agg='week')
|
||||
|
||||
return data
|
||||
|
||||
def interactive_zoneschart(rower,data,startdate,enddate,trainingzones='hr',date_agg='week'):
|
||||
def interactive_zoneschart(rower,data,startdate,enddate,trainingzones='hr',date_agg='week',
|
||||
yaxis='time'):
|
||||
duration = enddate-startdate
|
||||
|
||||
totaldays = duration.total_seconds()/(24*3600)
|
||||
@@ -6850,6 +6853,23 @@ def interactive_zoneschart(rower,data,startdate,enddate,trainingzones='hr',date_
|
||||
|
||||
df.sort_values('date_sorting',inplace=True)
|
||||
df.drop('date_sorting',inplace=True,axis='columns')
|
||||
df['totaltime'] = 0
|
||||
|
||||
if yaxis == 'percentage':
|
||||
dates = list(set(df['date'].values))
|
||||
for date in dates:
|
||||
qry = 'date == "{d}"'.format(d=date)
|
||||
|
||||
totaltime = df.query(qry)['hours'].sum()
|
||||
|
||||
mask = df['date'] == date
|
||||
df.loc[mask,'totaltime'] = totaltime
|
||||
|
||||
df['percentage'] = 100.*df['hours']/df['totaltime']
|
||||
df.drop('hours',inplace=True,axis='columns')
|
||||
df.drop('totaltime',inplace=True,axis='columns')
|
||||
print(df.head())
|
||||
|
||||
|
||||
hv.extension('bokeh')
|
||||
|
||||
@@ -6882,6 +6902,9 @@ def interactive_zoneschart(rower,data,startdate,enddate,trainingzones='hr',date_
|
||||
else: # pragma: no cover
|
||||
p.xaxis.axis_label = 'Month'
|
||||
|
||||
if yaxis == 'percentage':
|
||||
p.yaxis.axis_label = 'Percentage'
|
||||
|
||||
p.plot_width=550
|
||||
p.plot_height=350
|
||||
p.toolbar_location = 'above'
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
<script>
|
||||
$(function($) {
|
||||
console.log('loading script for chart');
|
||||
$.getJSON(window.location.protocol + '//'+window.location.host + '/rowers/trainingzones/user/{{ rower.user.id }}/data/?startdate={{ startdate|date:"Y-m-d" }}&enddate={{ enddate|date:"Y-m-d" }}&zones={{ zones }}&dates={{ dates }}', function(json) {
|
||||
$.getJSON(window.location.protocol + '//'+window.location.host + '/rowers/trainingzones/user/{{ rower.user.id }}/data/?startdate={{ startdate|date:"Y-m-d" }}&enddate={{ enddate|date:"Y-m-d" }}&zones={{ zones }}&dates={{ dates }}&yaxis={{ yaxis }}', function(json) {
|
||||
var script = json.script;
|
||||
var div = json.div;
|
||||
$("#id_sitready").remove();
|
||||
|
||||
@@ -1064,6 +1064,7 @@ def trainingzones_view(request,userid=0):
|
||||
startdate = enddate-datetime.timedelta(days=42)
|
||||
zones = 'hr'
|
||||
date_agg = 'week'
|
||||
yaxis = 'time'
|
||||
|
||||
|
||||
form = TrainingZonesForm({
|
||||
@@ -1071,6 +1072,7 @@ def trainingzones_view(request,userid=0):
|
||||
'enddate':enddate,
|
||||
'zones':zones,
|
||||
'dates':date_agg,
|
||||
'yaxis':yaxis,
|
||||
})
|
||||
|
||||
|
||||
@@ -1082,7 +1084,7 @@ def trainingzones_view(request,userid=0):
|
||||
enddate = form.cleaned_data['enddate']
|
||||
zones = form.cleaned_data['zones']
|
||||
date_agg = form.cleaned_data['dates']
|
||||
|
||||
yaxis = form.cleaned_data['yaxis']
|
||||
|
||||
if date_agg == 'week':
|
||||
startdate = startdate - datetime.timedelta(days = startdate.weekday())
|
||||
@@ -1094,6 +1096,7 @@ def trainingzones_view(request,userid=0):
|
||||
'enddate':enddate,
|
||||
'zones':zones,
|
||||
'dates':date_agg,
|
||||
'yaxis':yaxis,
|
||||
})
|
||||
|
||||
script = ''
|
||||
@@ -1122,6 +1125,7 @@ def trainingzones_view(request,userid=0):
|
||||
'enddate':enddate,
|
||||
'zones':zones,
|
||||
'dates': date_agg,
|
||||
'yaxis': yaxis,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1133,12 +1137,13 @@ def trainingzones_view_data(request,userid=0):
|
||||
enddate = timezone.now()
|
||||
zones = 'hr'
|
||||
date_agg = 'week'
|
||||
yaxis = 'time'
|
||||
|
||||
if request.GET.get('zones'):
|
||||
zones = request.GET.get('zones')
|
||||
zones = request.GET.get('zones',zones)
|
||||
|
||||
if request.GET.get('dates'):
|
||||
date_agg = request.GET.get('dates')
|
||||
date_agg = request.GET.get('dates',date_agg)
|
||||
|
||||
yaxis = request.GET.get('yaxis',yaxis)
|
||||
|
||||
if request.GET.get('startdate'):
|
||||
startdate = datetime.datetime.strptime(request.GET.get('startdate'),"%Y-%m-%d")
|
||||
@@ -1148,9 +1153,10 @@ def trainingzones_view_data(request,userid=0):
|
||||
enddate = datetime.datetime.strptime(request.GET.get('enddate'),"%Y-%m-%d")
|
||||
enddate = arrow.get(enddate).datetime
|
||||
|
||||
data = get_zones_report(r,startdate,enddate,trainingzones=zones,date_agg=date_agg)
|
||||
|
||||
script, div = interactive_zoneschart(r,data,startdate,enddate,trainingzones=zones,date_agg=date_agg)
|
||||
data = get_zones_report(r,startdate,enddate,trainingzones=zones,date_agg=date_agg,yaxis=yaxis)
|
||||
|
||||
script, div = interactive_zoneschart(r,data,startdate,enddate,trainingzones=zones,date_agg=date_agg,yaxis=yaxis)
|
||||
|
||||
return JSONResponse({
|
||||
'script': script,
|
||||
|
||||
Reference in New Issue
Block a user