From 4816cef4f64959d2d95fd094fd1b2389bd09879c Mon Sep 17 00:00:00 2001 From: Goutte Date: Wed, 24 May 2017 09:32:52 +0200 Subject: [PATCH] Refactor some more. --- web/run.py | 2 -- web/static/js/swapp.js | 32 ++++++++++++++++++++++++++++++++ web/static/js/swapp.ls | 19 +++++++++++++++++++ web/view/home.html.jinja2 | 51 ++++++--------------------------------------------- 4 files changed, 57 insertions(+), 47 deletions(-) diff --git a/web/run.py b/web/run.py index 4a92c2b..c2f47e9 100755 --- a/web/run.py +++ b/web/run.py @@ -271,8 +271,6 @@ def get_orbiter_csv(source): except: abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at) - - source_config = get_source_config(orbiter) # todo: iterate on models when there are many try: model_slug = source_config['models'][0]['slug'] diff --git a/web/static/js/swapp.js b/web/static/js/swapp.js index 5d6c06c..166382d 100644 --- a/web/static/js/swapp.js +++ b/web/static/js/swapp.js @@ -75,6 +75,38 @@ }); return promise; }; + SpaceWeather.prototype.createTimeSeries = function(data){ + var timeSeries; + timeSeries = []; + this.configuration['parameters'].forEach(function(parameter){ + var container, id, title; + container = this.configuration['time_series_container']; + id = parameter['id']; + title = parameter['title']; + if (!(id in data)) { + console.error("No data for id '" + id + "'.", data); + } + return timeSeries.push(new TimeSeries(id, title, data[id], container)); + }); + return timeSeries.forEach(function(ts){ + ts.options['onMouseOver'] = function(){ + return timeSeries.forEach(function(ts2){ + return ts2.showCursor(); + }); + }; + ts.options['onMouseOut'] = function(){ + return timeSeries.forEach(function(ts2){ + return ts2.hideCursor(); + }); + }; + return ts.options['onMouseMove'] = function(t){ + timeSeries.forEach(function(ts2){ + return ts2.moveCursor(t); + }); + return typeof orbits != 'undefined' && orbits !== null ? orbits.moveToDate(t) : void 8; + }; + }); + }; return SpaceWeather; }()); Source = (function(){ diff --git a/web/static/js/swapp.ls b/web/static/js/swapp.ls index 1bbbea7..86402fc 100644 --- a/web/static/js/swapp.ls +++ b/web/static/js/swapp.ls @@ -57,6 +57,25 @@ export class SpaceWeather ) promise + createTimeSeries: (data) -> + timeSeries = [] + @configuration['parameters'].forEach((parameter) -> + container = @configuration['time_series_container'] + id = parameter['id'] ; title = parameter['title'] + if id not of data then console.error("No data for id '#{id}'.", data) + timeSeries.push(new TimeSeries(id, title, data[id], container)) + ) + timeSeries.forEach((ts) -> + ts.options['onMouseOver'] = -> + timeSeries.forEach((ts2) -> ts2.showCursor()) + ts.options['onMouseOut'] = -> + timeSeries.forEach((ts2) -> ts2.hideCursor()) + ts.options['onMouseMove'] = (t) -> + timeSeries.forEach((ts2) -> ts2.moveCursor(t)) + orbits?.moveToDate(t) + ) + + class Source diff --git a/web/view/home.html.jinja2 b/web/view/home.html.jinja2 index e826519..b4f9ca1 100755 --- a/web/view/home.html.jinja2 +++ b/web/view/home.html.jinja2 @@ -213,53 +213,14 @@ jQuery().ready(function($){ var sw = new SpaceWeather(configuration); sw.loadData('jupiter', '2016-01-01T00:00:00', '2023-01-01T00:00:00').then( - function (data) { console.log('OK', data); }, - function (data) { console.log('Failed', data); } + function (data) { + console.log('OK', data); + sw.createTimeSeries(data); + orbits = new Orbits(configuration.sources, data['hci'], configuration.orbits_container); + }, + function (data) { console.error('Failed to load SW data.', data); } ); - d3.csv('{{ url_for('get_orbiter_csv', source='jupiter', started_at='2016-03-19T00:00:00', stopped_at='2022-01-01T00:00:00') }}', function(csv){ - var timeFormat = d3.timeParse('%Y-%m-%dT%H:%M:%S%Z'); - var data = {'hci': []}; - configuration['parameters'].forEach(function(parameter){ - data[parameter['id']] = []; - }); - csv.forEach(function (d) { - var dtime = timeFormat(d['time']); - configuration['parameters'].forEach(function(parameter){ - var id = parameter['id']; - data[id].push({x: dtime, y: parseFloat(d[id])}); - }); - if (d['xhci'] && d['yhci']) { - data['hci'].push({t: dtime, x: parseFloat(d['xhci']), y: parseFloat(d['yhci'])}); - } - }); - - var container = '#time_series'; - - configuration['parameters'].forEach(function(parameter){ - var id = parameter['id']; - var title = parameter['title']; - timeSeries.push(new TimeSeries(id, title, data[id], container)); - }); - // move outside (after promises) - timeSeries.forEach(function(ts){ - ts.options['onMouseOver'] = function() { - timeSeries.forEach(function(ts2){ ts2.showCursor(); }); - }; - ts.options['onMouseOut'] = function() { - timeSeries.forEach(function(ts2){ ts2.hideCursor(); }); - }; - ts.options['onMouseMove'] = function(t) { - timeSeries.forEach(function(ts2){ ts2.moveCursor(t); }); - if (orbits != null) orbits.moveToDate(t); - }; - }); - - - orbits = new Orbits(configuration.sources, data['hci'], configuration.orbits_container); - - }); - var toggleTimeSeries = function(slug) { $('#time_series .'+slug).toggleClass('hidden'); }; -- libgit2 0.21.2