Self hosted FLOSS fitness/workout, nutrition and weight tracker
Move D3 code for BMI chart to external JS file
Roland Geider049fcb63fbb5
diff --git a/wger/manager/static/js/workout-manager.js b/wger/manager/static/js/workout-manager.jsindex 9611ed37f..dde5ac694 100644--- a/wger/manager/static/js/workout-manager.js+++ b/wger/manager/static/js/workout-manager.js@@ -796,6 +796,117 @@ function weight_log_chart(data, div_id, reps_i18n, width_factor)form_modal_dialog();}++/*+ * Draw the BMI chart+ */+function render_bmi(width_factor)+{+ // Delete the other diagrams+ d3.selectAll('svg').remove();++ // Calculate the size+ var width_factor = typeof width_factor !== 'undefined' ? width_factor: 600;+ var height_factor = width_factor / 600 * 300;++ 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 x = d3.scale.linear()+ .range([0, width]);++ var y = d3.scale.linear()+ .range([height, 0]);++ var z = d3.scale.ordinal().range(['#000080', '#0000ff', '#00ffff', '#00ff00', '#ffff00', '#ff7f2a', '#ff0000', '#800000']);++ var xAxis = d3.svg.axis()+ .scale(x)+ .orient("bottom");++ var yAxis = d3.svg.axis()+ .scale(y)+ .orient("left");++ var stack = d3.layout.stack()+ .offset("zero")+ .values(function(d) { return d.values; })+ .x(function(d) { return d.height; })+ .y(function(d) { return d.weight; });++ var nest = d3.nest()+ .key(function(d) { return d.key; });++ var area = d3.svg.area()+ .interpolate("linear")+ .x(function(d) { return x(d.height); })+ .y0(function(d) { return y(d.y0); })+ .y1(function(d) { return y(d.y); });+++ var svg = d3.select("#bmi-chart").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 + ")");++ // Clip path, drawings outside are removed+ svg.append("defs").append("clipPath")+ .attr("id", "clip")+ .append("rect")+ .attr("width", width)+ .attr("height", height);+++ d3.json("/nutrition/calculator/bmi/chart-data", function(data) {++ var layers = stack(nest.entries(data));++ // Manually set the domains+ x.domain([150, 200]); // height, in cm+ y.domain([30, 160]); // weight, in kg++ svg.selectAll(".layer")+ .data(layers)+ .enter().append("path")+ .attr("class", "layer")+ .attr("id", function(d, i) { return "key-" + d.key; })+ .attr("clip-path", "url(#clip)")+ .attr("d", function(d) { return area(d.values); })+ .style("fill", function(d, i) { return z(i); })+ .style("opacity", 0.6);++ svg.append("g")+ .attr("class", "x axis")+ .attr("transform", "translate(0," + height + ")")+ .call(xAxis);++ svg.append("g")+ //.attr("transform", "translate(" + width + ",0)")+ .attr("class", "y axis")+ .call(yAxis);++ var url = $("#bmi-form").attr( 'action' );++ $.post(url,+ $("#bmi-form").serialize(),+ function(data) {+ $('#bmi-result-container').show();+ $('#bmi-result-value').html(data.bmi);+ svg.append("circle")+ .attr("cx", x(data.height))+ .attr("cy", y(data.weight))+ .attr("fill", "black")+ .attr("r", 5);+ });+ });++}++++function toggle_weight_log_table(){$(".weight-chart-table-toggle").click(function(e) {diff --git a/wger/nutrition/templates/bmi/form.html b/wger/nutrition/templates/bmi/form.htmlindex 2c0525fcf..0c351c50a 100644--- a/wger/nutrition/templates/bmi/form.html+++ b/wger/nutrition/templates/bmi/form.html@@ -27,116 +27,14 @@ div.bmi-legend {<script>$(document).ready(function () {- /*- * Draw the static part of the chart- */- var margin = {top: 20, right: 20, bottom: 30, left: 50},- width = 600 - margin.left - margin.right,- height = 300 - margin.top - margin.bottom;-- var x = d3.scale.linear()- .range([0, width]);-- var y = d3.scale.linear()- .range([height, 0]);-- var z = d3.scale.ordinal().range(['#000080', '#0000ff', '#00ffff', '#00ff00', '#ffff00', '#ff7f2a', '#ff0000', '#800000']);-- var xAxis = d3.svg.axis()- .scale(x)- .orient("bottom");-- var yAxis = d3.svg.axis()- .scale(y)- .orient("left");-- var stack = d3.layout.stack()- .offset("zero")- .values(function(d) { return d.values; })- .x(function(d) { return d.height; })- .y(function(d) { return d.weight; });-- var nest = d3.nest()- .key(function(d) { return d.key; });-- var area = d3.svg.area()- .interpolate("linear")- .x(function(d) { return x(d.height); })- .y0(function(d) { return y(d.y0); })- .y1(function(d) { return y(d.y); });--- var svg = d3.select("#bmi-chart").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 + ")");-- // Clip path, drawings outside are removed- svg.append("defs").append("clipPath")- .attr("id", "clip")- .append("rect")- .attr("width", width)- .attr("height", height);--- d3.json("/nutrition/calculator/bmi/chart-data", function(data) {-- var layers = stack(nest.entries(data));-- // Manually set the domains- x.domain([150, 200]); // height, in cm- y.domain([30, 160]); // weight, in kg-- svg.selectAll(".layer")- .data(layers)- .enter().append("path")- .attr("class", "layer")- .attr("id", function(d, i) { return "key-" + d.key; })- .attr("clip-path", "url(#clip)")- .attr("d", function(d) { return area(d.values); })- .style("fill", function(d, i) { return z(i); })- .style("opacity", 0.6);-- svg.append("g")- .attr("class", "x axis")- .attr("transform", "translate(0," + height + ")")- .call(xAxis);-- svg.append("g")- //.attr("transform", "translate(" + width + ",0)")- .attr("class", "y axis")- .call(yAxis);-- render_current_bmi();- });-- /*- * Draw the calculated BMI- */- function render_current_bmi()- {- var url = $("#bmi-form").attr( 'action' );-- $.post(url,- $("#bmi-form").serialize(),- function(data) {- $('#bmi-result-container').show();- $('#bmi-result-value').html(data.bmi);- svg.append("circle")- .attr("cx", x(data.height))- .attr("cy", y(data.weight))- .attr("fill", "black")- .attr("r", 5);- });- }+ render_bmi({% if flavour = 'mobile'%}$('#content').width() * 0.95{% endif %});/** Process the form*/$("#bmi-form").click(function(e){e.preventDefault();- render_current_bmi();+ render_bmi({% if flavour = 'mobile'%}$('#content').width() * 0.95{% endif %});});});@@ -157,6 +55,7 @@ $(document).ready(function () {Main Content-->{% block content %}+<form action="{% url 'bmi-calculate' %}"method="post"class="ym-form"@@ -194,16 +93,6 @@ $(document).ready(function () {--<!---<img width="479"- height="300"- title="{% trans 'BMI' %}"- alt="{% trans 'BMI' %}"- src="{% static 'images/bmi.svg' %}"- style="margin-top:3em;"></img>--->-<div id="bmi-chart"></div>