File tree 3 files changed +40
-3
lines changed
Filter options
3 files changed +40
-3
lines changed
Original file line number Diff line number Diff line change @@ -30,19 +30,26 @@ function Sieve(traces, separateNegativeValues, dontMergeOverlappingData) {
30
30
this . separateNegativeValues = separateNegativeValues ;
31
31
this . dontMergeOverlappingData = dontMergeOverlappingData ;
32
32
33
+ // for single-bin histograms - see histogram/calc
34
+ var width1 = Infinity ;
35
+
33
36
var positions = [ ] ;
34
37
for ( var i = 0 ; i < traces . length ; i ++ ) {
35
38
var trace = traces [ i ] ;
36
39
for ( var j = 0 ; j < trace . length ; j ++ ) {
37
40
var bar = trace [ j ] ;
38
41
if ( bar . p !== BADNUM ) positions . push ( bar . p ) ;
39
42
}
43
+ if ( trace [ 0 ] && trace [ 0 ] . width1 ) {
44
+ width1 = Math . min ( trace [ 0 ] . width1 , width1 ) ;
45
+ }
40
46
}
41
47
this . positions = positions ;
42
48
43
- var dv = Lib . distinctVals ( this . positions ) ;
49
+ var dv = Lib . distinctVals ( positions ) ;
44
50
this . distinctPositions = dv . vals ;
45
- this . minDiff = dv . minDiff ;
51
+ if ( dv . vals . length === 1 && width1 ) this . minDiff = width1 ;
52
+ else this . minDiff = Math . min ( dv . minDiff , width1 ) ;
46
53
47
54
this . binWidth = this . minDiff ;
48
55
Original file line number Diff line number Diff line change @@ -135,7 +135,7 @@ module.exports = function calc(gd, trace) {
135
135
break ;
136
136
}
137
137
}
138
- for ( i = seriesLen - 1 ; i > firstNonzero ; i -- ) {
138
+ for ( i = seriesLen - 1 ; i >= firstNonzero ; i -- ) {
139
139
if ( size [ i ] ) {
140
140
lastNonzero = i ;
141
141
break ;
@@ -149,6 +149,12 @@ module.exports = function calc(gd, trace) {
149
149
}
150
150
}
151
151
152
+ if ( cd . length === 1 ) {
153
+ // when we collapse to a single bin, calcdata no longer describes bin size
154
+ // so we need to explicitly specify it
155
+ cd [ 0 ] . width1 = Axes . tickIncrement ( cd [ 0 ] . p , binSpec . size , false , calendar ) - cd [ 0 ] . p ;
156
+ }
157
+
152
158
arraysToCalcdata ( cd , trace ) ;
153
159
154
160
return cd ;
Original file line number Diff line number Diff line change @@ -269,6 +269,17 @@ describe('Test histogram', function() {
269
269
expect ( out . length ) . toEqual ( 9001 ) ;
270
270
} ) ;
271
271
272
+ it ( 'handles single-bin data without extra bins' , function ( ) {
273
+ var out = _calc ( {
274
+ x : [ 2.1 , 3 , 3.9 ] ,
275
+ xbins : { start : 0 , end : 10 , size : 2 }
276
+ } ) ;
277
+
278
+ expect ( out ) . toEqual ( [
279
+ { b : 0 , p : 3 , s : 3 , width1 : 2 }
280
+ ] ) ;
281
+ } ) ;
282
+
272
283
function calcPositions ( opts , extraTraces ) {
273
284
return _calc ( opts , extraTraces ) . map ( function ( v ) { return v . p ; } ) ;
274
285
}
@@ -554,5 +565,18 @@ describe('Test histogram', function() {
554
565
. catch ( fail )
555
566
. then ( done ) ;
556
567
} ) ;
568
+
569
+ it ( 'give the right bar width for single-bin histograms' , function ( done ) {
570
+ Plotly . newPlot ( gd , [ {
571
+ type : 'histogram' ,
572
+ x : [ 3 , 3 , 3 ] ,
573
+ xbins : { start : 0 , end : 10 , size : 2 }
574
+ } ] )
575
+ . then ( function ( ) {
576
+ expect ( gd . _fullLayout . xaxis . range ) . toBeCloseToArray ( [ 2 , 4 ] , 3 ) ;
577
+ } )
578
+ . catch ( fail )
579
+ . then ( done ) ;
580
+ } ) ;
557
581
} ) ;
558
582
} ) ;
You can’t perform that action at this time.
0 commit comments