Self hosted FLOSS fitness/workout, nutrition and weight tracker
First step in using metricsgraphics for exercise charts
Roland Geider14cf2ee84af8
diff --git a/wger/exercises/static/js/exercises.js b/wger/exercises/static/js/exercises.jsindex a2eb5970d..37f9f4c18 100644--- a/wger/exercises/static/js/exercises.js+++ b/wger/exercises/static/js/exercises.js@@ -56,141 +56,27 @@ function highlight_muscle(element) {**/-function weight_log_chart(data, div_id, reps_i18n, width_factor) {-- // Calculate the size- var width_factor = typeof width_factor !== 'undefined' ? width_factor: 600;- var height_factor = width_factor / 600 * 200;-- var margin = {top: 20, right: 80, bottom: 30, left: 50},- width = width_factor - margin.left - margin.right,- height = height_factor - margin.top - margin.bottom;-- var parseDate = d3.time.format("%Y-%m-%d").parse;-- var x = d3.time.scale()- .range([0, width]);-- var y = d3.scale.linear()- .range([height, 0]);-- var color = d3.scale.category10();-- var xAxis = d3.svg.axis()- .scale(x)- .ticks(6)- .orient("bottom");-- var yAxis = d3.svg.axis()- .scale(y)- .ticks(6)- .orient("left");-- var line = d3.svg.line()- .interpolate("cardinal")- .tension(0.6)- .x(function (d) { return x(d.date); })- .y(function (d) { return y(d.weight); });-- var svg = d3.select("#svg-" + div_id).append("svg")- .attr("width", width + margin.left + margin.right)- .attr("height", height + margin.top + margin.bottom)- .append("g")- .attr("transform", "translate(" + margin.left + "," + margin.top + ")");--- color.domain(d3.keys(data[0]).filter(function (key) {- return ($.inArray(key, ['date', 'id']) == -1);- })- );-- data.forEach(function(d) {- d.date = parseDate(d.date);- });--- var reps = color.domain().map(function (name) {-- var temp_values = data.filter(function(d) {- return (d[name] != 'n.a');- });-- var filtered_values = temp_values.map(function (d) {- return {date: d.date,- weight: +d[name],- log_id: d.id};- });-- return {- name: name,- values: filtered_values- };- });-- //console.log(reps);-- x.domain(d3.extent(data, function (d) { return d.date; }));-- // Add 1 kg of "breathing room" on the min value, so the diagrams don't look- // too flat- y.domain([- d3.min(reps, function(c) { return d3.min(c.values, function(v) { return v.weight - 1; }); }),- d3.max(reps, function(c) { return d3.max(c.values, function(v) { return v.weight; }); })- ]);-- svg.append("g")- .attr("class", "x axis")- .attr("transform", "translate(0," + height + ")")- .call(xAxis);-- svg.append("g")- .attr("class", "y axis")- .call(yAxis)- .append("text")- .attr("transform", "rotate(-90)")- .attr("y", 6)- .attr("dy", ".71em");- //.style("text-anchor", "end")- //.text("Weight");-- var log_series = svg.selectAll(".log_series")- .data(reps)- .enter().append("g")- .attr("class", "log_series");-- log_series.append("path")- .attr("class", "line")- .attr("d", function(d) { return line(d.values); })- .style("stroke", function(d) { return color(d.name); });-- reps.forEach(function(d){- var color_name = d.name- var temp_name = hex_random();- var color_class = 'color-' + color(color_name).replace('#', '');-- svg.selectAll(".dot" + temp_name)- .data(d.values)- .enter().append("circle")- .attr("class", "dot wger-modal-dialog " + color_class)- .attr("cx", line.x())- .attr("cy", line.y())- .attr("id", function(d) { return d.log_id; })- .attr("href", function(d) { return '/' + get_current_language() + '/workout/log/' + d.log_id.match(/\d+/) + '/edit'; })- .attr("r", 5)- .style("stroke", function(d) {- return color(color_name);- });- });--- log_series.append("text")- .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })- .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.weight) + ")"; })- .attr("x", 6)- .attr("dy", ".35em")- .text(function(d) { return d.name + " " + reps_i18n; });-- // Make the circles clickable: open their edit dialog- form_modal_dialog();+function draw_weight_log_chart(data, div_id, rep_i18n) {++ var legend = [];+ var chart_data = [];+ for (var i = 0; i < data.length; i++) {+ chart_data[i] = MG.convert.date(data[i], 'date');++ // Read the possible repetitions for the chart legend+ legend[i] = data[i][0].reps+ }++ MG.data_graphic({+ data: chart_data,+ y_accessor: 'weight',+ // min_y_from_data: true,+ aggregate_rollover: true,+ full_width: true,+ height: 200,+ right: 40,+ legend: legend,+ target: '#svg-' + div_id+ });}diff --git a/wger/exercises/templates/exercise/view.html b/wger/exercises/templates/exercise/view.htmlindex ecbdb4751..7be5009bb 100644--- a/wger/exercises/templates/exercise/view.html+++ b/wger/exercises/templates/exercise/view.html@@ -39,7 +39,17 @@{# #}{# Header #}{# #}-{% block header %}{% endblock %}+{% block header %}+<link rel="stylesheet" type="text/css" href="{% static 'bower_components/metrics-graphics/dist/metricsgraphics.css' %}">+<link rel="stylesheet" type="text/css" href="{% static 'css/metricsgraphics-custom.css' %}">+<script src="{% static 'bower_components/metrics-graphics/dist/metricsgraphics.min.js' %}"></script>+<script>+$(document).ready(function() {+ toggle_weight_log_table();+ draw_weight_log_chart({{json|safe}}, "{{svg_uuid}}", "{{reps}}");+});+</script>+{% endblock %}{# #}@@ -330,10 +340,7 @@<script>-$(document).ready(function() {- toggle_weight_log_table();- weight_log_chart({{json|safe}}, "{{svg_uuid}}", "{{reps}}", $('#content').width());-});+</script>{% endif %}diff --git a/wger/manager/templates/workout/log.html b/wger/manager/templates/workout/log.htmlindex 6cd49396d..9f863ec9a 100644--- a/wger/manager/templates/workout/log.html+++ b/wger/manager/templates/workout/log.html@@ -33,6 +33,9 @@{# Header #}{# #}{% block header %}+<link rel="stylesheet" type="text/css" href="{% static 'bower_components/metrics-graphics/dist/metricsgraphics.css' %}">+<link rel="stylesheet" type="text/css" href="{% static 'css/metricsgraphics-custom.css' %}">+<script src="{% static 'bower_components/metrics-graphics/dist/metricsgraphics.min.js' %}"></script><script>$(document).ready(function() {toggle_weight_log_table();@@ -84,10 +87,9 @@ $(document).ready(function() {<script>$(document).ready(function() {- weight_log_chart({{exercise_list.chart_data|safe}},- "{{exercise_list.div_uuid}}",- "{{reps}}",- $('#content').width());+ draw_weight_log_chart({{exercise_list.chart_data|safe}},+ "{{exercise_list.div_uuid}}",+ "{{reps}}");});</script>{% endwith %}diff --git a/wger/utils/helpers.py b/wger/utils/helpers.pyindex be08fb577..4b1b5f8fe 100644--- a/wger/utils/helpers.py+++ b/wger/utils/helpers.py@@ -63,6 +63,8 @@ class DecimalJsonEncoder(json.JSONEncoder):def default(self, obj):if isinstance(obj, decimal.Decimal):return str(obj)+ if isinstance(obj, datetime.date):+ return str(obj)return json.JSONEncoder.default(self, obj)diff --git a/wger/weight/helpers.py b/wger/weight/helpers.pyindex b691eba99..4fe469ecb 100644--- a/wger/weight/helpers.py+++ b/wger/weight/helpers.py@@ -182,30 +182,19 @@ def process_log_entries(logs):if entry.weight > max_weight[entry.date][entry.reps]:max_weight[entry.date][entry.reps] = entry.weight- # Group by repetitions- reps_list = {}+ tmp = {}for entry in logs:- temp = {'date': '%s' % entry.date,- 'id': 'manager:workout:log-%s' % entry.id}-- # Only unique date, rep and weight combinations- if reps_list.get((entry.date, entry.reps, entry.weight)):- continue- else:- reps_list[(entry.date, entry.reps, entry.weight)] = True+ if not tmp.get(entry.reps):+ tmp[entry.reps] = []# Only add if weight is the maximum for the dayif entry.weight != max_weight[entry.date][entry.reps]:continue- for rep in reps:- if entry.reps == rep:- temp[rep] = entry.weight- else:- # Mark entries without data, this is later filtered out by D3.- # We use the string 'n.a' instead of 0 to differentiate actual exercises- # where no weight was used.- temp[rep] = 'n.a'- chart_data.append(temp)+ tmp[entry.reps].append({'date': entry.date,+ 'weight': entry.weight,+ 'reps': entry.reps})+ for rep in tmp:+ chart_data.append(tmp[rep])return entry_log, json.dumps(chart_data, cls=DecimalJsonEncoder)diff --git a/wger/weight/templates/overview.html b/wger/weight/templates/overview.htmlindex 52352c704..2bd4faadb 100644--- a/wger/weight/templates/overview.html+++ b/wger/weight/templates/overview.html@@ -7,7 +7,7 @@{% block title %}{% trans "Weight overview" %}{% endblock %}{# #}-{# HEader #}+{# Header #}{# #}{% block header %}<link rel="stylesheet" type="text/css" href="{% static 'bower_components/metrics-graphics/dist/metricsgraphics.css' %}">