-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
179 lines (151 loc) · 2.88 KB
/
index.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
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
/**
* Module dependencies.
*/
var svg = require('svg');
var min = require('min');
var max = require('max');
var bin = require('bin');
/**
* Expose `Histogram`.
*/
module.exports = Histogram;
/**
* Initialize a new histogram.
*
* @api public
*/
function Histogram() {
this.datasets = [];
this.bins(10);
this.barColor = '#555555';
this.barWidth = 1.5;
this.el = document.createElement('div');
this.el.className = 'histogram';
this.svg = svg(this.el);
}
/**
* Add `data` set with `options`.
*
* Options:
*
* - `color` bar color
*
* @param {Array} data
* @param {Object} options
* @return {Histogram} self
* @api public
*/
Histogram.prototype.add = function(data, options){
this.datasets.push({
data: data,
options: options || {}
});
return this;
};
/**
* Set size to `w` / `h`.
*
* @param {Number} w
* @param {Number} h
* @return {Histogram} self
* @api public
*/
Histogram.prototype.size = function(w, h){
this.width = w;
this.height = h;
this.el.style.width = w + 'px';
this.el.style.height = h + 'px';
return this;
};
/**
* Set the total number of bins to `n`.
*
* @param {Number} n
* @return {Histogram} self
* @api public
*/
Histogram.prototype.bins = function(n){
this.maxBins = n;
return this;
};
/**
* Return the smallest value in the datasets.
*
* @return {Number}
* @api private
*/
Histogram.prototype.min = function(){
return min(this.datasets, function(set){
return min(set.data);
});
};
/**
* Return the largest value in the datasets.
*
* @return {Number}
* @api private
*/
Histogram.prototype.max = function(){
return max(this.datasets, function(set){
return max(set.data);
});
};
/**
* Distribute the data into bins.
*
* @api private
*/
Histogram.prototype.distribute = function(min, max){
for (var i = 0; i < this.datasets.length; i++) {
var o = this.datasets[i];
o.data = bin(o.data, this.maxBins, {
min: min,
max: max
});
}
};
/**
* Render the histogram and return a canvas.
*
* @return {Canvas}
* @api public
*/
Histogram.prototype.render = function(){
var self = this;
var svg = this.svg;
var w = this.width;
var h = this.height;
var maxBins = this.maxBins;
var sets = this.datasets;
// bin
var min = this.min();
var max = this.max();
this.distribute(min, max);
// render
var sx = (w / maxBins | 0) + 1;
var n = 0;
var x = 0;
while (x < w) {
// ticks
svg('rect')
.size(this.barWidth, h)
.move(x)
.attr('class', 'tick')
.attr('fill', this.barColor)
.attr('opacity', .8)
// bins
sets.forEach(function(set){
var val = set.data[n];
var bh = h * (val / max);
if (!bh) return;
svg('rect')
.size(self.barWidth, bh)
.move(x, h - bh)
.attr('class', 'bar')
.attr('fill', set.options.color);
});
x += sx;
++n;
}
return this.el;
};