Debian Codegraph @

bqplot/js/src/test/lines.ts

raw
1 import { expect } from 'chai';
2 import { DummyManager } from './dummy-manager';
3 import bqplot = require('..');
4 import { create_model_bqplot, create_figure_lines } from './widget-utils';
5 import * as d3Timer from 'd3-timer';
6
7 describe('lines >', () => {
8 beforeEach(async function () {
9 this.manager = new DummyManager({ bqplot: bqplot });
10 });
11
12 it('model mark set methods', async function () {
13 const x = {
14 dtype: 'float32',
15 value: new DataView(new Float32Array([0, 1]).buffer),
16 };
17 const y = {
18 dtype: 'float32',
19 value: new DataView(new Float32Array([2, 3]).buffer),
20 };
21 const objects = await create_figure_lines(this.manager, x, y);
22 const lines = objects.lines;
23 lines.model.set('marker_size', 7);
24 lines.model.set('marker', 'circle');
25 lines.model.set('stroke_width', 3);
26 lines.model.set('labels_visibility', 'label');
27 lines.model.set('curves_subset', [2, 4, 5]);
28 lines.model.set('line_style', 'solid');
29 });
30
31 it('create 1d ordinal', async function () {
32 const x = ['a', 'b'];
33 const scale_x = await create_model_bqplot(
34 this.manager,
35 'OrdinalScale',
36 'scale_x_ordinal',
37 { allow_padding: false, domain: [] }
38 );
39 const y = {
40 dtype: 'float32',
41 value: new DataView(new Float32Array([2, 3]).buffer),
42 };
43 const objects = await create_figure_lines(this.manager, x, y, {
44 x: scale_x.toJSON(),
45 });
46 const lines = objects.lines;
47 const data = lines.d3el.selectAll('.curve').data();
48 expect(data[0].values[0].x).to.equal('a');
49 expect(data[0].values[1].x).to.equal('b');
50 expect(data[0].values[0].y).to.equal(2);
51 expect(data[0].values[1].y).to.equal(3);
52
53 lines.update_line_xy();
54 d3Timer.timerFlush(); // this makes sure the animations are all executed
55 const paths = lines.d3el
56 .selectAll('.curve path.line')
57 .nodes()
58 .map((el) => el.getAttribute('d'));
59 const width = objects.figure.plotarea_width;
60 const height = objects.figure.plotarea_height;
61 expect(paths).to.deep.equal([
62 `M${(width * 1) / 4},${height}L${(width * 3) / 4},0`,
63 ]);
64 });
65
66 it('create 1d', async function () {
67 const x = {
68 dtype: 'float32',
69 value: new DataView(new Float32Array([0, 1]).buffer),
70 };
71 const y = {
72 dtype: 'float32',
73 value: new DataView(new Float32Array([2, 3]).buffer),
74 };
75 const objects = await create_figure_lines(this.manager, x, y);
76 const lines = objects.lines;
77 const data = lines.d3el.selectAll('.curve').data();
78 expect(data[0].values[0].x).to.equal(0);
79 expect(data[0].values[1].x).to.equal(1);
80 expect(data[0].values[0].y).to.equal(2);
81 expect(data[0].values[1].y).to.equal(3);
82
83 lines.update_line_xy();
84 d3Timer.timerFlush(); // this makes sure the animations are all executed
85 const paths = lines.d3el
86 .selectAll('.curve path.line')
87 .nodes()
88 .map((el) => el.getAttribute('d'));
89 const width = objects.figure.plotarea_width;
90 const height = objects.figure.plotarea_height;
91 expect(paths).to.deep.equal([`M0,${height}L${width},0`]);
92 });
93
94 it('create 2d classic', async function () {
95 const x = [
96 [0, 0.5, 1],
97 [0.5, 0.5, 1],
98 ];
99 const y = [
100 [2, 2.0, 3],
101 [2.0, 2.5, 3],
102 ];
103 const objects = await create_figure_lines(this.manager, x, y);
104 const lines = objects.lines;
105 const data = lines.d3el.selectAll('.curve').data();
106 expect(data[0].values[0].x).to.equal(0);
107 expect(data[0].values[1].x).to.equal(0.5);
108 expect(data[0].values[2].x).to.equal(1);
109 expect(data[0].values[0].y).to.equal(2);
110 expect(data[0].values[1].y).to.equal(2);
111 expect(data[0].values[2].y).to.equal(3);
112 expect(data[0].values).to.have.lengthOf(3);
113
114 expect(data[1].values[0].x).to.equal(0.5);
115 expect(data[1].values[1].x).to.equal(0.5);
116 expect(data[1].values[2].x).to.equal(1);
117 expect(data[1].values[0].y).to.equal(2.0);
118 expect(data[1].values[1].y).to.equal(2.5);
119 expect(data[1].values[2].y).to.equal(3);
120 expect(data[1].values).to.have.lengthOf(3);
121
122 lines.update_line_xy();
123 d3Timer.timerFlush(); // this makes sure the animations are all executed
124 const paths = lines.d3el
125 .selectAll('.curve path.line')
126 .nodes()
127 .map((el) => el.getAttribute('d'));
128 const width = objects.figure.plotarea_width;
129 const height = objects.figure.plotarea_height;
130 expect(paths).to.deep.equal([
131 `M0,${height}L${width / 2},${height}L${width},0`,
132 `M${width / 2},${height}L${width / 2},${height / 2}L${width},0`,
133 ]);
134 });
135
136 it('create 2d binary', async function () {
137 const x = {
138 dtype: 'float32',
139 value: new DataView(
140 new Float32Array([0.0, 0.5, 1.0, 0.5, 0.5, 1.0]).buffer
141 ),
142 shape: [2, 3],
143 };
144 const y = {
145 dtype: 'float32',
146 value: new DataView(
147 new Float32Array([2.0, 2.0, 3.0, 2.0, 2.5, 3.0]).buffer
148 ),
149 shape: [2, 3],
150 };
151 const objects = await create_figure_lines(this.manager, x, y);
152 const lines = objects.lines;
153 const data = lines.d3el.selectAll('.curve').data();
154 expect(data[0].values[0].x).to.equal(0);
155 expect(data[0].values[1].x).to.equal(0.5);
156 expect(data[0].values[2].x).to.equal(1);
157 expect(data[0].values[0].y).to.equal(2);
158 expect(data[0].values[1].y).to.equal(2);
159 expect(data[0].values[2].y).to.equal(3);
160 expect(data[0].values).to.have.lengthOf(3);
161
162 expect(data[1].values[0].x).to.equal(0.5);
163 expect(data[1].values[1].x).to.equal(0.5);
164 expect(data[1].values[2].x).to.equal(1);
165 expect(data[1].values[0].y).to.equal(2.0);
166 expect(data[1].values[1].y).to.equal(2.5);
167 expect(data[1].values[2].y).to.equal(3);
168 expect(data[1].values).to.have.lengthOf(3);
169
170 lines.update_line_xy();
171 d3Timer.timerFlush(); // this makes sure the animations are all executed
172 const paths = lines.d3el
173 .selectAll('.curve path.line')
174 .nodes()
175 .map((el) => el.getAttribute('d'));
176 const width = objects.figure.plotarea_width;
177 const height = objects.figure.plotarea_height;
178 expect(paths).to.deep.equal([
179 `M0,${height}L${width / 2},${height}L${width},0`,
180 `M${width / 2},${height}L${width / 2},${height / 2}L${width},0`,
181 ]);
182 });
183
184 it('create 2d binary shared x', async function () {
185 const x = {
186 dtype: 'float32',
187 value: new DataView(new Float32Array([0.0, 0.5, 1.0]).buffer),
188 shape: [3],
189 };
190 const y = {
191 dtype: 'float32',
192 value: new DataView(
193 new Float32Array([2.0, 2.0, 3.0, 2.0, 2.5, 3.0]).buffer
194 ),
195 shape: [2, 3],
196 };
197 const objects = await create_figure_lines(this.manager, x, y);
198 const lines = objects.lines;
199 const data = lines.d3el.selectAll('.curve').data();
200 expect(data[0].values[0].x).to.equal(0);
201 expect(data[0].values[1].x).to.equal(0.5);
202 expect(data[0].values[2].x).to.equal(1);
203 expect(data[0].values[0].y).to.equal(2);
204 expect(data[0].values[1].y).to.equal(2);
205 expect(data[0].values[2].y).to.equal(3);
206 expect(data[0].values).to.have.lengthOf(3);
207
208 expect(data[1].values[0].x).to.equal(0.0);
209 expect(data[1].values[1].x).to.equal(0.5);
210 expect(data[1].values[2].x).to.equal(1);
211 expect(data[1].values[0].y).to.equal(2.0);
212 expect(data[1].values[1].y).to.equal(2.5);
213 expect(data[1].values[2].y).to.equal(3);
214 expect(data[1].values).to.have.lengthOf(3);
215
216 lines.update_line_xy();
217 d3Timer.timerFlush(); // this makes sure the animations are all executed
218 const paths = lines.d3el
219 .selectAll('.curve path.line')
220 .nodes()
221 .map((el) => el.getAttribute('d'));
222 const width = objects.figure.plotarea_width;
223 const height = objects.figure.plotarea_height;
224
225 expect(paths).to.deep.equal([
226 `M0,${height}L${width / 2},${height}L${width},0`,
227 `M0,${height}L${width / 2},${height / 2}L${width},0`,
228 ]);
229 });
230 });