|
1 require("../env"); |
|
2 require("../../d3"); |
|
3 require("../../d3.layout"); |
|
4 |
|
5 var vows = require("vows"), |
|
6 assert = require("assert"); |
|
7 |
|
8 var suite = vows.describe("d3.layout.histogram"); |
|
9 |
|
10 suite.addBatch({ |
|
11 "histogram": { |
|
12 topic: function() { |
|
13 return d3.layout.histogram; |
|
14 }, |
|
15 "defaults to frequencies": function(histogram) { |
|
16 var h = histogram(); |
|
17 assert.deepEqual(h([0,0,0,1,2,2]).map(elements), [[0, 0, 0], [], [1], [2, 2]]); |
|
18 }, |
|
19 "each bin contains the matching source elements": function(histogram) { |
|
20 var h = histogram(); |
|
21 assert.deepEqual(h([0,0,0,1,2,2]).map(elements), [[0, 0, 0], [], [1], [2, 2]]); |
|
22 }, |
|
23 "each bin also has defined x, y and dx properties": function(histogram) { |
|
24 var h = histogram(); |
|
25 assert.deepEqual(h([0,0,0,1,2,2]).map(metadata), [ |
|
26 {x: 0, y: 3, dx: 0.5}, |
|
27 {x: 0.5, y: 0, dx: 0.5}, |
|
28 {x: 1, y: 1, dx: 0.5}, |
|
29 {x: 1.5, y: 2, dx: 0.5} |
|
30 ]); |
|
31 }, |
|
32 "can output frequencies": function(histogram) { |
|
33 var h = histogram().frequency(true); |
|
34 assert.deepEqual(h([0,0,0,1,2,2]).map(metadata), [ |
|
35 {x: 0, y: 3, dx: 0.5}, |
|
36 {x: 0.5, y: 0, dx: 0.5}, |
|
37 {x: 1, y: 1, dx: 0.5}, |
|
38 {x: 1.5, y: 2, dx: 0.5} |
|
39 ]); |
|
40 }, |
|
41 "can output probabilities": function(histogram) { |
|
42 var h = histogram().frequency(false); |
|
43 assert.deepEqual(h([0,0,0,1,2,2]).map(metadata), [ |
|
44 {x: 0, y: 3/6, dx: 0.5}, |
|
45 {x: 0.5, y: 0, dx: 0.5}, |
|
46 {x: 1, y: 1/6, dx: 0.5}, |
|
47 {x: 1.5, y: 2/6, dx: 0.5} |
|
48 ]); |
|
49 }, |
|
50 "can specify number of bins": function(histogram) { |
|
51 var h = histogram().bins(2); |
|
52 assert.deepEqual(h([0,0,0,1,2,2]).map(elements), [ |
|
53 [0, 0, 0], |
|
54 [1, 2, 2] |
|
55 ]); |
|
56 assert.deepEqual(h([0,0,0,1,2,2]).map(metadata), [ |
|
57 {x: 0, y: 3, dx: 1}, |
|
58 {x: 1, y: 3, dx: 1} |
|
59 ]); |
|
60 }, |
|
61 "can specify bin thresholds": function(histogram) { |
|
62 var h = histogram().bins([0,1,2,3]); |
|
63 assert.deepEqual(h([0,0,0,1,2,2]).map(elements), [ |
|
64 [0, 0, 0], |
|
65 [1], |
|
66 [2, 2] |
|
67 ]); |
|
68 assert.deepEqual(h([0,0,0,1,2,2]).map(metadata), [ |
|
69 {x: 0, y: 3, dx: 1}, |
|
70 {x: 1, y: 1, dx: 1}, |
|
71 {x: 2, y: 2, dx: 1} |
|
72 ]); |
|
73 } |
|
74 } |
|
75 }); |
|
76 |
|
77 function elements(bin) { |
|
78 var array = [], i = -1, n = bin.length; |
|
79 while (++i < n) array.push(bin[i]); |
|
80 return array; |
|
81 } |
|
82 |
|
83 function metadata(bin) { |
|
84 var metadata = {}; |
|
85 if ("x" in bin) metadata.x = bin.x; |
|
86 if ("y" in bin) metadata.y = bin.y; |
|
87 if ("dx" in bin) metadata.dx = bin.dx; |
|
88 return metadata; |
|
89 } |
|
90 |
|
91 suite.export(module); |