Blame view

web/static/js/swapp.js 14 KB
438929a4   Goutte   Rewrite the orbit...
1
2
// Generated by LiveScript 1.5.0
(function(){
ae0aa7d2   Goutte   Add an x axis lab...
3
  var GOLDEN_RATIO, SpaceWeather, Source, TimeSeries, Orbits, out$ = typeof exports != 'undefined' && exports || this;
438929a4   Goutte   Rewrite the orbit...
4
  GOLDEN_RATIO = 2 / (1 + Math.sqrt(5));
ae0aa7d2   Goutte   Add an x axis lab...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  out$.SpaceWeather = SpaceWeather = (function(){
    SpaceWeather.displayName = 'SpaceWeather';
    var prototype = SpaceWeather.prototype, constructor = SpaceWeather;
    function SpaceWeather(configuration){
      console.info("Creating Space Weather app...", configuration);
    }
    return SpaceWeather;
  }());
  Source = (function(){
    Source.displayName = 'Source';
    var prototype = Source.prototype, constructor = Source;
    function Source(slug){
      this.slug = slug;
      this.time_series = {};
    }
    Source.prototype.addTimeSeries = function(ts){
      return this.time_series[ts.slug] = ts;
    };
    Source.prototype.show = function(){
      var slug, ref$, ts, results$ = [];
      for (slug in ref$ = this.time_series) {
        ts = ref$[slug];
        results$.push($(ts.svg).show());
      }
      return results$;
    };
    Source.prototype.hide = function(){
      var slug, ref$, ts, results$ = [];
      for (slug in ref$ = this.time_series) {
        ts = ref$[slug];
        results$.push($(ts.svg).hide());
      }
      return results$;
    };
    return Source;
  }());
438929a4   Goutte   Rewrite the orbit...
41
42
43
  out$.TimeSeries = TimeSeries = (function(){
    TimeSeries.displayName = 'TimeSeries';
    var prototype = TimeSeries.prototype, constructor = TimeSeries;
6f9a3852   Goutte   Hide time series ...
44
45
    function TimeSeries(slug, title, data, container, options){
      this.slug = slug;
438929a4   Goutte   Rewrite the orbit...
46
47
48
49
50
51
      this.title = title;
      this.data = data;
      this.container = container;
      this.options = options != null
        ? options
        : {};
541e2936   Goutte   Synchronize the t...
52
53
54
      this.onMouseOut = bind$(this, 'onMouseOut', prototype);
      this.onMouseOver = bind$(this, 'onMouseOver', prototype);
      this.onMouseMove = bind$(this, 'onMouseMove', prototype);
ae0aa7d2   Goutte   Add an x axis lab...
55
      console.info("Creating time series '" + this.title + "'...");
438929a4   Goutte   Rewrite the orbit...
56
57
58
      this.init();
    }
    TimeSeries.prototype.init = function(){
541e2936   Goutte   Synchronize the t...
59
      var dx, this$ = this;
ae0aa7d2   Goutte   Add an x axis lab...
60
      console.info("Initializing time series '" + this.title + "'...", this.data, this.options);
438929a4   Goutte   Rewrite the orbit...
61
62
63
64
65
66
67
68
69
70
71
72
      this.margin = {
        top: 30,
        right: 20,
        bottom: 30,
        left: 60
      };
      this.xScale = d3.scaleTime().domain(d3.extent(this.data, function(d){
        return d.x;
      }));
      this.yScale = d3.scaleLinear().domain(d3.extent(this.data, function(d){
        return d.y;
      }));
438929a4   Goutte   Rewrite the orbit...
73
74
75
76
77
78
79
80
      this.xAxis = d3.axisBottom().ticks(7, ",f").tickFormat(d3.timeFormat("%Y-%m-%d"));
      this.yAxis = d3.axisLeft().ticks(10);
      this.line = d3.line().x(function(d){
        return this$.xScale(d.x);
      }).y(function(d){
        return this$.yScale(d.y);
      });
      this.svg = d3.select(this.container).append('svg');
6f9a3852   Goutte   Hide time series ...
81
      this.svg.attr("class", this.slug);
438929a4   Goutte   Rewrite the orbit...
82
83
      this.plotWrapper = this.svg.append('g');
      this.plotWrapper.attr('transform', 'translate(' + this.margin.left + ',' + this.margin.top + ')');
438929a4   Goutte   Rewrite the orbit...
84
      this.path = this.plotWrapper.append('path').datum(this.data).classed('line', true);
2463bd16   Goutte   Add a circle foll...
85
      this.mouseCanvas = this.plotWrapper.append("rect").style("fill", "none").style("pointer-events", "all");
541e2936   Goutte   Synchronize the t...
86
      this.mouseCanvas.on("mouseover", this.onMouseOver).on("mouseout", this.onMouseOut).on("mousemove", this.onMouseMove);
438929a4   Goutte   Rewrite the orbit...
87
88
89
      this.plotWrapper.append('g').classed('x axis', true);
      this.plotWrapper.append('g').classed('y axis', true);
      this.yAxisText = this.plotWrapper.append("text").attr("transform", "rotate(-90)").attr("dy", "1em").style("text-anchor", "middle").text(this.title);
81c9b2e8   Goutte   Add the values to...
90
91
      this.focus = this.plotWrapper.append('g').style("display", "none");
      this.cursorCircle = this.focus.append("circle").attr("class", "cursor-circle").attr("r", 3);
541e2936   Goutte   Synchronize the t...
92
93
94
95
96
      dx = 8;
      this.cursorValueShadow = this.focus.append("text").attr("class", "cursor-text cursor-text-shadow").attr("dx", dx).attr("dy", "-.3em");
      this.cursorValue = this.focus.append("text").attr("class", "cursor-text cursor-value").attr("dx", dx).attr("dy", "-.3em");
      this.cursorDateShadow = this.focus.append("text").attr("class", "cursor-text cursor-text-shadow").attr("dx", dx).attr("dy", "1em");
      this.cursorDate = this.focus.append("text").attr("class", "cursor-text cursor-date").attr("dx", dx).attr("dy", "1em");
438929a4   Goutte   Rewrite the orbit...
97
98
99
100
101
      return this.resize();
    };
    TimeSeries.prototype.resize = function(){
      var width, height;
      width = jQuery(this.container).width() - this.margin.left - this.margin.right;
541e2936   Goutte   Synchronize the t...
102
103
104
      height = GOLDEN_RATIO * GOLDEN_RATIO * GOLDEN_RATIO * GOLDEN_RATIO * width;
      this.plotWidth = width;
      this.plotHeight = height;
438929a4   Goutte   Rewrite the orbit...
105
106
107
108
109
110
111
      console.log("Resize time series " + this.title + " : " + width + " x " + height);
      this.xScale.range([0, width]);
      this.yScale.range([height, 0]);
      this.svg.attr('width', width + this.margin.right + this.margin.left).attr('height', height + this.margin.top + this.margin.bottom);
      this.path.attr('d', this.line);
      this.xAxis.scale(this.xScale);
      this.yAxis.scale(this.yScale);
2463bd16   Goutte   Add a circle foll...
112
113
      this.xAxis.ticks(Math.floor(width / 60));
      this.yAxis.ticks(Math.floor(height / 18));
438929a4   Goutte   Rewrite the orbit...
114
115
116
      this.svg.select('.x.axis').attr('transform', 'translate(0,' + height + ')').call(this.xAxis);
      this.svg.select('.y.axis').call(this.yAxis);
      this.yAxisText.attr("y", 0 - this.margin.left).attr("x", 0 - height / 2);
2463bd16   Goutte   Add a circle foll...
117
      this.mouseCanvas.attr("width", width).attr("height", height);
2463bd16   Goutte   Add a circle foll...
118
119
      return this;
    };
541e2936   Goutte   Synchronize the t...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    TimeSeries.prototype.onMouseMove = function(){
      var x;
      x = this.xScale.invert(d3.mouse(this.mouseCanvas.node())[0]);
      if (this.options.onMouseMove != null) {
        return this.options.onMouseMove(x);
      } else {
        return this.moveCursor(x);
      }
    };
    TimeSeries.prototype.onMouseOver = function(){
      if (this.options.onMouseOver != null) {
        return this.options.onMouseOver();
      } else {
        return this.showCursor();
      }
    };
    TimeSeries.prototype.onMouseOut = function(){
      if (this.options.onMouseOut != null) {
        return this.options.onMouseOut();
      } else {
        return this.hideCursor();
      }
    };
    TimeSeries.prototype.showCursor = function(){
      return this.focus.style("display", null);
    };
    TimeSeries.prototype.hideCursor = function(){
      return this.focus.style("display", "none");
    };
2463bd16   Goutte   Add a circle foll...
149
150
151
    TimeSeries.prototype.bisectDate = d3.bisector(function(d){
      return d.x;
    }).left;
81c9b2e8   Goutte   Add the values to...
152
    TimeSeries.prototype.timeFormat = d3.timeFormat("%Y-%m-%d %Hh");
541e2936   Goutte   Synchronize the t...
153
154
    TimeSeries.prototype.moveCursor = function(x0){
      var i, d0, d1, d, xx, yy, transform, mirrored, dx;
2463bd16   Goutte   Add a circle foll...
155
156
157
      i = this.bisectDate(this.data, x0, 1);
      d0 = this.data[i - 1];
      d1 = this.data[i];
541e2936   Goutte   Synchronize the t...
158
159
160
      if (!(d1 && d0)) {
        return;
      }
2463bd16   Goutte   Add a circle foll...
161
162
163
      d = x0 - d0.x > d1.x - x0 ? d1 : d0;
      xx = this.xScale(d.x);
      yy = this.yScale(d.y);
81c9b2e8   Goutte   Add the values to...
164
      transform = "translate(" + xx + ", " + yy + ")";
541e2936   Goutte   Synchronize the t...
165
166
167
168
169
      mirrored = this.plotWidth != null && xx > this.plotWidth / 2 ? true : false;
      dx = 8;
      if (mirrored) {
        dx = -1 * dx;
      }
81c9b2e8   Goutte   Add the values to...
170
      this.cursorCircle.attr("transform", transform);
541e2936   Goutte   Synchronize the t...
171
172
173
174
      this.cursorValue.attr("transform", transform).text(d.y).attr('text-anchor', mirrored ? 'end' : 'start').attr("dx", dx);
      this.cursorValueShadow.attr("transform", transform).text(d.y).attr('text-anchor', mirrored ? 'end' : 'start').attr("dx", dx);
      this.cursorDate.attr("transform", transform).text(this.timeFormat(d.x)).attr('text-anchor', mirrored ? 'end' : 'start').attr("dx", dx);
      this.cursorDateShadow.attr("transform", transform).text(this.timeFormat(d.x)).attr('text-anchor', mirrored ? 'end' : 'start').attr("dx", dx);
438929a4   Goutte   Rewrite the orbit...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
      return this;
    };
    return TimeSeries;
  }());
  out$.Orbits = Orbits = (function(){
    Orbits.displayName = 'Orbits';
    var prototype = Orbits.prototype, constructor = Orbits;
    function Orbits(orbiters, data, container, options){
      this.orbiters = orbiters;
      this.data = data;
      this.container = container;
      this.options = options != null
        ? options
        : {};
      console.log("Create orbits");
      this.init();
    }
    Orbits.prototype.init = function(){
      var slug, ref$, config;
      console.log("Initialize orbits", this.data, this.options);
      this.margin = {
        top: 30,
        right: 20,
11662eed   Goutte   Add Y axis label ...
198
        bottom: 42,
438929a4   Goutte   Rewrite the orbit...
199
200
201
202
203
204
205
206
207
208
209
210
        left: 60
      };
      this.extremum = 1.11 * d3.max(this.data, function(d){
        return Math.max(Math.abs(d.x), Math.abs(d.y));
      });
      this.xScale = d3.scaleLinear().domain([-1 * this.extremum, this.extremum]);
      this.yScale = d3.scaleLinear().domain([-1 * this.extremum, this.extremum]);
      this.xAxis = d3.axisBottom().ticks(10);
      this.yAxis = d3.axisLeft().ticks(10);
      this.svg = d3.select(this.container).append('svg');
      this.plotWrapper = this.svg.append('g');
      this.plotWrapper.attr('transform', 'translate(' + this.margin.left + ',' + this.margin.top + ')');
ae0aa7d2   Goutte   Add an x axis lab...
211
212
213
214
215
      this.xAxisLine = this.plotWrapper.append('g').classed('x axis', true);
      this.yAxisLine = this.plotWrapper.append('g').classed('y axis', true);
      this.xAxisTitle = this.xAxisLine.append('text').attr('fill', '#000');
      this.xAxisTitle.style("text-anchor", "middle");
      this.xAxisTitle.append('tspan').text('X');
11662eed   Goutte   Add Y axis label ...
216
      this.xAxisTitle.append('tspan').attr('dy', '3px').text('HEE').attr('font-size', '8px');
ae0aa7d2   Goutte   Add an x axis lab...
217
      this.xAxisTitle.append('tspan').attr('dy', '-3px').text('   (AU)');
11662eed   Goutte   Add Y axis label ...
218
219
220
221
222
223
      this.yAxisTitle = this.yAxisLine.append('text').attr('fill', '#000');
      this.yAxisTitle.style("text-anchor", "middle");
      this.yAxisTitle.append('tspan').text('Y');
      this.yAxisTitle.append('tspan').attr('dy', '3px').text('HEE').attr('font-size', '8px');
      this.yAxisTitle.append('tspan').attr('dy', '-3px').text('   (AU)');
      this.yAxisTitle.attr('transform', 'rotate(-90)');
438929a4   Goutte   Rewrite the orbit...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
      this.sun = this.plotWrapper.append("svg:circle");
      this.sun.append('svg:title').text("Sol");
      this.sun.attr("r", 17).style("fill", "yellow");
      for (slug in ref$ = this.orbiters) {
        config = ref$[slug];
        this.initOrbiter(slug, config);
      }
      return this.resize();
    };
    Orbits.prototype.orbitersElements = {};
    Orbits.prototype.initOrbiter = function(slug, config){
      var orbit_ellipse, orbiter, orbit_line, orbit_section, this$ = this;
      if (slug in this.orbitersElements) {
        throw new Error("Second init of " + slug);
      }
      orbit_ellipse = this.plotWrapper.append("svg:ellipse").classed('orbit orbit_ellipse', true);
      orbiter = this.plotWrapper.append("svg:image").attr('xlink:href', config['img']).attr('width', '32px').attr('height', '32px');
      orbit_line = d3.line().x(function(d){
        return this$.xScale(d.x);
      }).y(function(d){
        return this$.yScale(d.y);
      });
      orbit_section = this.plotWrapper.append('path').datum(this.data).classed('orbit orbit_section', true);
      this.orbitersElements[slug] = {
        orbiter: orbiter,
        orbit_ellipse: orbit_ellipse,
        orbit_section: orbit_section,
        orbit_line: orbit_line
      };
      return this;
    };
    Orbits.prototype.resize = function(){
      var width, height, slug, ref$, config;
      width = jQuery(this.container).width() - this.margin.left - this.margin.right;
      height = 1.0 * width;
      console.log("Resize orbits : " + width + " x " + height);
      this.xScale.range([0, width]);
      this.yScale.range([height, 0]);
      this.svg.attr('width', width + this.margin.right + this.margin.left).attr('height', height + this.margin.top + this.margin.bottom);
      this.sun.attr("cx", width / 2).attr("cy", height / 2);
      for (slug in ref$ = this.orbiters) {
        config = ref$[slug];
        this.resizeOrbiter(slug, config);
      }
      this.xAxis.scale(this.xScale);
      this.yAxis.scale(this.yScale);
      this.svg.select('.x.axis').attr('transform', 'translate(0,' + height + ')').call(this.xAxis);
      this.svg.select('.y.axis').call(this.yAxis);
11662eed   Goutte   Add Y axis label ...
272
273
      this.xAxisTitle.attr("x", width / 2).attr("y", 37);
      this.yAxisTitle.attr("x", -1 * height / 2).attr("y", -30);
438929a4   Goutte   Rewrite the orbit...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
      return this;
    };
    Orbits.prototype.resizeOrbiter = function(slug, config){
      var width, height, el, a, b, c, cx, cy, data;
      width = jQuery(this.container).width() - this.margin.left - this.margin.right;
      height = 1.0 * width;
      console.log("Resize orbiter " + slug);
      el = this.orbitersElements[slug];
      el['orbit_section'].attr('d', el['orbit_line']);
      a = config['orbit']['a'];
      b = config['orbit']['b'];
      c = Math.sqrt(a * a - b * b);
      cx = width / 2 - c;
      cy = height / 2;
      this.yScale.range([0, height]);
      el['orbit_ellipse'].attr('cx', cx).attr('cy', cy).attr('rx', this.xScale(a) - this.xScale(0)).attr('ry', this.yScale(b) - this.yScale(0)).attr('transform', 'rotate(66,' + (cx + c) + ', ' + cy + ')');
      this.yScale.range([height, 0]);
      data = this.data;
      el['orbiter'].attr('x', this.xScale(data[data.length - 1].x) - 16);
      el['orbiter'].attr('y', this.yScale(data[data.length - 1].y) - 16);
      return this;
    };
ae0aa7d2   Goutte   Add an x axis lab...
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    Orbits.prototype.repositionOrbiter = function(slug, datum){
      var data, el;
      data = this.data;
      datum == null && (datum = data[data.length - 1]);
      el = this.orbitersElements[slug];
      el['orbiter'].attr('x', this.xScale(datum.x) - 16);
      el['orbiter'].attr('y', this.yScale(datum.y) - 16);
      return this;
    };
    Orbits.prototype.bisectDate = d3.bisector(function(d){
      return d.t;
    }).left;
    Orbits.prototype.moveToDate = function(t){
      var slug, ref$, el, data, i, d0, d1, d, results$ = [];
      for (slug in ref$ = this.orbitersElements) {
        el = ref$[slug];
        data = this.data;
        i = this.bisectDate(data, t, 1);
        d0 = data[i - 1];
        d1 = data[i];
        if (!(d1 && d0)) {
          continue;
        }
        d = t - d0.t > d1.t - t ? d1 : d0;
        results$.push(this.repositionOrbiter(slug, d));
      }
      return results$;
    };
438929a4   Goutte   Rewrite the orbit...
324
325
    return Orbits;
  }());
2463bd16   Goutte   Add a circle foll...
326
327
328
  function bind$(obj, key, target){
    return function(){ return (target || obj)[key].apply(obj, arguments) };
  }
438929a4   Goutte   Rewrite the orbit...
329
}).call(this);