Commits

Matt Bone committed d751f16

growing % view mostly working

Comments (0)

Files changed (3)

growthrates/inputs/views.py

 from django.core.urlresolvers import reverse
 from django.template.defaultfilters import floatformat
 
+import json
+import math
+
 class GrowingForm(forms.Form):
     rate = forms.FloatField(required=True)
     period = forms.ChoiceField((('year', 'year'), ('month', 'month')))
 
 
 def growing_at(request, rate, period):
+
+    rate_f = float(rate)/100.0
+    doubling_rate = math.log(2)/math.log1p(rate_f)
+
+    initial_value = 100
+    num_points = 25
+
+    data_array = []
+    curr = initial_value
+    data_array.append([0, initial_value])
+    for i in range(1, num_points):
+        curr = curr + rate_f*curr
+        data_array.append([i, curr])
+
+
     return render_to_response("growing.html", {'rate':rate,
-                                               'period': period})
+                                               'period': period,
+                                               'data_array': data_array,
+                                               'json_data_array': json.dumps(data_array),
+                                               'doubling_rate': floatformat(doubling_rate, "-3")})
 
 
 def doubling_at(request, rate, period):

growthrates/templates/base.html

 <head>
     <meta charset="utf-8">
     <title>Growth Rates</title>
-    <meta name="description" content="">
-    <meta name="author" content="">
 
-    <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
     <!--[if lt IE 9]>
     <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
     <![endif]-->
 
-    <!-- Le styles -->
-    <link href="/static/bootstrap.css" rel="stylesheet"> {# TODO fix this to use STATIC_URL #}
 
+    {# TODO fix this to use STATIC_URL #}
+    <link href="/static/bootstrap.css" rel="stylesheet">
+    <script language="javascript" type="text/javascript" src="/static/jquery.js"></script>
+    <script language="javascript" type="text/javascript" src="/static/jquery.flot.js"></script>
 
     <style type="text/css">
-            /* Override some defaults */
+        /* Override some defaults */
         html, body {
             background-color: #eee;
         }
     </style>
 
 
+
     {#
     <!-- Le fav and touch icons -->
     <link rel="shortcut icon" href="images/favicon.ico">

growthrates/templates/growing.html

 {% extends "base.html" %}
 
 {% block content %}
-<h1>Growing at {{ rate }}% per {{ period }}</h1>
+    <h1>Growing at {{ rate }}% per {{ period }}</h1>
+    
+    <p>Is the same as <a href="{{ url_for("doubling_at", args=(doubling_rate, period+"s")) }}">doubling every {{ doubling_rate }} {{ period }}s.</a></p>
 
+    <h2>If you had $100 growing at {{ rate }}% per {{ period }}...</h2>
+
+    <div id="growth-graph" style="width:600px;height:300px;"></div>
+
+    <h2 style="padding-top: 25px">Or in table form...</h2>
+    <table class="span5 zebra-striped">
+        <thead>
+            <th>{{ period }}</th>
+            <th>Dollars</th>
+        </thead>
+    {% for n, m in data_array %}
+    <tr>
+        <td>{{ n }}</td>
+        <td>{{ m }}</td>
+    </tr>
+    {% endfor %}
+    </table>
+
+    <script type="text/javascript">
+        $(function () {
+            $.plot($("#growth-graph"), [{{ json_data_array }}]);
+        });
+    </script>
 {% endblock %}