diff --git a/src/traces/violin/calc.js b/src/traces/violin/calc.js index 163f50a20ea..e4852c02b8d 100644 --- a/src/traces/violin/calc.js +++ b/src/traces/violin/calc.js @@ -83,6 +83,9 @@ function silvermanRule(len, ssd, iqr) { function calcBandwidth(trace, cdi, vals) { var span = cdi.max - cdi.min; + // plot single-value violin with bandwidth of 1 + if(!span) return 1; + // Limit how small the bandwidth can be. // // Silverman's rule of thumb can be "very" small diff --git a/test/jasmine/tests/violin_test.js b/test/jasmine/tests/violin_test.js index e4b588d6693..164c37ad4c3 100644 --- a/test/jasmine/tests/violin_test.js +++ b/test/jasmine/tests/violin_test.js @@ -239,6 +239,25 @@ describe('Test violin calc:', function() { expect(fullLayout._violinScaleGroupStats.one.maxWidth).toBeCloseTo(0.055); expect(fullLayout._violinScaleGroupStats.one.maxCount).toBe(8); }); + + it('handle multi-box / single-value case', function() { + _calc({ + x: [1, 2, 3, 4, 5, 6], + y: [1, 2, 3, 4, 5, 6] + }); + + expect(cd.length).toBe(6, '# of violins'); + expect(cd.every(function(d) { return d.bandwidth; })).toBe(true, 'bandwidth'); + }); + + it('handle multi-value / single-but-unique-value case', function() { + _calc({ + y: [1, 1, 1, 1, 1] + }); + + expect(cd.length).toBe(1, '# of violins'); + expect(cd[0].bandwidth).toBe(1, 'bandwidth'); + }); }); describe('Test violin hover:', function() {