summaryrefslogtreecommitdiff
path: root/code/errorvis.js
blob: 9cd87f4c9ae3cf3a5a1071adcb2a6933589dd9f9 (plain)
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
var ErrorVis = (function() {
    var margins = {
        "top": 40,
        "bottom": 40,
        "left": 40,
        "right": 40
    };

    function ErrorVis() {
        this.container = d3.select("body").append("div").remove();
        this.svgContainer = this.container.append("div");
        this.svg = this.svgContainer.append("svg");
        this.ready = false;
        this.readyHooks = [];

        this.svgContainer
            .style("margin-top", "1.5em")
            .style("margin-bottom", "1em")
            .style("height", "100%")
            .style("width", "100%");

        this.spinner = this.container.append("div")
            .attr("class", "mdl-spinner mdl-js-spinner mdl-spinner--single-color is-active spinner")
            .style("position", "absolute")
            .style("left", "50%")
            .style("top", "50%");
        componentHandler.upgradeElement(this.spinner.node());
    }

    ErrorVis.prototype.addPlaceholder = function(el) {
        el.appendChild(this.spinner.node().cloneNode(true));
    };

    ErrorVis.prototype.afterLoad = function(cb) {
        if (!this.ready) {
            this.readyHooks.push(cb);
        } else {
            cb();
        }
    };

    ErrorVis.prototype.load = function(url, callback) {
        var self = this;
        d3.json(url)
            .get(function(e, json) {
                if (e) console.log(e);
                self.data = Object.keys(json).map(function(k) { return parseFloat(json[k]); });
                self.ready = true;
                callback();
                self.readyHooks.forEach(function(hook) { hook(); });
            });
    };

    ErrorVis.prototype.openIn = function(el) {
        d3.select(el).select(".spinner").remove();
        el.appendChild(this.svgContainer.node());
        this.reposition();
    };

    ErrorVis.prototype.remove = function(el) {
        this.svgContainer.node().parentNode.appendChild(this.spinner.node().cloneNode(true));
        this.svgContainer.node().parentNode.removeChild(this.svgContainer.node());
    };

    ErrorVis.prototype.reposition = function() {
        var self = this;
        // TODO may not need to recreate everything
        this.svg.remove();
        this.svg = this.svgContainer.append("svg");

        var width = parseInt(this.svgContainer.style("width"))   || 800,
            height = parseInt(this.svgContainer.style("height")) || 600;
        this.svg
            .style("width", width)
            .style("height", height);

        this.scalerX = d3.scale.linear()
            .range([margins.left, width-margins.right])
            .domain([0, this.data.length]);
        this.scalerY = d3.scale.linear()
            .range([height-margins.top, margins.bottom])
            .domain(d3.extent(this.data));

        var xAxis = d3.svg.axis()
            .scale(this.scalerX);
        var yAxis = d3.svg.axis()
            .orient("left")
            .scale(this.scalerY);

        var gy = this.svg.append("g")
            .attr("transform", "translate(" + margins.left + ", 0)")
            .attr("class", "axis")
            .call(yAxis);
        var gx = this.svg.append("g")
            .attr("transform", "translate(0, " + (height-margins.bottom) + ")")
            .attr("class", "axis")
            .call(xAxis);

        var line = d3.svg.line()
            .x(function(d, i) { return self.scalerX(i); })
            .y(function(d) { return self.scalerY(d); });

        this.svg.append("path")
            .attr("d", line(this.data))
            .attr("stroke", "#673ab7")
            .attr("stroke-width", 5)
            .attr("fill", "none");

        this.marker = this.svg.append("circle")
            .attr("display", "none")
            .attr("r", 10)
            .attr("fill", "#2962ff");
    };

    ErrorVis.prototype.showMarker = function(step) {
        this.marker.style("display", "inherit");
        this.marker.attr("cx", this.scalerX(step));
        this.marker.attr("cy", this.scalerY(this.data[step]));
    };

    ErrorVis.prototype.hideMarker = function() {
        this.marker.style("display", "none");
    };

    return ErrorVis;
})();