Self hosted FLOSS fitness/workout, nutrition and weight tracker
First version of weight chart created with d3js
Roland Geider6c9deb3f4617
diff --git a/weight/templates/weight_overview.html b/weight/templates/weight_overview.htmlindex 2ee5007b9..6dccc9de5 100644--- a/weight/templates/weight_overview.html+++ b/weight/templates/weight_overview.html@@ -13,8 +13,38 @@{% block header %}<script src="{% static 'js/jquery-ui.js' %}"></script><script src="{% static 'js/workout-manager.js' %}"></script>+<script src="{% static 'js/d3.js' %}"></script><script src="{% static 'js/jquery-globalize/lib/globalize.js' %}"></script>+<style>++.axis path, .axis line {+ fill: none;+ stroke: #000;+ shape-rendering: crispEdges;+}++.area {+ fill: lightsteelblue;+}++.line {+ fill: none;+ stroke: steelblue;+ stroke-width: 1.5px;+}++.dot {+ fill: white;+ stroke: steelblue;+ stroke-width: 1.5px;+}++</style>++++<script type="text/javascript">$(document).ready(function () {@@ -26,7 +56,72 @@ $(document).ready(function () {position: 'top'});- });++var data = {{chart_data}};++var margin = {top: 10, right: 10, bottom: 20, left: 40},+ width = 600 - margin.left - margin.right,+ height = 300 - margin.top - margin.bottom;++var x = d3.scale.linear()+ .domain([0, 35])+ .range([0, width]);++var y = d3.scale.linear()+ .domain([75, 85])+ .range([height, 0]);++var xAxis = d3.svg.axis()+ .scale(x)+ .orient("bottom");++var yAxis = d3.svg.axis()+ .scale(y)+ .orient("left");++var line = d3.svg.line()+ .x(function(d) { return x(d.x); })+ .y(function(d) { return y(d.y); });++var area = d3.svg.area()+ .x(line.x())+ .y1(line.y())+ .y0(y(75));++var svg = d3.select("#weight_diagram").append("svg")+ .datum(data)+ .attr("width", width + margin.left + margin.right)+ .attr("height", height + margin.top + margin.bottom)+ .append("g")+ .attr("transform", "translate(" + margin.left + "," + margin.top + ")");++svg.append("path")+ .attr("class", "area")+ .attr("d", area);++svg.append("g")+ .attr("class", "x axis")+ .attr("transform", "translate(0," + height + ")")+ .call(xAxis);++svg.append("g")+ .attr("class", "y axis")+ .call(yAxis);++svg.append("path")+ .attr("class", "line")+ .attr("d", line);++svg.selectAll(".dot")+ .data(data.filter(function(d) { return d.y; }))+ .enter().append("circle")+ .attr("class", "dot")+ .attr("cx", line.x())+ .attr("cy", line.y())+ .attr("r", 3.5);+++});</script>{% endblock %}@@ -38,6 +133,8 @@ $(document).ready(function () {<h2>{% trans "Weight overview" %}</h2>+<div id="weight_diagram"></div>+<p><a href="{% url weight.views.add None %}">{% trans "Add weight entry" %}</a></p>diff --git a/weight/views.py b/weight/views.pyindex 28ba591b9..f75b9f23c 100644--- a/weight/views.py+++ b/weight/views.py@@ -108,13 +108,18 @@ def overview(request):template_data['active_tab'] = 'weight'# Process the data to pass it to the JS libraries to generate an SVG image+ chart_data = "[\n"+ j = 0+ for i in weights:+ chart_data += '{x: %s, y: %s},\n' % (j, i.weight)+ j += 1data_y = ', '.join([str(i.weight) for i in weights])data_x = ', '.join(["new Date(%s, %s, %s)" % (i.creation_date.year,i.creation_date.month,i.creation_date.day) for i in weights])-- template_data['data_x'] = data_x- template_data['data_y'] = data_y+ chart_data = chart_data.rstrip(',')+ chart_data += "]"+ template_data['chart_data'] = chart_data# Make a mapping between index and ID, this is used to edit the entries