@@ -37,24 +37,32 @@ module.exports = function calc(gd, trace) {
3737 var bandwidth = cdi . bandwidth = calcBandwidth ( trace , cdi , vals ) ;
3838 var span = cdi . span = calcSpan ( trace , cdi , valAxis , bandwidth ) ;
3939
40- // step that well covers the bandwidth and is multiple of span distance
41- var dist = span [ 1 ] - span [ 0 ] ;
42- var n = Math . ceil ( dist / ( bandwidth / 3 ) ) ;
43- var step = dist / n ;
44-
45- if ( ! isFinite ( step ) || ! isFinite ( n ) ) {
46- Lib . error ( 'Something went wrong with computing the violin span' ) ;
47- cd [ 0 ] . t . empty = true ;
48- return cd ;
49- }
50-
51- var kde = helpers . makeKDE ( cdi , trace , vals ) ;
52- cdi . density = new Array ( n ) ;
53-
54- for ( var k = 0 , t = span [ 0 ] ; t < ( span [ 1 ] + step / 2 ) ; k ++ , t += step ) {
55- var v = kde ( t ) ;
56- cdi . density [ k ] = { v : v , t : t } ;
57- maxKDE = Math . max ( maxKDE , v ) ;
40+ if ( cdi . min === cdi . max && bandwidth === 0 ) {
41+ // if span is zero and bandwidth is zero, we want a violin with zero width
42+ span = cdi . span = [ cdi . min , cdi . max ] ;
43+ cdi . density = [ { v : 1 , t : span [ 0 ] } ] ;
44+ cdi . bandwidth = bandwidth ;
45+ maxKDE = Math . max ( maxKDE , 1 ) ;
46+ } else {
47+ // step that well covers the bandwidth and is multiple of span distance
48+ var dist = span [ 1 ] - span [ 0 ] ;
49+ var n = Math . ceil ( dist / ( bandwidth / 3 ) ) ;
50+ var step = dist / n ;
51+
52+ if ( ! isFinite ( step ) || ! isFinite ( n ) ) {
53+ Lib . error ( 'Something went wrong with computing the violin span' ) ;
54+ cd [ 0 ] . t . empty = true ;
55+ return cd ;
56+ }
57+
58+ var kde = helpers . makeKDE ( cdi , trace , vals ) ;
59+ cdi . density = new Array ( n ) ;
60+
61+ for ( var k = 0 , t = span [ 0 ] ; t < ( span [ 1 ] + step / 2 ) ; k ++ , t += step ) {
62+ var v = kde ( t ) ;
63+ cdi . density [ k ] = { v : v , t : t } ;
64+ maxKDE = Math . max ( maxKDE , v ) ;
65+ }
5866 }
5967
6068 maxCount = Math . max ( maxCount , vals . length ) ;
@@ -100,8 +108,16 @@ function silvermanRule(len, ssd, iqr) {
100108function calcBandwidth ( trace , cdi , vals ) {
101109 var span = cdi . max - cdi . min ;
102110
103- // plot single-value violin with bandwidth of 1
104- if ( ! span ) return 1 ;
111+ // If span is zero
112+ if ( ! span ) {
113+ if ( trace . bandwidth ) {
114+ return trace . bandwidth ;
115+ } else {
116+ // if span is zero and no bandwidth is specified
117+ // it returns zero bandwidth which is a special case
118+ return 0 ;
119+ }
120+ }
105121
106122 // Limit how small the bandwidth can be.
107123 //
0 commit comments