Debian Codegraph @

bqplot/js/src/test/bars.ts

raw
1 import { expect } from 'chai';
2
3 import * as d3Timer from 'd3-timer';
4
5 import { DummyManager } from './dummy-manager';
6
7 import {
8 create_figure_bars,
9 getFills,
10 getStrokes,
11 getStrokeWidth,
12 } from './widget-utils';
13
14 import * as bqplot from '..';
15
16 describe('bars >', () => {
17 beforeEach(async function () {
18 this.manager = new DummyManager({ bqplot: bqplot });
19 });
20
21 it('create 1d', async function () {
22 const x = {
23 dtype: 'float32',
24 value: new DataView(new Float32Array([0, 1]).buffer),
25 };
26 const y = {
27 dtype: 'float32',
28 value: new DataView(new Float32Array([2, 3]).buffer),
29 };
30 const objects = await create_figure_bars(this.manager, x, y);
31 const bars = objects.bars;
32 const data = bars.d3el.selectAll('.bargroup').data();
33 expect(data[0].values[0].x).to.equal(0);
34 expect(data[1].values[0].x).to.equal(1);
35 expect(data[0].values[0].y).to.equal(2);
36 expect(data[1].values[0].y).to.equal(3);
37
38 d3Timer.timerFlush(); // this makes sure the animations are all executed
39 const heights = bars.d3el
40 .selectAll('.bargroup rect.bar')
41 .nodes()
42 .map((el) => Number(el.getAttribute('height')));
43 const height = objects.figure.plotarea_height;
44 expect(heights).to.deep.equal([(height * 2) / 3, height]);
45 });
46
47 it('create 2d classic', async function () {
48 const x = [0, 0.5, 1];
49 const y = [
50 [2, 2.0, 3],
51 [2.0, 2.5, 3.5],
52 ];
53 const objects = await create_figure_bars(this.manager, x, y);
54 const bars = objects.bars;
55 const data = bars.d3el.selectAll('.bargroup').data();
56 expect(data).to.have.lengthOf(3);
57 expect(data[0].values[0].x).to.equal(0);
58 expect(data[0].values[0].y).to.equal(2);
59 expect(data[0].values[1].x).to.equal(0);
60 expect(data[0].values[1].y).to.equal(2);
61 expect(data[0].values).to.have.lengthOf(2);
62
63 expect(data[1].values[0].x).to.equal(0.5);
64 expect(data[1].values[0].y).to.equal(2);
65 expect(data[1].values[1].x).to.equal(0.5);
66 expect(data[1].values[1].y).to.equal(2.5);
67 expect(data[1].values).to.have.lengthOf(2);
68
69 expect(data[2].values[0].x).to.equal(1);
70 expect(data[2].values[0].y).to.equal(3);
71 expect(data[2].values[1].x).to.equal(1);
72 expect(data[2].values[1].y).to.equal(3.5);
73 expect(data[2].values).to.have.lengthOf(2);
74
75 d3Timer.timerFlush(); // this makes sure the animations are all executed
76 const heights = bars.d3el
77 .selectAll('.bargroup rect.bar')
78 .nodes()
79 .map((el) => Math.round(Number(el.getAttribute('height'))));
80 const height = objects.figure.plotarea_height;
81 const h0 = Math.round((height * 2) / 3);
82 const h2 = Math.round(height);
83 const h3 = Math.round((height * 2.5) / 3);
84 const h4 = Math.round((height * 3.5) / 3);
85 expect(heights).to.deep.equal([h0, h0, h0, h3, h2, h4]);
86 });
87
88 it('create 2d binary', async function () {
89 const x = {
90 dtype: 'float32',
91 value: new DataView(new Float32Array([0, 0.5, 1]).buffer),
92 shape: [3],
93 };
94 const y = {
95 dtype: 'float32',
96 value: new DataView(new Float32Array([2, 2.0, 3, 2.0, 2.5, 3.5]).buffer),
97 shape: [2, 3],
98 };
99 const objects = await create_figure_bars(this.manager, x, y);
100 const bars = objects.bars;
101 const data = bars.d3el.selectAll('.bargroup').data();
102 expect(data).to.have.lengthOf(3);
103 expect(data[0].values[0].x).to.equal(0);
104 expect(data[0].values[0].y).to.equal(2);
105 expect(data[0].values[1].x).to.equal(0);
106 expect(data[0].values[1].y).to.equal(2);
107 expect(data[0].values).to.have.lengthOf(2);
108
109 expect(data[1].values[0].x).to.equal(0.5);
110 expect(data[1].values[0].y).to.equal(2);
111 expect(data[1].values[1].x).to.equal(0.5);
112 expect(data[1].values[1].y).to.equal(2.5);
113 expect(data[1].values).to.have.lengthOf(2);
114
115 expect(data[2].values[0].x).to.equal(1);
116 expect(data[2].values[0].y).to.equal(3);
117 expect(data[2].values[1].x).to.equal(1);
118 expect(data[2].values[1].y).to.equal(3.5);
119 expect(data[2].values).to.have.lengthOf(2);
120
121 d3Timer.timerFlush(); // this makes sure the animations are all executed
122 const heights = bars.d3el
123 .selectAll('.bargroup rect.bar')
124 .nodes()
125 .map((el) => Math.round(Number(el.getAttribute('height'))));
126 const height = objects.figure.plotarea_height;
127 const h0 = Math.round((height * 2) / 3);
128 const h2 = Math.round(height);
129 const h3 = Math.round((height * 2.5) / 3);
130 const h4 = Math.round((height * 3.5) / 3);
131 expect(heights).to.deep.equal([h0, h0, h0, h3, h2, h4]);
132 });
133
134 it('selected', async function () {
135 const x = [0, 1, 2, 3, 5, 6];
136 const y = [2, 3, 4, 5, 1, 2];
137 const objects = await create_figure_bars(this.manager, x, y);
138
139 const bars = objects.bars;
140 const rects = bars.d3el.selectAll('.bargroup').selectAll('.bar');
141
142 bars.model.set('selected_style', { fill: 'orange' });
143 bars.model.set('unselected_style', { fill: 'red' });
144
145 // Default color, no selection
146 expect(getFills(rects)).to.eql([
147 'steelblue',
148 'steelblue',
149 'steelblue',
150 'steelblue',
151 'steelblue',
152 'steelblue',
153 ]);
154
155 // Simulate selection from back-end
156 bars.model.set('selected', [0, 3]);
157 expect(getFills(rects)).to.eql([
158 'orange',
159 'red',
160 'red',
161 'orange',
162 'red',
163 'red',
164 ]);
165
166 bars.model.set('selected', [4, 5]);
167 expect(getFills(rects)).to.eql([
168 'red',
169 'red',
170 'red',
171 'red',
172 'orange',
173 'orange',
174 ]);
175
176 bars.model.set('selected_style', { fill: 'green' });
177 expect(getFills(rects)).to.eql([
178 'red',
179 'red',
180 'red',
181 'red',
182 'green',
183 'green',
184 ]);
185
186 bars.model.set('unselected_style', { fill: 'black' });
187 expect(getFills(rects)).to.eql([
188 'black',
189 'black',
190 'black',
191 'black',
192 'green',
193 'green',
194 ]);
195
196 bars.model.set('selected_style', {});
197 expect(getFills(rects)).to.eql([
198 'black',
199 'black',
200 'black',
201 'black',
202 'steelblue',
203 'steelblue',
204 ]);
205
206 bars.model.set('selected', null);
207 expect(getFills(rects)).to.eql([
208 'steelblue',
209 'steelblue',
210 'steelblue',
211 'steelblue',
212 'steelblue',
213 'steelblue',
214 ]);
215 });
216
217 it('styles', async function () {
218 const x = [0, 1, 2, 3, 5, 6];
219 const y = [2, 3, 4, 5, 1, 2];
220 const objects = await create_figure_bars(this.manager, x, y);
221
222 const bars = objects.bars;
223 const rects = bars.d3el.selectAll('.bargroup').selectAll('.bar');
224
225 bars.model.set('fill', false);
226
227 // No fill
228 expect(getFills(rects)).to.eql([
229 'none',
230 'none',
231 'none',
232 'none',
233 'none',
234 'none',
235 ]);
236 expect(getStrokes(rects)).to.eql([
237 'none',
238 'none',
239 'none',
240 'none',
241 'none',
242 'none',
243 ]);
244 expect(getStrokeWidth(rects)).to.eql(['1', '1', '1', '1', '1', '1']);
245
246 bars.model.set('stroke', 'red');
247 expect(getStrokes(rects)).to.eql([
248 'red',
249 'red',
250 'red',
251 'red',
252 'red',
253 'red',
254 ]);
255 expect(getStrokeWidth(rects)).to.eql(['1', '1', '1', '1', '1', '1']);
256
257 bars.model.set('stroke_width', 3);
258 expect(getStrokes(rects)).to.eql([
259 'red',
260 'red',
261 'red',
262 'red',
263 'red',
264 'red',
265 ]);
266 expect(getStrokeWidth(rects)).to.eql(['3', '3', '3', '3', '3', '3']);
267 });
268 });