Debian Codegraph @

bqplot/js/src/test/hist.ts

raw
1 // import * as d3Timer from 'd3-timer';
2
3 import { expect } from 'chai';
4
5 import { DummyManager } from './dummy-manager';
6
7 import { create_figure_hist, getFills } from './widget-utils';
8
9 import * as bqplot from '..';
10
11 describe('hist >', () => {
12 beforeEach(async function () {
13 this.manager = new DummyManager({ bqplot: bqplot });
14 });
15
16 it('colors', async function () {
17 const sample = {
18 dtype: 'float32',
19 value: new DataView(new Float32Array(new Array(100).keys()).buffer),
20 };
21 const objects = await create_figure_hist(this.manager, sample, 3);
22
23 const hist = objects.hist;
24 let rects = hist.d3el.selectAll('.bargroup').selectAll('.rect');
25
26 // Default color
27 let colors = getFills(rects);
28 expect(colors[0]).to.equal('steelblue');
29 expect(colors[1]).to.equal('steelblue');
30 expect(colors[2]).to.equal('steelblue');
31
32 // Update colors
33 hist.model.set('colors', ['steelblue', 'red', 'green']);
34 colors = getFills(rects);
35 expect(colors[0]).to.equal('steelblue');
36 expect(colors[1]).to.equal('red');
37 expect(colors[2]).to.equal('green');
38
39 // Colors should cycle
40 hist.model.set('bins', 6);
41 rects = hist.d3el.selectAll('.bargroup').selectAll('.rect');
42 colors = getFills(rects);
43 expect(colors[0]).to.equal('steelblue');
44 expect(colors[1]).to.equal('red');
45 expect(colors[2]).to.equal('green');
46 expect(colors[3]).to.equal('steelblue');
47 expect(colors[4]).to.equal('red');
48 expect(colors[5]).to.equal('green');
49 });
50
51 it('selected', async function () {
52 const sample = {
53 dtype: 'float32',
54 value: new DataView(new Float32Array(new Array(100).keys()).buffer),
55 };
56 const objects = await create_figure_hist(this.manager, sample, 6);
57
58 const hist = objects.hist;
59 const rects = hist.d3el.selectAll('.bargroup').selectAll('.rect');
60
61 hist.model.set('selected_style', { fill: 'orange' });
62 hist.model.set('unselected_style', { fill: 'red' });
63
64 // Default color, no selection
65 let colors = getFills(rects);
66 expect(colors[0]).to.equal('steelblue');
67 expect(colors[1]).to.equal('steelblue');
68 expect(colors[2]).to.equal('steelblue');
69 expect(colors[3]).to.equal('steelblue');
70 expect(colors[4]).to.equal('steelblue');
71 expect(colors[5]).to.equal('steelblue');
72
73 // Simulate selection of the first bar from back-end
74 hist.model.set('selected', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
75 colors = getFills(rects);
76 expect(colors[0]).to.equal('orange');
77 expect(colors[1]).to.equal('red');
78 expect(colors[2]).to.equal('red');
79 expect(colors[3]).to.equal('red');
80 expect(colors[4]).to.equal('red');
81 expect(colors[5]).to.equal('red');
82
83 // Simulate selection of both the first and the second bar from back-end
84 // (there is no current way of visually showing the selection of a subset of the data in a bin)
85 hist.model.set('selected', [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
86 colors = getFills(rects);
87 expect(colors[0]).to.equal('orange');
88 expect(colors[1]).to.equal('orange');
89 expect(colors[2]).to.equal('red');
90 expect(colors[3]).to.equal('red');
91 expect(colors[4]).to.equal('red');
92 expect(colors[5]).to.equal('red');
93 });
94 });