-
Notifications
You must be signed in to change notification settings - Fork 941
/
Rickshaw.Graph.Legend.js
123 lines (98 loc) · 2.72 KB
/
Rickshaw.Graph.Legend.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
var d3 = require("d3");
var Rickshaw;
exports.setUp = function(callback) {
Rickshaw = require('../rickshaw');
global.document = require("jsdom").jsdom("<html><head></head><body></body></html>");
global.window = document.defaultView;
new Rickshaw.Compat.ClassList();
var el = document.createElement("div");
this.graph = new Rickshaw.Graph({
element: el,
width: 960,
height: 500,
renderer: 'stack',
series: [
{
name: 'foo',
color: 'green',
stroke: 'red',
data: [
{ x: 4, y: 32 }
]
},
{
name: 'bar',
data: [
{ x: 4, y: 32 }
]
}
]
});
this.legendEl = document.createElement("div");
callback();
};
exports.tearDown = function(callback) {
delete require.cache.d3;
callback();
};
exports.rendersLegend = function(test) {
var legend = new Rickshaw.Graph.Legend({
graph: this.graph,
element: this.legendEl
});
var items = this.legendEl.getElementsByTagName('li')
test.equal(items.length, 2, "legend count")
test.equal(items[1].getElementsByClassName('label')[0].innerHTML, "foo")
test.equal(items[0].getElementsByClassName('label')[0].innerHTML, "bar")
test.done();
};
exports.hasDefaultClassName = function(test) {
var legend = new Rickshaw.Graph.Legend({
graph: this.graph,
element: this.legendEl
});
test.equal(this.legendEl.className, "rickshaw_legend")
test.done();
};
exports.canOverrideClassName = function(test) {
var MyLegend = Rickshaw.Class.create( Rickshaw.Graph.Legend, {
className: 'fnord'
});
var legend = new MyLegend({
graph: this.graph,
element: this.legendEl
});
test.equal(this.legendEl.className, "fnord")
test.done();
};
exports.hasDefaultColorKey = function(test) {
var legend = new Rickshaw.Graph.Legend({
graph: this.graph,
element: this.legendEl
});
test.equal(legend.colorKey, "color");
test.equal(this.legendEl.getElementsByClassName('swatch')[1].style.backgroundColor, "green");
test.done();
};
exports.canOverrideColorKey = function(test) {
var legend = new Rickshaw.Graph.Legend({
graph: this.graph,
element: this.legendEl,
colorKey: 'stroke'
});
test.equal(legend.colorKey, "stroke");
test.equal(this.legendEl.getElementsByClassName('swatch')[1].style.backgroundColor, "red");
test.done();
};
exports['should put series classes on legend elements'] = function(test) {
this.graph.series[0].className = 'fnord-series-0';
this.graph.series[1].className = 'fnord-series-1';
var legend = new Rickshaw.Graph.Legend({
graph: this.graph,
element: this.legendEl
});
test.equal(d3.select(this.legendEl).selectAll('.line').size(), 2);
test.equal(d3.select(this.legendEl).selectAll('.fnord-series-0').size(), 1);
test.equal(d3.select(this.legendEl).selectAll('.fnord-series-1').size(), 1);
test.done();
};