-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathannotations_test.js
205 lines (160 loc) · 6.56 KB
/
annotations_test.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var Annotations = require('@src/components/annotations');
var Plotly = require('@lib/index');
var Plots = require('@src/plots/plots');
var Lib = require('@src/lib');
var Dates = require('@src/lib/dates');
var d3 = require('d3');
var customMatchers = require('../assets/custom_matchers');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
describe('Test annotations', function() {
'use strict';
describe('supplyLayoutDefaults', function() {
it('should default to pixel for axref/ayref', function() {
var annotationDefaults = {};
annotationDefaults._has = Plots._hasPlotType.bind(annotationDefaults);
Annotations.supplyLayoutDefaults({ annotations: [{ showarrow: true, arrowhead: 2}] }, annotationDefaults);
expect(annotationDefaults.annotations[0].axref).toEqual('pixel');
expect(annotationDefaults.annotations[0].ayref).toEqual('pixel');
});
it('should convert ax/ay date coordinates to milliseconds if tail is in axis terms and axis is a date', function() {
var annotationOut = { xaxis: { type: 'date', range: ['2000-01-01', '2016-01-01'] }};
annotationOut._has = Plots._hasPlotType.bind(annotationOut);
var annotationIn = {
annotations: [{ showarrow: true, axref: 'x', ayref: 'y', x: '2008-07-01', ax: '2004-07-01', y: 0, ay: 50}]
};
Annotations.supplyLayoutDefaults(annotationIn, annotationOut);
expect(annotationIn.annotations[0].ax).toEqual(Dates.dateTime2ms('2004-07-01'));
});
});
});
describe('annotations relayout', function() {
'use strict';
var mock = require('@mocks/annotations.json');
var gd;
// there is 1 visible: false item
var len = mock.layout.annotations.length - 1;
beforeEach(function(done) {
gd = createGraphDiv();
var mockData = Lib.extendDeep([], mock.data),
mockLayout = Lib.extendDeep({}, mock.layout);
Plotly.plot(gd, mockData, mockLayout).then(done);
});
afterEach(destroyGraphDiv);
function countAnnotations() {
return d3.selectAll('g.annotation').size();
}
it('should be able to add /remove annotations', function(done) {
expect(countAnnotations()).toEqual(len);
var ann = { text: '' };
Plotly.relayout(gd, 'annotations[' + len + ']', ann).then(function() {
expect(countAnnotations()).toEqual(len + 1);
return Plotly.relayout(gd, 'annotations[0]', 'remove');
})
.then(function() {
expect(countAnnotations()).toEqual(len);
return Plotly.relayout(gd, 'annotations[0]', null);
})
.then(function() {
expect(countAnnotations()).toEqual(len - 1);
return Plotly.relayout(gd, 'annotations[0].visible', false);
})
.then(function() {
expect(countAnnotations()).toEqual(len - 2);
return Plotly.relayout(gd, { annotations: [] });
})
.then(function() {
expect(countAnnotations()).toEqual(0);
done();
});
});
it('should be able update annotations', function(done) {
function assertText(index, expected) {
var query = '.annotation[data-index="' + index + '"]',
actual = d3.select(query).select('text').text();
expect(actual).toEqual(expected);
}
assertText(0, 'left top');
Plotly.relayout(gd, 'annotations[0].text', 'hello').then(function() {
assertText(0, 'hello');
return Plotly.relayout(gd, 'annotations[0].text', null);
})
.then(function() {
assertText(0, 'new text');
})
.then(done);
});
});
describe('annotations autosize', function() {
'use strict';
var mock = Lib.extendDeep({}, require('@mocks/annotations-autorange.json'));
var gd;
beforeAll(function() {
jasmine.addMatchers(customMatchers);
});
afterEach(destroyGraphDiv);
it('should adapt to relayout calls', function(done) {
gd = createGraphDiv();
function assertRanges(x, y, x2, y2, x3, y3) {
var fullLayout = gd._fullLayout;
var PREC = 1;
// xaxis2 need a bit more tolerance to pass on CI
// this most likely due to the different text bounding box values
// on headfull vs headless browsers.
var PREC2 = 0.1;
expect(fullLayout.xaxis.range).toBeCloseToArray(x, PREC, '- xaxis');
expect(fullLayout.yaxis.range).toBeCloseToArray(y, PREC, '- yaxis');
expect(fullLayout.xaxis2.range).toBeCloseToArray(x2, PREC2, 'xaxis2');
expect(fullLayout.yaxis2.range).toBeCloseToArray(y2, PREC, 'yaxis2');
expect(fullLayout.xaxis3.range).toBeCloseToArray(x3, PREC, 'xaxis3');
expect(fullLayout.yaxis3.range).toBeCloseToArray(y3, PREC, 'yaxis3');
}
Plotly.plot(gd, mock).then(function() {
assertRanges(
[0.97, 2.03], [0.97, 2.03],
[-0.32, 3.38], [0.42, 2.58],
[0.9, 2.1], [0.86, 2.14]
);
return Plotly.relayout(gd, {
'annotations[0].visible': false,
'annotations[4].visible': false,
'annotations[8].visible': false
});
})
.then(function() {
assertRanges(
[1.44, 2.02], [0.97, 2.03],
[1.31, 2.41], [0.42, 2.58],
[1.44, 2.1], [0.86, 2.14]
);
return Plotly.relayout(gd, {
'annotations[2].visible': false,
'annotations[5].visible': false,
'annotations[9].visible': false
});
})
.then(function() {
assertRanges(
[1.44, 2.02], [0.99, 1.52],
[0.5, 2.5], [0.42, 2.58],
[0.5, 2.5], [0.86, 2.14]
);
return Plotly.relayout(gd, {
'annotations[0].visible': true,
'annotations[2].visible': true,
'annotations[4].visible': true,
'annotations[5].visible': true,
'annotations[8].visible': true,
'annotations[9].visible': true
});
})
.then(function() {
assertRanges(
[0.97, 2.03], [0.97, 2.03],
[-0.32, 3.38], [0.42, 2.58],
[0.9, 2.1], [0.86, 2.14]
);
})
.then(done);
});
});