swapp.js 30.5 KB
1 2 3 4 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
// Generated by LiveScript 1.5.0
(function(){
  var GOLDEN_RATIO, Target, SpaceWeather, TimeSeries, Orbits, out$ = typeof exports != 'undefined' && exports || this;
  GOLDEN_RATIO = 2 / (1 + Math.sqrt(5));
  Target = (function(){
    Target.displayName = 'Target';
    var prototype = Target.prototype, constructor = Target;
    function Target(slug, name, config){
      this.slug = slug;
      this.name = name;
      this.config = config;
      this.active = this.config.active;
    }
    return Target;
  }());
  out$.SpaceWeather = SpaceWeather = (function(){
    "The main app, instanciated from an inline script.\nIt defaults to an interval starting a year ago, and ending in seven days.\n(both at midnight)";
    SpaceWeather.displayName = 'SpaceWeather';
    var API_TIME_FORMAT, INPUT_TIME_FORMAT, prototype = SpaceWeather.prototype, constructor = SpaceWeather;
    API_TIME_FORMAT = "YYYY-MM-DDTHH:mm:ss";
    INPUT_TIME_FORMAT = "YYYY-MM-DD";
    function SpaceWeather(configuration){
      var configs, res$, k, this$ = this;
      this.configuration = configuration;
      console.info("  _   _      _ _       ____\n | | | | ___| (_) ___ |  _ \\ _ __ ___  _ __   __ _\n | |_| |/ _ \\ | |/ _ \\| |_) | '__/ _ \\| '_ \\ / _` |\n |  _  |  __/ | | (_) |  __/| | | (_) | |_) | (_| |\n |_| |_|\\___|_|_|\\___/|_|_  |_|_ \\___/| .__/ \\__,_|\n | |__  _   _   / ___|  _ \\|  _ \\|  _ \\_|\n | '_ \\| | | | | |   | | | | |_) | |_) |\n | |_) | |_| | | |___| |_| |  __/|  __/\n |_.__/ \\__, |  \\____|____/|_|   |_|\n        |___/\n\nThe full source of this website is available at :\nhttps://gitlab.irap.omp.eu/CDPP/SPACEWEATHERONLINE");
      this.targets = {};
      res$ = [];
      for (k in this.configuration.targets) {
        res$.push(this.configuration.targets[k]);
      }
      configs = res$;
      configs.forEach(function(target_config){
        return this$.targets[target_config.slug] = new Target(target_config.slug, target_config.name, target_config);
      });
      this.parameters = {};
      this.configuration['parameters'].forEach(function(p){
        return this$.parameters[p['id']] = p;
      });
      this.orbiter = null;
      this.timeSeries = [];
    }
    SpaceWeather.prototype.init = function(){
      "This is called by the inline bootstrap javascript code.\nThis ain't in the constructor because it might return a Promise later on.\n(for the loader, for example)";
      var started_at, stopped_at, this$ = this;
      started_at = moment().subtract(2, 'weeks').hours(0).minutes(0).seconds(0);
      stopped_at = moment().add(1, 'week').hours(0).minutes(0).seconds(0);
      this.setStartAndStop(started_at, stopped_at);
      this.loadAndCreatePlots(started_at, stopped_at);
      return window.addEventListener('resize', function(){
        return this$.resize();
      });
    };
    SpaceWeather.prototype.buildDataUrlForTarget = function(target_slug, started_at, stopped_at){
      var url;
      url = this.configuration['api']['data_for_interval'];
      url = url.replace('<target>', target_slug);
      url = url.replace('<started_at>', started_at);
      url = url.replace('<stopped_at>', stopped_at);
      return url;
    };
    SpaceWeather.prototype.addTarget = function(target){
      this.targets[target.slug] = target;
      return this;
    };
    SpaceWeather.prototype.enableTarget = function(target_slug){
      var this$ = this;
      this.timeSeries.forEach(function(ts){
        if (ts.target.slug === target_slug && this$.parameters[ts.parameter].active) {
          return ts.show();
        }
      });
      this.targets[target_slug].active = true;
      return this;
    };
    SpaceWeather.prototype.disableTarget = function(target_slug){
      this.timeSeries.forEach(function(ts){
        if (ts.target.slug === target_slug) {
          return ts.hide();
        }
      });
      this.targets[target_slug].active = false;
      return this;
    };
    SpaceWeather.prototype.resize = function(){
      var ref$;
      if ((ref$ = this.orbits) != null) {
        ref$.resize();
      }
      return this.timeSeries.forEach(function(ts){
        return ts.resize();
      });
    };
    SpaceWeather.prototype.showLoader = function(){
      return $("#plots_loader").show();
    };
    SpaceWeather.prototype.hideLoader = function(){
      return $("#plots_loader").hide();
    };
    SpaceWeather.prototype.loadData = function(target_slug, started_at, stopped_at){
      "Load the data as CSV for the specified target and interval,\nand return it in a Promise.";
      var sw;
      sw = this;
      return new Promise(function(resolve, reject){
        var url;
        url = sw.buildDataUrlForTarget(target_slug, started_at, stopped_at);
        return d3.csv(url, function(csv){
          var timeFormat, data;
          timeFormat = d3.timeParse('%Y-%m-%dT%H:%M:%S%Z');
          data = {
            'hci': []
          };
          configuration['parameters'].forEach(function(parameter){
            return data[parameter['id']] = [];
          });
          if (!csv) {
            reject("CSV is empty or nonexistent at URL '" + url + "'.");
          }
          csv.forEach(function(d){
            var dtime;
            dtime = timeFormat(d['time']);
            configuration['parameters'].forEach(function(parameter){
              var id;
              id = parameter['id'];
              return data[id].push({
                x: dtime,
                y: parseFloat(d[id])
              });
            });
            if (d['xhci'] && d['yhci']) {
              return data['hci'].push({
                t: dtime,
                x: parseFloat(d['xhci']),
                y: parseFloat(d['yhci'])
              });
            }
          });
          return resolve(data);
        });
      });
    };
    SpaceWeather.prototype.loadAndCreatePlots = function(started_at, stopped_at){
      "started_at: moment(.js) object\nstopped_at: moment(.js) object";
      var k, this$ = this;
      this.showLoader();
      this.started_at = started_at;
      this.stopped_at = stopped_at;
      this.orbits = new Orbits(this.configuration.orbits_container, this.configuration);
      started_at = started_at.format(API_TIME_FORMAT);
      stopped_at = stopped_at.format(API_TIME_FORMAT);
      return (function(){
        var results$ = [];
        for (k in this.targets) {
          results$.push(this.targets[k]);
        }
        return results$;
      }.call(this)).forEach(function(target){
        var targetButton;
        console.info("Loading CSV data of " + target.name + "…");
        targetButton = $(".targets-filters .target." + target.slug);
        targetButton.addClass('loading');
        return this$.loadData(target.slug, started_at, stopped_at).then(function(data){
          console.info("Loaded CSV data of " + target.name + ".");
          this$.createTimeSeries(target, data);
          this$.orbits.initOrbiter(target.slug, target.config, data['hci']);
          targetButton.removeClass('loading');
          if (target.active) {
            return this$.hideLoader();
          } else {
            return this$.disableTarget(target.slug);
          }
        }, function(error){
          return console.error("Failed loading CSV data of " + target.name + ".", error);
        });
      });
    };
    SpaceWeather.prototype.clearPlots = function(){
      this.orbits.clear();
      this.timeSeries.forEach(function(ts){
        return ts.clear();
      });
      this.orbits = null;
      this.timeSeries = [];
      return this;
    };
    SpaceWeather.prototype.createTimeSeries = function(target, data){
      var this$ = this;
      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 this$.timeSeries.push(new TimeSeries(id, title, target, data[id], this$.parameters[id].active, container));
      });
      this.timeSeries.forEach(function(ts){
        ts.options['onMouseOver'] = function(){
          this$.timeSeries.forEach(function(ts2){
            return ts2.showCursor();
          });
          return true;
        };
        ts.options['onMouseOut'] = function(){
          this$.timeSeries.forEach(function(ts2){
            return ts2.hideCursor();
          });
          return true;
        };
        ts.options['onMouseMove'] = function(t){
          var ref$;
          this$.timeSeries.forEach(function(ts2){
            return ts2.moveCursor(t);
          });
          if ((ref$ = this$.orbits) != null) {
            ref$.moveToDate(t);
          }
          return true;
        };
        ts.options['onBrushEnd'] = function(sta, sto){
          this$.resizeDomain(moment(sta), moment(sto));
          return true;
        };
        return ts.options['onDblClick'] = function(){
          var ref$;
          this$.resetZoom();
          if ((ref$ = $("#zoom_controls_help")) != null) {
            ref$.remove();
          }
          return true;
        };
      });
      return this.timeSeries;
    };
    SpaceWeather.prototype.enableParameter = function(parameter_slug){
      var this$ = this;
      if (!(parameter_slug in this.parameters)) {
        console.error("Unknown parameter " + parameter_slug + ".");
      }
      this.parameters[parameter_slug].active = true;
      this.timeSeries.forEach(function(ts){
        if (ts.parameter === parameter_slug && this$.targets[ts.target.slug].active) {
          return ts.show();
        }
      });
      return this;
    };
    SpaceWeather.prototype.disableParameter = function(parameter_slug){
      if (!(parameter_slug in this.parameters)) {
        console.error("Unknown parameter " + parameter_slug + ".");
      }
      this.parameters[parameter_slug].active = false;
      this.timeSeries.forEach(function(ts){
        if (ts.parameter === parameter_slug) {
          return ts.hide();
        }
      });
      return this;
    };
    SpaceWeather.prototype.getDomain = function(){
      if (this.current_started_at != null && this.current_stopped_at != null) {
        return [this.current_started_at, this.current_stopped_at];
      }
      return [this.started_at, this.stopped_at];
    };
    SpaceWeather.prototype.resizeDomain = function(started_at, stopped_at){
      var ref$, max_stopped_at, formatted_started_at, formatted_stopped_at;
      if (stopped_at < started_at) {
        ref$ = [stopped_at, started_at], started_at = ref$[0], stopped_at = ref$[1];
      }
      if (started_at === stopped_at) {
        console.warn("Please provide distinct start and stop dates.");
        return;
      }
      max_stopped_at = started_at.clone().add(2, 'years');
      if (stopped_at > max_stopped_at) {
        console.warn("The time interval was truncated beacuse it was bigger than two years.");
        stopped_at = max_stopped_at;
      }
      this.setStartAndStop(started_at, stopped_at);
      formatted_started_at = started_at.format();
      formatted_stopped_at = stopped_at.format();
      if ((this.started_at <= started_at && started_at <= this.stopped_at) && (this.started_at <= stopped_at && stopped_at <= this.stopped_at)) {
        console.info("Resizing the temporal domain from " + formatted_started_at + " to " + formatted_stopped_at + " without fetching new data…");
        this.timeSeries.forEach(function(ts){
          if (!ts.visible) {
            return ts.zoomIn(started_at, stopped_at);
          }
        });
        this.timeSeries.forEach(function(ts){
          if (ts.visible) {
            return ts.zoomIn(started_at, stopped_at);
          }
        });
        this.orbits.resizeDomain(started_at, stopped_at);
      } else {
        console.info("Resizing the temporal domain from " + formatted_started_at + " to " + formatted_stopped_at + " and fetching new data…");
        console.warn("This might take a while… Why not see what else we're up to on http://cdpp.eu while you're waiting?");
        this.clearPlots();
        this.loadAndCreatePlots(started_at, stopped_at);
      }
      return this;
    };
    SpaceWeather.prototype.resetZoom = function(){
      this.timeSeries.forEach(function(ts){
        return ts.resetZoom();
      });
      this.orbits.resetZoom();
      this.setStartAndStop(this.started_at, this.stopped_at);
      return this;
    };
    SpaceWeather.prototype.setStartAndStop = function(started_at, stopped_at){
      console.info("Setting time interval from " + started_at + " to " + stopped_at + "…");
      this.current_started_at = started_at;
      this.current_stopped_at = stopped_at;
      $("#started_at").val(started_at.format(INPUT_TIME_FORMAT));
      $("#stopped_at").val(stopped_at.format(INPUT_TIME_FORMAT));
      return this;
    };
    return SpaceWeather;
  }());
  out$.TimeSeries = TimeSeries = (function(){
    TimeSeries.displayName = 'TimeSeries';
    var RATIO, prototype = TimeSeries.prototype, constructor = TimeSeries;
    function TimeSeries(parameter, title, target, data, visible, container, options){
      this.parameter = parameter;
      this.title = title;
      this.target = target;
      this.visible = visible;
      this.container = container;
      this.options = options != null
        ? options
        : {};
      this.onBrushEnd = bind$(this, 'onBrushEnd', prototype);
      this.onDoubleClick = bind$(this, 'onDoubleClick', prototype);
      this.onMouseOut = bind$(this, 'onMouseOut', prototype);
      this.onMouseOver = bind$(this, 'onMouseOver', prototype);
      this.onMouseMove = bind$(this, 'onMouseMove', prototype);
      this.setData(data);
      this.init();
    }
    TimeSeries.prototype.toString = function(){
      return this.title + " of " + this.target.name;
    };
    TimeSeries.prototype.setData = function(data){
      this.data = data;
      this.xDataExtent = d3.extent(this.data, function(d){
        return d.x;
      });
      return this.yDataExtent = d3.extent(this.data, function(d){
        return d.y;
      });
    };
    TimeSeries.prototype.init = function(){
      var clipId, dx, this$ = this;
      console.info("Initializing plot of " + this + "…");
      this.margin = {
        top: 30,
        right: 20,
        bottom: 30,
        left: 80
      };
      this.xScale = d3.scaleTime().domain(this.xDataExtent);
      this.yScale = d3.scaleLinear().domain(this.yDataExtent);
      this.xAxis = d3.axisBottom().ticks(7);
      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');
      this.svg.attr("class", this.parameter + " " + this.target.slug);
      this.plotWrapper = this.svg.append('g');
      this.plotWrapper.attr('transform', 'translate(' + this.margin.left + ',' + this.margin.top + ')');
      clipId = "ts-clip-" + this.parameter + "-" + this.target.slug;
      this.clip = this.svg.append("defs").append("svg:clipPath").attr("id", clipId).append("svg:rect").attr("x", 0).attr("y", 0);
      this.pathWrapper = this.plotWrapper.append('g');
      this.pathWrapper.attr("clip-path", "url(#" + clipId + ")");
      this.path = this.pathWrapper.append('path').datum(this.data).classed('line', true);
      this.brush = this.plotWrapper.append("g").attr("class", "brush");
      this.mouseCanvas = this.plotWrapper.append("rect").style("fill", "none");
      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);
      this.yAxisTextTarget = this.plotWrapper.append("text").attr("transform", "rotate(-90)").attr("dy", "1em").style("text-anchor", "middle").style("font-style", "oblique").text(this.target.name);
      this.focus = this.plotWrapper.append('g').style("display", "none");
      this.cursorCircle = this.focus.append("circle").attr("class", "cursor-circle").attr("r", 3);
      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");
      return this.resize();
    };
    RATIO = GOLDEN_RATIO * GOLDEN_RATIO * GOLDEN_RATIO * GOLDEN_RATIO;
    TimeSeries.prototype.resize = function(){
      var width, height;
      width = Math.ceil($(this.container).width() - this.margin.left - this.margin.right);
      height = Math.ceil(RATIO * width);
      this.plotWidth = width;
      this.plotHeight = height;
      console.debug("Resizing " + this + ": " + 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.clip.attr("width", width).attr("height", height);
      this.path.attr('d', this.line);
      this.xAxis.scale(this.xScale);
      this.yAxis.scale(this.yScale);
      this.xAxis.ticks(Math.floor(width / 90.0));
      this.yAxis.ticks(Math.floor(height / 18.0));
      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", 20 - this.margin.left).attr("x", 0 - height / 2);
      this.yAxisTextTarget.attr("y", 0 - this.margin.left).attr("x", 0 - height / 2);
      this.mouseCanvas.attr("width", width).attr("height", height);
      if (this.brushFunction == null) {
        console.debug("Creating the zooming brush for " + this + "…");
        this.brushFunction = d3.brushX().extent([[0, 0], [width, height]]).handleSize(0).on("end", this.onBrushEnd);
        this.brush.call(this.brushFunction);
        this.svg.select(".brush .overlay").on("mouseover.swapp", this.onMouseOver).on("mouseout.swapp", this.onMouseOut).on("mousemove.swapp", this.onMouseMove).on("dblclick.swapp", this.onDoubleClick);
      }
      if (!this.visible) {
        this.hide();
      }
      return this;
    };
    TimeSeries.prototype.clear = function(){
      $(this.svg.node()).remove();
      return this.visible = false;
    };
    TimeSeries.prototype.show = function(){
      $(this.svg.node()).show();
      return this.visible = true;
    };
    TimeSeries.prototype.hide = function(){
      $(this.svg.node()).hide();
      return this.visible = false;
    };
    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.onDoubleClick = function(){
      if (this.options.onDblClick != null) {
        return this.options.onDblClick();
      } else {
        return this.resetZoom();
      }
    };
    TimeSeries.prototype.onBrushEnd = function(){
      var s, minmax;
      s = d3.event.selection;
      if (s) {
        minmax = [s[0], s[1]].map(this.xScale.invert, this.xScale);
        this.brush.call(this.brushFunction.move, null);
        if (this.options.onBrushEnd != null) {
          return this.options.onBrushEnd(minmax[0], minmax[1]);
        } else {
          return this.zoomIn(minmax[0], minmax[1]);
        }
      }
    };
    TimeSeries.prototype.zoomIn = function(startDate, stopDate){
      var ref$, minDate, maxDate;
      console.debug("Zooming in " + this + " from " + startDate + " to " + stopDate + ".");
      ref$ = this.xDataExtent, minDate = ref$[0], maxDate = ref$[1];
      if (startDate < minDate) {
        startDate = minDate;
      }
      if (stopDate > maxDate) {
        stopDate = maxDate;
      }
      this.xScale.domain([startDate, stopDate]);
      return this.applyZoom();
    };
    TimeSeries.prototype.resetZoom = function(){
      this.xScale.domain(this.xDataExtent);
      this.yScale.domain(this.yDataExtent);
      return this.applyZoom();
    };
    TimeSeries.prototype.applyZoom = function(){
      var t;
      if (this.visible) {
        console.debug("Applying zoom to visible " + this + "…");
        t = this.svg.transition().duration(750);
        this.svg.select('.x.axis').transition(t).call(this.xAxis);
        this.svg.select('.y.axis').transition(t).call(this.yAxis);
        return this.path.transition(t).attr('d', this.line);
      } else {
        console.debug("Applying zoom to hidden " + this + "…");
        this.svg.select('.x.axis').call(this.xAxis);
        this.svg.select('.y.axis').call(this.yAxis);
        return this.path.attr('d', this.line);
      }
    };
    TimeSeries.prototype.showCursor = function(){
      return this.focus.style("display", null);
    };
    TimeSeries.prototype.hideCursor = function(){
      return this.focus.style("display", "none");
    };
    TimeSeries.prototype.bisectDate = d3.bisector(function(d){
      return d.x;
    }).left;
    TimeSeries.prototype.timeFormat = d3.timeFormat("%Y-%m-%d %Hh");
    TimeSeries.prototype.moveCursor = function(x0){
      var i, d0, d1, d, xx, yy, mirrored, dx, transform;
      i = this.bisectDate(this.data, x0, 1);
      d0 = this.data[i - 1];
      d1 = this.data[i];
      if (!(d1 && d0)) {
        return;
      }
      d = x0 - d0.x > d1.x - x0 ? d1 : d0;
      xx = this.xScale(d.x);
      yy = this.yScale(d.y);
      mirrored = this.plotWidth != null && xx > this.plotWidth / 2 ? true : false;
      dx = 8;
      if (mirrored) {
        dx = -1 * dx;
      }
      transform = "translate(" + xx + ", " + yy + ")";
      this.cursorCircle.attr("transform", transform);
      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);
      return this;
    };
    return TimeSeries;
  }());
  out$.Orbits = Orbits = (function(){
    "View of the solar system from above, with orbits segments for selected time\ninterval, from real data.";
    Orbits.displayName = 'Orbits';
    var prototype = Orbits.prototype, constructor = Orbits;
    function Orbits(container, options){
      this.container = container;
      this.options = options != null
        ? options
        : {};
      this.init();
    }
    Orbits.prototype.init = function(){
      console.log("Initializing plot of orbits…");
      this.margin = {
        top: 30,
        right: 20,
        bottom: 42,
        left: 60
      };
      this.data = {};
      this.orbiters = {};
      this.orbitersElements = {};
      this.extremum = 1;
      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 + ')');
      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');
      this.xAxisTitle.append('tspan').attr('dy', '3px').text('HEE').attr('font-size', '8px');
      this.xAxisTitle.append('tspan').attr('dy', '-3px').text('   (AU)');
      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)');
      this.sun = this.plotWrapper.append("svg:image").attr('xlink:href', this.options.sun.img).attr('width', '32px').attr('height', '32px');
      this.sun.append('svg:title').text("Sol");
      $(this.svg.node()).hide();
      return this.resize();
    };
    Orbits.prototype.initOrbiter = function(slug, config, data){
      var orbit_ellipse, orbiter, orbit_line, orbit_section, this$ = this;
      console.info("Initializing orbit of " + config.name + "…");
      if (slug in this.orbitersElements) {
        throw new Error("Second init of " + slug);
      }
      this.extremum = Math.max(this.extremum, 1.11 * d3.max(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]);
      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(data).classed('orbit orbit_section', true);
      this.orbiters[slug] = config;
      this.data[slug] = data;
      this.orbitersElements[slug] = {
        orbiter: orbiter,
        orbit_ellipse: orbit_ellipse,
        orbit_section: orbit_section,
        orbit_line: orbit_line
      };
      this.resize();
      $(this.svg.node()).show();
      return this;
    };
    Orbits.prototype.clear = function(){
      return $(this.svg.node()).remove();
    };
    Orbits.prototype.resize = function(){
      var width, height, slug, ref$, config;
      width = Math.ceil($(this.container).width() - this.margin.left - this.margin.right);
      height = Math.ceil(1.0 * width);
      console.debug("Resizing orbits : " + width + " × " + 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("x", width / 2 - 16).attr("y", height / 2 - 16);
      for (slug in ref$ = this.orbiters) {
        config = ref$[slug];
        this.resizeOrbiter(slug, config, width, height);
      }
      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);
      this.xAxisTitle.attr("x", width / 2).attr("y", 37);
      this.yAxisTitle.attr("x", -1 * height / 2).attr("y", -30);
      return this;
    };
    Orbits.prototype.resizeOrbiter = function(slug, config, width, height){
      var el, a, b, c, cx, cy, data;
      console.debug("Resizing orbit of " + 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));
      this.yScale.range([height, 0]);
      data = this.data[slug];
      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;
    };
    Orbits.prototype.repositionOrbiter = function(slug, datum){
      var data, el;
      data = this.data[slug];
      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;
      if (!t) {
        console.warn("Trying to move to an undefined date !");
      }
      for (slug in ref$ = this.orbitersElements) {
        el = ref$[slug];
        data = this.data[slug];
        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;
        this.repositionOrbiter(slug, d);
      }
      return this;
    };
    Orbits.prototype.resizeDomain = function(started_at, stopped_at){
      var slug, ref$, config, el, data, results$ = [];
      for (slug in ref$ = this.orbiters) {
        config = ref$[slug];
        el = this.orbitersElements[slug];
        data = this.data[slug].filter(fn$);
        el['orbit_section'].datum(data);
        results$.push(el['orbit_section'].attr('d', el['orbit_line']));
      }
      return results$;
      function fn$(d){
        var ref$;
        return started_at <= (ref$ = d.t) && ref$ <= stopped_at;
      }
    };
    Orbits.prototype.resetZoom = function(){
      var slug, ref$, config, el, results$ = [];
      for (slug in ref$ = this.orbiters) {
        config = ref$[slug];
        el = this.orbitersElements[slug];
        el['orbit_section'].datum(this.data[slug]);
        results$.push(el['orbit_section'].attr('d', el['orbit_line']));
      }
      return results$;
    };
    return Orbits;
  }());
  function bind$(obj, key, target){
    return function(){ return (target || obj)[key].apply(obj, arguments) };
  }
}).call(this);