Skip to content

Commit 04ca7c6

Browse files
committed
fix bandwidth for single-value violins
1 parent f06032f commit 04ca7c6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/traces/violin/calc.js

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ function silvermanRule(len, ssd, iqr) {
8383
function calcBandwidth(trace, cdi, vals) {
8484
var span = cdi.max - cdi.min;
8585

86+
// plot single-value violin with bandwidth of 1
87+
if(!span) return 1;
88+
8689
// Limit how small the bandwidth can be.
8790
//
8891
// Silverman's rule of thumb can be "very" small

test/jasmine/tests/violin_test.js

+19
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,25 @@ describe('Test violin calc:', function() {
239239
expect(fullLayout._violinScaleGroupStats.one.maxWidth).toBeCloseTo(0.055);
240240
expect(fullLayout._violinScaleGroupStats.one.maxCount).toBe(8);
241241
});
242+
243+
it('handle multi-box / single-value case', function() {
244+
_calc({
245+
x: [1, 2, 3, 4, 5, 6],
246+
y: [1, 2, 3, 4, 5, 6]
247+
});
248+
249+
expect(cd.length).toBe(6, '# of violins');
250+
expect(cd.every(function(d) { return d.bandwidth; })).toBe(true, 'bandwidth');
251+
});
252+
253+
it('handle multi-value / single-but-unique-value case', function() {
254+
_calc({
255+
y: [1, 1, 1, 1, 1]
256+
});
257+
258+
expect(cd.length).toBe(1, '# of violins');
259+
expect(cd[0].bandwidth).toBe(1, 'bandwidth');
260+
});
242261
});
243262

244263
describe('Test violin hover:', function() {

0 commit comments

Comments
 (0)