Private
Public Access
1
0

v1 instroke interactive chart

This commit is contained in:
2024-04-01 16:35:20 +02:00
parent 7f3fea5f37
commit 3792a6f7b6
4 changed files with 34 additions and 127 deletions

View File

@@ -2864,137 +2864,43 @@ def instroke_interactive_chart(df,metric, workout, spm_min, spm_max,
except TypeError: except TypeError:
pass pass
TOOLS = 'save,pan,box_zoom,wheel_zoom,reset,tap,crosshair'
plot = figure(width=920,tools=TOOLS,
toolbar_location='above',
toolbar_sticky=False)
#plot.sizing_mode = 'stretch_both'
plot.title.text = str(workout) + ' - ' + metric
# add watermark
watermarkurl = "/static/img/logo7.png"
watermarkrange = Range1d(start=0, end=1)
watermarkalpha = 0.6
watermarkx = 0.99
watermarky = 0.01
watermarkw = 184
watermarkh = 35
watermarkanchor = 'bottom_right'
plot.extra_y_ranges = {"watermark": watermarkrange}
plot.extra_x_ranges = {"watermark": watermarkrange}
plot.image_url([watermarkurl], watermarkx, watermarky,
watermarkw, watermarkh,
global_alpha=watermarkalpha,
w_units='screen',
h_units='screen',
anchor=watermarkanchor,
dilate=True,
x_range_name="watermark",
y_range_name="watermark",
)
source = ColumnDataSource(
df_plot
)
TIPS = OrderedDict([
('x','@x'),
('median','@median'),
('high','@high'),
('low','@low')
])
hover = plot.select(type=HoverTool)
hover.tooltips = TIPS
s = 'SPM: {spm_min} - {spm_max}'.format(
spm_min = spm_min,
spm_max = spm_max,
)
label = Label(x=50, y=450, x_units='screen',y_units='screen',
text=s,
background_fill_alpha=0.7,
background_fill_color='white',
text_color='black',
)
s2 = 'Time: {activeminutesmin} - {activeminutesmax}'.format(
activeminutesmin=datetime.timedelta(seconds=60*activeminutesmin),
activeminutesmax=datetime.timedelta(seconds=60*activeminutesmax)
)
label2 = Label(x=50,y=400, x_units='screen', y_units='screen',
text=s2,
background_fill_alpha=0.7,
background_fill_color='white',
text_color='black',
)
plot.add_layout(label)
plot.add_layout(label2)
if name:
namelabel = Label(x=50, y=480, x_units='screen', y_units='screen',
text=name,
background_fill_alpha=0.7,
background_fill_color='white',
text_color='black',
)
plot.add_layout(namelabel)
if notes:
noteslabel = Label(x=50, y=50, x_units='screen', y_units='screen',
text=notes,
background_fill_alpha=0.7,
background_fill_color='white',
text_color='black',
)
plot.add_layout(noteslabel)
if individual_curves:
for index,row in df.iterrows():
plot.line(xvals,row,color='lightgray',line_width=1)
else:
plot.varea('x', y1='high', y2='low',source=source,fill_color="lightgray",alpha=0.5)
plot.line('x','median',source=source,legend_label='median',color="black",
line_width=3)
medrange = mean_vals.max()-mean_vals.min()
yrange = Range1d(start=mean_vals.min()-0.2*medrange,
end=mean_vals.max()+0.2*medrange,)
plot.y_range = yrange
plot.add_tools(HoverTool(tooltips=TIPS))
if metric == 'boat accelerator curve': if metric == 'boat accelerator curve':
plot.yaxis.axis_label = "Boat acceleration (m/s^2)" ytitle = "Boat acceleration (m/s^2)"
elif metric == 'instroke boat speed': elif metric == 'instroke boat speed':
plot.yaxis.axis_label = "Boat Speed (m/s)" ytitle = "Boat Speed (m/s)"
vavg = mean_vals.median() vavg = mean_vals.median()
elif metric == 'oar angle velocity curve': elif metric == 'oar angle velocity curve':
plot.yaxis.axis_label = "Oar Angular Velocity (degree/s)" ytitle = "Oar Angular Velocity (degree/s)"
elif metric == 'seat curve': elif metric == 'seat curve':
plot.yaxis.axis_label = "Seat Speed (m/s)" ytitle = "Seat Speed (m/s)"
plot.xaxis.axis_label = 'Time (%)'
try: lines_dict = df.to_dict("records")
script, div = components(plot) data_dict = df_plot.to_dict("records")
except ValueError:
script = ""
div = "Something went wrong with the chart"
return (script, div) chart_data = {
'lines': lines_dict,
'data': data_dict,
'ytitle': ytitle,
'title': str(workout) + ' - ' + metric,
'individual_curves': individual_curves,
'spmmin': spm_min,
'spmmax': spm_max,
'timemin' :'{activeminutesmin}'.format(
activeminutesmin=datetime.timedelta(seconds=60*activeminutesmin),
),
'timemax': '{activeminutesmax}'.format(
activeminutesmax=datetime.timedelta(seconds=60*activeminutesmax)
),
'analysis_name': name,
}
script, div = get_chart("/instroke", chart_data, debug=False)
return script, div
def interactive_chart(id=0, promember=0, intervaldata={}): def interactive_chart(id=0, promember=0, intervaldata={}):

View File

@@ -13,7 +13,6 @@
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css"> <link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script> <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script> <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<script> <script>
function submit_form() { function submit_form() {
console.log("form changed"); console.log("form changed");
@@ -29,8 +28,8 @@
dataType: 'json', dataType: 'json',
success: function(data) { success: function(data) {
$("#id_script").replaceWith('<div id="id_script">'+data.script+'</d'+'iv>');
$("#id_chart").replaceWith('<div id="id_chart">'+data.div+'</d'+'iv>'); $("#id_chart").replaceWith('<div id="id_chart">'+data.div+'</d'+'iv>');
$("#id_script").replaceWith('<div id="id_script">'+data.script+'</d'+'iv>');
$("#dd").empty(); $("#dd").empty();
@@ -121,12 +120,10 @@ $( function() {
Bokeh.set_log_level("info"); Bokeh.set_log_level("info");
</script> </script>
<div id="id_script">
{{ the_script |safe }}
</div>
<h1>In Stroke Metrics</h1> <h1>In Stroke Metrics</h1>
<ul class="main-content"> <ul class="main-content">
<canvas hidden id="canvas"></canvas>
<li class="grid_4" > <li class="grid_4" >
{% if user.rower|is_basic %} {% if user.rower|is_basic %}
@@ -167,9 +164,13 @@ $( function() {
</p> </p>
</li> </li>
<li class="grid_4"> <li class="grid_4">
<script src="https://d3js.org/d3.v6.js"></script>
<div id="id_chart" class="flexplot"> <div id="id_chart" class="flexplot">
{{ the_div|safe }} {{ the_div|safe }}
</div> </div>
<div id="id_script">
{{ the_script |safe }}
</div>
</li> </li>
<script src="https://d3js.org/d3.v6.js"></script> <script src="https://d3js.org/d3.v6.js"></script>
<li class="grid_2" id="interactive"> <li class="grid_2" id="interactive">

View File

@@ -685,7 +685,7 @@ class InteractivePlotTests(TestCase):
startdate = workouts[0].date-datetime.timedelta(days=5) startdate = workouts[0].date-datetime.timedelta(days=5)
enddate = workouts2[0].date+datetime.timedelta(days=5) enddate = workouts2[0].date+datetime.timedelta(days=5)
script, div = interactiveplots.interactive_activitychart(workouts,startdate,enddate) script, div = interactiveplots.interactive_activitychart2(workouts,startdate,enddate)
self.assertFalse(len(script)==0) self.assertFalse(len(script)==0)
self.assertFalse(len(div)==0) self.assertFalse(len(div)==0)

Binary file not shown.