| 1 |
import * as d3Timer from 'd3-timer'; |
| 2 |
import * as d3Color from 'd3-color'; |
| 3 |
|
| 4 |
import { expect } from 'chai'; |
| 5 |
|
| 6 |
import * as bqplot from '..'; |
| 7 |
|
| 8 |
import { DummyManager } from './dummy-manager'; |
| 9 |
|
| 10 |
import { create_figure_pie } from './widget-utils'; |
| 11 |
|
| 12 |
describe('pie >', () => { |
| 13 |
beforeEach(async function () { |
| 14 |
this.manager = new DummyManager({ bqplot: bqplot }); |
| 15 |
}); |
| 16 |
|
| 17 |
it('create', async function () { |
| 18 |
const sizes = { |
| 19 |
dtype: 'float32', |
| 20 |
value: new DataView(new Float32Array([0, 1, 4]).buffer), |
| 21 |
}; |
| 22 |
const objects = await create_figure_pie(this.manager, sizes, [ |
| 23 |
'foo', |
| 24 |
'bar', |
| 25 |
'baz', |
| 26 |
]); |
| 27 |
const pie = objects.pie; |
| 28 |
|
| 29 |
d3Timer.timerFlush(); |
| 30 |
|
| 31 |
const slices = pie.d3el.selectAll('.slice'); |
| 32 |
const labels = pie.d3el.selectAll('.labels').selectAll('text'); |
| 33 |
|
| 34 |
// Check slice values |
| 35 |
const data = slices.data(); |
| 36 |
expect(data[0].value).to.equal(0); |
| 37 |
expect(data[1].value).to.equal(1); |
| 38 |
expect(data[2].value).to.equal(4); |
| 39 |
|
| 40 |
// Check slice colors |
| 41 |
const sliceElts = slices.nodes(); |
| 42 |
expect(sliceElts[0].style.fill).to.equal(d3Color.rgb('#1f77b4').toString()); |
| 43 |
expect(sliceElts[1].style.fill).to.equal(d3Color.rgb('#ff7f0e').toString()); |
| 44 |
expect(sliceElts[2].style.fill).to.equal(d3Color.rgb('#2ca02c').toString()); |
| 45 |
|
| 46 |
// Check labels |
| 47 |
const labelElts = labels.nodes(); |
| 48 |
expect(labelElts[0].innerHTML).to.equal('foo'); |
| 49 |
expect(parseFloat(labelElts[0].style.opacity)).to.equal(0); |
| 50 |
expect(labelElts[1].innerHTML).to.equal('bar'); |
| 51 |
expect(parseFloat(labelElts[1].style.opacity)).to.equal(1); |
| 52 |
expect(labelElts[2].innerHTML).to.equal('baz'); |
| 53 |
expect(parseFloat(labelElts[2].style.opacity)).to.equal(1); |
| 54 |
}); |
| 55 |
|
| 56 |
it('update colors', async function () { |
| 57 |
const sizes = { |
| 58 |
dtype: 'float32', |
| 59 |
value: new DataView(new Float32Array([1, 3, 4, 5]).buffer), |
| 60 |
}; |
| 61 |
const objects = await create_figure_pie(this.manager, sizes, [ |
| 62 |
'foo', |
| 63 |
'bar', |
| 64 |
'baz', |
| 65 |
'Hello World', |
| 66 |
]); |
| 67 |
const pie = objects.pie; |
| 68 |
|
| 69 |
d3Timer.timerFlush(); |
| 70 |
|
| 71 |
const slices = pie.d3el.selectAll('.slice'); |
| 72 |
|
| 73 |
// Check slice colors |
| 74 |
const sliceElts = slices.nodes(); |
| 75 |
expect(sliceElts[0].style.fill).to.equal(d3Color.rgb('#1f77b4').toString()); |
| 76 |
expect(sliceElts[1].style.fill).to.equal(d3Color.rgb('#ff7f0e').toString()); |
| 77 |
expect(sliceElts[2].style.fill).to.equal(d3Color.rgb('#2ca02c').toString()); |
| 78 |
expect(sliceElts[3].style.fill).to.equal(d3Color.rgb('#d62728').toString()); |
| 79 |
|
| 80 |
// Update slice colors |
| 81 |
pie.model.set('colors', ['red', 'blue', 'green', 'steelblue']); |
| 82 |
d3Timer.timerFlush(); |
| 83 |
|
| 84 |
expect(sliceElts[0].style.fill).to.equal('red'); |
| 85 |
expect(sliceElts[1].style.fill).to.equal('blue'); |
| 86 |
expect(sliceElts[2].style.fill).to.equal('green'); |
| 87 |
expect(sliceElts[3].style.fill).to.equal('steelblue'); |
| 88 |
|
| 89 |
// Update slice colors |
| 90 |
pie.model.set('colors', ['red', 'blue']); |
| 91 |
d3Timer.timerFlush(); |
| 92 |
|
| 93 |
expect(sliceElts[0].style.fill).to.equal('red'); |
| 94 |
expect(sliceElts[1].style.fill).to.equal('blue'); |
| 95 |
expect(sliceElts[2].style.fill).to.equal('red'); |
| 96 |
expect(sliceElts[3].style.fill).to.equal('blue'); |
| 97 |
}); |
| 98 |
|
| 99 |
it('update text', async function () { |
| 100 |
const sizes = { |
| 101 |
dtype: 'float32', |
| 102 |
value: new DataView(new Float32Array([1, 0, 4, 5]).buffer), |
| 103 |
}; |
| 104 |
const objects = await create_figure_pie(this.manager, sizes, [ |
| 105 |
'foo', |
| 106 |
'bar', |
| 107 |
'baz', |
| 108 |
'Hello World', |
| 109 |
]); |
| 110 |
const pie = objects.pie; |
| 111 |
|
| 112 |
d3Timer.timerFlush(); |
| 113 |
|
| 114 |
let labels = pie.d3el.selectAll('.labels').selectAll('text'); |
| 115 |
|
| 116 |
// Check labels |
| 117 |
let labelElts = labels.nodes(); |
| 118 |
expect(labelElts[0].innerHTML).to.equal('foo'); |
| 119 |
expect(parseFloat(labelElts[0].style.opacity)).to.equal(1); |
| 120 |
expect(labelElts[1].innerHTML).to.equal('bar'); |
| 121 |
expect(parseFloat(labelElts[1].style.opacity)).to.equal(0); |
| 122 |
expect(labelElts[2].innerHTML).to.equal('baz'); |
| 123 |
expect(parseFloat(labelElts[2].style.opacity)).to.equal(1); |
| 124 |
expect(labelElts[3].innerHTML).to.equal('Hello World'); |
| 125 |
expect(parseFloat(labelElts[3].style.opacity)).to.equal(1); |
| 126 |
|
| 127 |
// Update pie data |
| 128 |
pie.model.set('sizes', [1, 2, 0, 5, 6]); |
| 129 |
pie.model.set('labels', ['hey', 'there', 'how', 'are', 'you']); |
| 130 |
|
| 131 |
d3Timer.timerFlush(); |
| 132 |
|
| 133 |
labels = pie.d3el.selectAll('.labels').selectAll('text'); |
| 134 |
|
| 135 |
// Check labels |
| 136 |
labelElts = labels.nodes(); |
| 137 |
expect(labelElts[0].innerHTML).to.equal('hey'); |
| 138 |
expect(parseFloat(labelElts[0].style.opacity)).to.equal(1); |
| 139 |
expect(labelElts[1].innerHTML).to.equal('there'); |
| 140 |
expect(parseFloat(labelElts[1].style.opacity)).to.equal(1); |
| 141 |
expect(labelElts[2].innerHTML).to.equal('how'); |
| 142 |
expect(parseFloat(labelElts[2].style.opacity)).to.equal(0); |
| 143 |
expect(labelElts[3].innerHTML).to.equal('are'); |
| 144 |
expect(parseFloat(labelElts[3].style.opacity)).to.equal(1); |
| 145 |
expect(labelElts[4].innerHTML).to.equal('you'); |
| 146 |
expect(parseFloat(labelElts[4].style.opacity)).to.equal(1); |
| 147 |
}); |
| 148 |
|
| 149 |
it('update selected', async function () { |
| 150 |
const sizes = { |
| 151 |
dtype: 'float32', |
| 152 |
value: new DataView(new Float32Array([1, 0, 4, 5]).buffer), |
| 153 |
}; |
| 154 |
const objects = await create_figure_pie(this.manager, sizes, [ |
| 155 |
'foo', |
| 156 |
'bar', |
| 157 |
'baz', |
| 158 |
'Hello World', |
| 159 |
]); |
| 160 |
const pie = objects.pie; |
| 161 |
|
| 162 |
d3Timer.timerFlush(); |
| 163 |
|
| 164 |
pie.model.set('selected', new Float32Array([0, 1])); |
| 165 |
pie.model.set('selected_style', { fill: 'red', opacity: 1 }); |
| 166 |
|
| 167 |
d3Timer.timerFlush(); |
| 168 |
|
| 169 |
const slices = pie.pieSelection.selectAll('.slice'); |
| 170 |
|
| 171 |
// Check slice colors |
| 172 |
const sliceElts = slices.nodes(); |
| 173 |
expect(sliceElts[0].style.fill).to.equal('red'); |
| 174 |
expect(sliceElts[1].style.fill).to.equal('red'); |
| 175 |
expect(sliceElts[2].style.fill).to.equal(d3Color.rgb('#2ca02c').toString()); |
| 176 |
expect(sliceElts[3].style.fill).to.equal(d3Color.rgb('#d62728').toString()); |
| 177 |
|
| 178 |
pie.model.set('selected', new Float32Array([1, 2])); |
| 179 |
|
| 180 |
d3Timer.timerFlush(); |
| 181 |
|
| 182 |
expect(sliceElts[0].style.fill).to.equal(d3Color.rgb('#1f77b4').toString()); |
| 183 |
expect(sliceElts[1].style.fill).to.equal('red'); |
| 184 |
expect(sliceElts[2].style.fill).to.equal('red'); |
| 185 |
expect(sliceElts[3].style.fill).to.equal(d3Color.rgb('#d62728').toString()); |
| 186 |
|
| 187 |
pie.model.set('selected', new Float32Array([0, 1])); |
| 188 |
pie.model.set('selected_style', {}); |
| 189 |
pie.model.set('unselected_style', { fill: 'blue', opacity: 0.1 }); |
| 190 |
|
| 191 |
d3Timer.timerFlush(); |
| 192 |
|
| 193 |
// Check slice colors |
| 194 |
expect(sliceElts[0].style.fill).to.equal(d3Color.rgb('#1f77b4').toString()); |
| 195 |
expect(sliceElts[1].style.fill).to.equal(d3Color.rgb('#ff7f0e').toString()); |
| 196 |
expect(sliceElts[2].style.fill).to.equal('blue'); |
| 197 |
expect(parseFloat(sliceElts[2].style.opacity)).to.equal(0.1); |
| 198 |
expect(sliceElts[3].style.fill).to.equal('blue'); |
| 199 |
expect(parseFloat(sliceElts[3].style.opacity)).to.equal(0.1); |
| 200 |
|
| 201 |
pie.model.set('selected', new Float32Array([1, 2])); |
| 202 |
|
| 203 |
d3Timer.timerFlush(); |
| 204 |
|
| 205 |
expect(sliceElts[0].style.fill).to.equal('blue'); |
| 206 |
expect(parseFloat(sliceElts[0].style.opacity)).to.equal(0.1); |
| 207 |
expect(sliceElts[1].style.fill).to.equal(d3Color.rgb('#ff7f0e').toString()); |
| 208 |
expect(sliceElts[2].style.fill).to.equal(d3Color.rgb('#2ca02c').toString()); |
| 209 |
expect(sliceElts[3].style.fill).to.equal('blue'); |
| 210 |
expect(parseFloat(sliceElts[3].style.opacity)).to.equal(0.1); |
| 211 |
}); |
| 212 |
}); |