Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Filter the weight chart with click and drag on a secondary chart

Roland Geider7e5d00e5f1ce
diff --git a/manager/static/css/workout-manager.css b/manager/static/css/workout-manager.css
index 25da75386..12df72fff 100644
--- a/manager/static/css/workout-manager.css
+++ b/manager/static/css/workout-manager.css
@@ -190,6 +190,12 @@ circle{
cursor: pointer;
}
+.brush .extent {
+ stroke: #fff;
+ fill-opacity: .125;
+ shape-rendering: crispEdges;
+}
+
// Color series from d3.scale.category10()
circle.color-1f77b4:hover
{
diff --git a/manager/static/js/workout-manager.js b/manager/static/js/workout-manager.js
index 6dd2ae53c..ec3248f58 100644
--- a/manager/static/js/workout-manager.js
+++ b/manager/static/js/workout-manager.js
@@ -466,13 +466,18 @@ function weight_chart(data)
var minDate = getDate(data[0].x),
maxDate = getDate(data[data.length-1].x);
- var margin = {top: 10, right: 10, bottom: 20, left: 40},
+ var margin = {top: 10, right: 10, bottom: 150, left: 40},
+ margin2 = {top: 270, right: 10, bottom: 50, left: 40},
width = 600 - margin.left - margin.right,
- height = 300 - margin.top - margin.bottom;
+ height = 400 - margin.top - margin.bottom;
+ height2 = 390 - margin2.top - margin2.bottom;
var x = d3.time.scale()
.domain([minDate, maxDate])
.range([0, width]);
+ var x2 = d3.time.scale()
+ .domain([minDate, maxDate])
+ .range([0, width]);
var min_y_value = d3.min(data, y_value) - 1;
var max_y_value = d3.max(data, y_value) + 1;
@@ -481,27 +486,50 @@ function weight_chart(data)
var y = d3.scale.linear()
.domain([min_y_value, max_y_value])
.range([height, 0]);
+ var y2 = d3.scale.linear()
+ .domain([min_y_value, max_y_value])
+ .range([height2, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.ticks(6)
.orient("bottom");
+ var xAxis2 = d3.svg.axis()
+ .scale(x2)
+ .ticks(6)
+ .orient("bottom");
+
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
+ var brush = d3.svg.brush()
+ .x(x2)
+ .on("brush", brush);
+
var line = d3.svg.line()
.x(function(d) { return x(getDate(d.x)); })
.y(function(d) { return y(d.y); })
.interpolate('cardinal');
+ var line2 = d3.svg.line()
+ .x(function(d) { return x2(getDate(d.x)); })
+ .y(function(d) { return y2(d.y); })
+ .interpolate('cardinal');
+
var area = d3.svg.area()
.x(line.x())
.y1(line.y())
.y0(y(min_y_value))
.interpolate('cardinal');
+ var area2 = d3.svg.area()
+ .x(line2.x())
+ .y1(line2.y())
+ .y0(y2(min_y_value))
+ .interpolate('cardinal');
+
// Reset the content of weight_diagram, otherwise if there is a filter
// a new SVG will be appended to it
$("#weight_diagram").html("");
@@ -513,24 +541,37 @@ function weight_chart(data)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
- svg.append("path")
+ svg.append("defs").append("clipPath")
+ .attr("id", "clip")
+ .append("rect")
+ .attr("width", width)
+ .attr("height", height);
+
+ var focus = svg.append("g")
+ .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
+
+ var context = svg.append("g")
+ .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
+
+ focus.append("path")
.attr("class", "area")
+ .attr("clip-path", "url(#clip)")
.attr("d", area);
- svg.append("g")
+ focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
- svg.append("g")
+ focus.append("g")
.attr("class", "y axis")
.call(yAxis);
- svg.append("path")
+ focus.append("path")
.attr("class", "line")
.attr("d", line);
- svg.selectAll(".dot")
+ focus.selectAll(".dot")
.data(data.filter(function(d) { return d.y; }))
.enter().append("circle")
.attr("class", "dot modal-dialog")
@@ -540,12 +581,44 @@ function weight_chart(data)
.attr("cy", line.y())
.attr("r", 5);
+ context.append("path")
+ .attr("class", "area")
+ .attr("d", area2);
+
+ context.append("path")
+ .attr("class", "line")
+ .attr("d", line2);
+
+ context.append("g")
+ .attr("class", "x axis")
+ .attr("transform", "translate(0," + height2 + ")")
+ .call(xAxis2);
+
+ context.append("g")
+ .attr("class", "x brush")
+ .call(brush)
+ .selectAll("rect")
+ .attr("y", -6)
+ .attr("height", height2 + 7);
+
+ function brush() {
+ x.domain(brush.empty() ? x2.domain() : brush.extent());
+ focus.select("path").attr("d", area);
+ focus.select(".line").attr("d", line);
+
+ focus.selectAll(".dot")
+ .attr("cx", line.x())
+ .attr("cy", line.y());
+
+ focus.select(".x.axis").call(xAxis);
+ }
// Make the circles clickable: open their edit dialog
form_modal_dialog();
}
+
function weight_log_chart(data, div_id, reps_i18n)
{
var margin = {top: 20, right: 80, bottom: 30, left: 50},
diff --git a/weight/templates/weight_overview.html b/weight/templates/weight_overview.html
index f3edae9ca..0dd13391d 100644
--- a/weight/templates/weight_overview.html
+++ b/weight/templates/weight_overview.html
@@ -7,7 +7,7 @@
-->
{% block title %}{% trans "Weight overview" %}{% endblock %}
-<!--
+<!--
Header data
-->
{% block header %}
@@ -39,25 +39,25 @@ $(document).ready(function () {
filter_data = {'date_min': date_min,
'date_max': date_max}
}
-
+
$.getJSON("/weight/api/get_weight_data/", filter_data, function(json) {
weight_chart(json);
});
-
+
// Add datepickers to the filter elements
-
+
$("#weight_filter_from").datepicker({ dateFormat: "yy-mm-dd",
{% if min_date %}minDate: {{ min_date }}{% endif %}
});
$("#weight_filter_to").datepicker({ dateFormat: "yy-mm-dd",
{% if max_date %}maxDate: {{max_date}}{% endif %}
});
-
+
// Process the filter
$("#weight_diagram_refresh").click(function(e) {
-
+
e.preventDefault();
-
+
var date_min = $("#weight_filter_from").val();
var date_max = $("#weight_filter_to").val();
@@ -66,7 +66,7 @@ $(document).ready(function () {
'date_max': date_max},
function(json) {
weight_chart(json);
- });
+ });
});
});
</script>
@@ -107,29 +107,11 @@ $(document).ready(function () {
-->
{% block sidebar %}
<h3>{% trans "Filter options" %}</h3>
-<form>
-{% trans "from" %}
-<input type="text"
- id="weight_filter_from"
- name="weight_filter_from"
- maxlength="9"
- style="width: 6em;"
- class="ajax-form-element">
-
-{% trans "till" %}
-<input type="text"
- id="weight_filter_to"
- name="weight_filter_to"
- maxlength="9"
- style="width: 6em;"
- class="ajax-form-element">
-
-<input class="ajax-form-submit"
- id="weight_diagram_refresh"
- type="submit"
- value="{% trans 'Filter' %}">
-
-</form>
+<p>{% blocktrans %}
+Use the second chart to pan or zoom by clicking and dragging the selection. Clicking
+on the background resets it.
+{% endblocktrans %}</p>
+
<h3>{% trans "Options" %}</h3>
<p>