-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph.js
130 lines (109 loc) · 4.16 KB
/
graph.js
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
// Generated by CoffeeScript 1.3.3
(function() {
var BarGraph, Graph,
__slice = [].slice,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Graph = (function() {
function Graph(options) {
var _base, _base1, _base2, _ref, _ref1, _ref2;
this.options = options != null ? options : {};
if ((_ref = (_base = this.options).height) == null) {
_base.height = 200;
}
if ((_ref1 = (_base1 = this.options).interval) == null) {
_base1.interval = 100;
}
if ((_ref2 = (_base2 = this.options).extraStyles) == null) {
_base2.extraStyles = '';
}
this.points = [];
}
Graph.prototype.add = function() {
var vals;
vals = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
this.points.push.apply(this.points, vals);
return this.renderIfWatching();
};
Graph.prototype.clear = function() {
this.points = [];
return this.renderIfWatching();
};
Graph.prototype.watch = function(opts) {
return this.watching = true;
};
Graph.prototype.stop = function() {
this.watching = false;
return clearTimeout(this.timeout);
};
Graph.prototype.renderIfWatching = function() {
var timeSinceLast, timeToNext,
_this = this;
if (this.watching) {
if (!(this.lastRender != null)) {
timeToNext = 0;
} else {
timeSinceLast = +(new Date) - this.lastRender;
timeToNext = this.options.interval - timeSinceLast;
}
return this.timeout = setTimeout(function() {
return _this.render();
}, Math.max(timeToNext, 0));
}
};
Graph.prototype.render = function() {
return this.lastRender = +(new Date);
};
return Graph;
})();
BarGraph = (function(_super) {
__extends(BarGraph, _super);
function BarGraph(options) {
var _base, _ref;
this.options = options != null ? options : {};
BarGraph.__super__.constructor.apply(this, arguments);
if ((_ref = (_base = this.options).barWidth) == null) {
_base.barWidth = 3;
}
}
BarGraph.prototype._inspectorHeight = function() {
return window.outerHeight - window.innerHeight - this.options.height;
};
BarGraph.prototype.render = function() {
var height, i, inspectorHeight, inspectorWidth, point, pointMax, spaces, steps, styles, xScaling, yScaling, _base, _i, _ref;
BarGraph.__super__.render.apply(this, arguments);
inspectorHeight = this._inspectorHeight();
inspectorWidth = window.innerWidth;
console.log("%c ", "font-size: " + inspectorHeight + "px");
console.log("%c ", "padding-bottom: " + (this.options.height + this.options.barWidth) + "px");
steps = inspectorWidth / this.options.barWidth;
xScaling = Math.min(steps / this.points.length, 1);
pointMax = Math.max.apply(Math, this.points);
yScaling = pointMax / this.options.height;
spaces = "";
styles = [];
for (i = _i = 0, _ref = Math.min(steps, this.points.length); 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
point = this.points[Math.floor(i / xScaling)];
spaces += "%c ";
height = (point / yScaling) + this.options.barWidth;
styles.push("font-size: " + this.options.barWidth + "px; background-color: #444; padding-bottom: " + height + "px; " + this.options.extraStyles);
}
if (typeof (_base = this.options).step === "function") {
_base.step();
}
return console.log.apply(console, [spaces].concat(__slice.call(styles)));
};
return BarGraph;
})(Graph);
window.console.graph = function(options) {
var graph;
graph = new BarGraph(options);
if (options.points != null) {
graph.add.apply(graph, options.points);
}
graph.render();
return graph;
};
console.graph.Graph = Graph;
console.graph.BarGraph = BarGraph;
}).call(this);