@@ -282,14 +282,49 @@ describe('Test histogram', function() {
282282 var trace1 = { x : [ 1 , 2 , 3 , 4 ] } ;
283283 var trace2 = { x : [ 5 , 5.5 , 6 , 6.5 ] } ;
284284
285- expect ( calcPositions ( trace1 ) ) . toEqual ( [ 0.5 , 2.5 , 4.5 ] ) ;
285+ expect ( calcPositions ( trace1 ) ) . toBeCloseToArray ( [ 0.5 , 2.5 , 4.5 ] , 5 ) ;
286286
287- expect ( calcPositions ( trace2 ) ) . toEqual [ 5 , 6 , 7 ] ;
287+ expect ( calcPositions ( trace2 ) ) . toBeCloseToArray ( [ 5.5 , 6.5 ] , 5 ) ;
288288
289289 expect ( calcPositions ( trace1 , [ trace2 ] ) ) . toEqual ( [ 1 , 2 , 3 , 4 ] ) ;
290+ // huh, turns out even this one is an example of "unexpected bin positions"
291+ // (see another example below) - in this case it's because trace1 gets
292+ // autoshifted to keep integers off the bin edges, whereas trace2 doesn't
293+ // because there are as many integers as half-integers.
294+ // In this case though, it's unexpected but arguably better than the
295+ // "expected" result.
290296 expect ( calcPositions ( trace2 , [ trace1 ] ) ) . toEqual ( [ 5 , 6 , 7 ] ) ;
291297 } ) ;
292298
299+ it ( 'can sometimes give unexpected bin positions' , function ( ) {
300+ // documenting an edge case that might not be desirable but for now
301+ // we've decided to ignore: a larger bin sets the bin start, but then it
302+ // doesn't quite make sense with the smaller bin we end up with
303+ // we *could* fix this by ensuring that the bin start is based on the
304+ // same bin spec that gave the minimum bin size, but incremented down to
305+ // include the minimum start... but that would have awkward edge cases
306+ // involving month bins so for now we're ignoring it.
307+
308+ // all integers, so all autobins should get shifted to start 0.5 lower
309+ // than they otherwise would.
310+ var trace1 = { x : [ 1 , 2 , 3 , 4 ] } ;
311+ var trace2 = { x : [ - 2 , 1 , 4 , 7 ] } ;
312+
313+ // as above... size: 2
314+ expect ( calcPositions ( trace1 ) ) . toBeCloseToArray ( [ 0.5 , 2.5 , 4.5 ] , 5 ) ;
315+
316+ // {size: 5, start: -5.5}: -5..-1, 0..4, 5..9
317+ expect ( calcPositions ( trace2 ) ) . toEqual ( [ - 3 , 2 , 7 ] ) ;
318+
319+ // unexpected behavior when we put these together,
320+ // because 2 and 5 are mutually prime. Normally you could never get
321+ // groupings 1&2, 3&4... you'd always get 0&1, 2&3...
322+ expect ( calcPositions ( trace1 , [ trace2 ] ) ) . toBeCloseToArray ( [ 1.5 , 3.5 ] , 5 ) ;
323+ expect ( calcPositions ( trace2 , [ trace1 ] ) ) . toBeCloseToArray ( [
324+ - 2.5 , - 0.5 , 1.5 , 3.5 , 5.5 , 7.5
325+ ] , 5 ) ;
326+ } ) ;
327+
293328 it ( 'harmonizes autobins with smaller manual bins' , function ( ) {
294329 var trace1 = { x : [ 1 , 2 , 3 , 4 ] } ;
295330 var trace2 = { x : [ 5 , 6 , 7 , 8 ] , xbins : { start : 4.3 , end : 7.1 , size : 0.4 } } ;
@@ -315,7 +350,7 @@ describe('Test histogram', function() {
315350 var trace4 = { x : [ 1 , 1.2 , 1.4 , 1.6 ] , yaxis : 'y2' } ;
316351
317352 expect ( calcPositions ( trace1 , [ trace2 , trace3 , trace4 ] ) ) . toEqual ( [ 1 , 2 , 3 , 4 ] ) ;
318- expect ( calcPositions ( trace3 ) ) . toBeCloseToArray ( [ 0.9 , 1.1 , 1.3 ] ) ;
353+ expect ( calcPositions ( trace3 ) ) . toBeCloseToArray ( [ 0.9 , 1.1 , 1.3 ] , 5 ) ;
319354 } ) ;
320355
321356 describe ( 'cumulative distribution functions' , function ( ) {
0 commit comments