diff options
| -rw-r--r-- | python/risk_insight/static/indices.js | 49 | ||||
| -rw-r--r-- | python/risk_insight/static/tranches.js | 81 | ||||
| -rw-r--r-- | python/risk_insight/static/utils.js | 22 | ||||
| -rw-r--r-- | python/risk_insight/templates/indices.html | 2 | ||||
| -rw-r--r-- | python/risk_insight/templates/tranches.html | 2 |
5 files changed, 84 insertions, 72 deletions
diff --git a/python/risk_insight/static/indices.js b/python/risk_insight/static/indices.js index d3cc002a..6ada9916 100644 --- a/python/risk_insight/static/indices.js +++ b/python/risk_insight/static/indices.js @@ -1,32 +1,29 @@ function update_graph(g) { - return function(e) { - var payload = {i: document.getElementById('index').value, - s: document.getElementById('series').value, - t: document.getElementById('tenor').value, - w: document.getElementById('what').value}; - $.getJSON("_data_indices", - payload, - function(data){ - var newdata = data["data"].map(function(e){ - f = e.slice(1); - f.unshift(new Date(e[0])); - return f}); - g.updateOptions({'file': newdata, 'labels': data['labels']}); - }); - } + var payload = {i: $('index').value, + s: $('series').value, + t: $('tenor').value, + w: $('what').value}; + query("_data_indices", + payload, + function(data){ + data = JSON.parse(data); + var newdata = data["data"].map(function(e){ + f = e.slice(1); + f.unshift(new Date(e[0])); + return f}); + g.updateOptions({'file': newdata, 'labels': data['labels']}); + }); }; -document.addEventListener("DOMContentLoaded", function(event) { - var g = new Dygraph(document.getElementById("graph"), +document.addEventListener("DOMContentLoaded", function() { + var g = new Dygraph($("graph"), [], // path to CSV file - { labels: [], - title: "Index quotes", - showRoller: true, - legend: "always"}); + { labels: [], + title: "Index quotes", + showRoller: true, + legend: "always"}); ["index", "series", "tenor", "what"].forEach(function(id) { - document.getElementById(id).addEventListener('change', - update_graph(g))}); - var event = new Event('change'); - document.getElementById('index').dispatchEvent(event); - + $(id).addEventListener('change', function(){ + update_graph(g)}); + update_graph(g); }); diff --git a/python/risk_insight/static/tranches.js b/python/risk_insight/static/tranches.js index f7809884..10f40eaa 100644 --- a/python/risk_insight/static/tranches.js +++ b/python/risk_insight/static/tranches.js @@ -1,48 +1,41 @@ function update_graph(g) { - return function(e) { - var payload = {i: document.getElementById('index').value, - s: document.getElementById('series').value, - t: document.getElementById('tenor').value, - g: document.getElementById('greek').value}; - if( g.maindiv_.id == "graph1"){ - payload['g'] = "skew"; - } - $.getJSON("_data_tranches", - payload, - function(data){ - var newdata = data["data"].map(function(e){ - f = e.slice(1); - f.unshift(new Date(e[0])); - return f}); - g.updateOptions({'file': newdata, 'labels': data['labels']}); - }); - } + var payload = {i: $('index').value, + s: $('series').value, + t: $('tenor').value, + g: $('greek').value}; + if( g.maindiv_.id == "graph1"){ + payload['g'] = "skew"; + } + query("_data_tranches", payload, + function(data){ + data = JSON.parse(data); + var newdata = data["data"].map(function(e){ + f = e.slice(1); + f.unshift(new Date(e[0])); + return f}); + g.updateOptions({'file': newdata, 'labels': data['labels']}); + }); }; -document.addEventListener("DOMContentLoaded", function(event) { - var g1 = new Dygraph(document.getElementById("graph1"), - [], // path to CSV file - { labels: [], - title: "Correlation numbers", - showRoller: true, - legend: "always"}); - - var g2 = new Dygraph(document.getElementById("graph2"), - [], // path to CSV file - { labels: [], - title: "Risk numbers", - showRoller: true, - legend: 'always'}); - - document.getElementById('index'). - addEventListener('change', update_graph(g1)); - document.getElementById('series'). - addEventListener('change', update_graph(g1)); - document.getElementById('tenor'). - addEventListener('change', update_graph(g1)); - document.getElementById('greek'). - addEventListener('change', update_graph(g2)); - var event = new Event('change'); - document.getElementById('index').dispatchEvent(event); - document.getElementById('greek').dispatchEvent(event); +document.addEventListener("DOMContentLoaded", function() { + var g1 = new Dygraph($("graph1"), [], // path to CSV file + { labels: [], + title: "Correlation numbers", + showRoller: true, + legend: "always"}); + var g2 = new Dygraph($("graph2"), [], // path to CSV file + { labels: [], + title: "Risk numbers", + showRoller: true, + legend: 'always'}); + ["index", "series", "tenor", "greek"].forEach(function(id) { + $(id).addEventListener('change', function(){ + if(this.id != "greek"){ + update_graph(g1); + } + update_graph(g2); + }); + }) + update_graph(g1); + update_graph(g2); }); diff --git a/python/risk_insight/static/utils.js b/python/risk_insight/static/utils.js new file mode 100644 index 00000000..0b886e8f --- /dev/null +++ b/python/risk_insight/static/utils.js @@ -0,0 +1,22 @@ +function $(s) { + return( document.getElementById(s) ) +} + +function encode_dict(d){ + var r = [] + for(var k in d){ + r.push(encodeURIComponent(k) + '=' + encodeURIComponent(d[k])); + } + return r.join('&'); +} + +function query(url, params, callback){ + var xhr = new XMLHttpRequest(); + xhr.open('GET', url + '?' + encode_dict(params)); + xhr.onreadystatechange = function() { + if( xhr.readyState === 4){ + callback(xhr.responseText); + } + } + xhr.send(); +} diff --git a/python/risk_insight/templates/indices.html b/python/risk_insight/templates/indices.html index 11950694..af69636c 100644 --- a/python/risk_insight/templates/indices.html +++ b/python/risk_insight/templates/indices.html @@ -35,7 +35,7 @@ </select> </div> <script type="text/javascript" src="{{ url_for('static', filename='dygraph-combined.js') }}"></script> - <script type="text/javascript" src="{{ url_for('static', filename='jquery-2.1.1.min.js') }}"></script> + <script type="text/javascript" src="{{ url_for('static', filename='utils.js') }}"></script> <script type="text/javascript" src="{{ url_for('static', filename='indices.js') }}"></script> </body> </html> diff --git a/python/risk_insight/templates/tranches.html b/python/risk_insight/templates/tranches.html index 9cec5653..b6c88d3c 100644 --- a/python/risk_insight/templates/tranches.html +++ b/python/risk_insight/templates/tranches.html @@ -37,8 +37,8 @@ <option value="el">Expected Loss</option> </select> </div> + <script type="text/javascript" src="{{ url_for('static', filename='utils.js') }}"></script> <script type="text/javascript" src="{{ url_for('static', filename='tranches.js') }}"></script> <script type="text/javascript" src="{{ url_for('static', filename='dygraph-combined.js') }}"></script> - <script type="text/javascript" src="{{ url_for('static', filename='jquery-2.1.1.min.js') }}"></script> </body> </html> |
