@@ -19,7 +19,7 @@ var normFunctions = require('./norm_functions');
1919var doAvg = require ( './average' ) ;
2020var getBinSpanLabelRound = require ( './bin_label_vals' ) ;
2121
22- module . exports = function calc ( gd , trace ) {
22+ function calc ( gd , trace ) {
2323 var pos = [ ] ;
2424 var size = [ ] ;
2525 var pa = Axes . getFromId ( gd , trace . orientation === 'h' ? trace . yaxis : trace . xaxis ) ;
@@ -193,23 +193,30 @@ module.exports = function calc(gd, trace) {
193193 }
194194
195195 return cd ;
196- } ;
196+ }
197197
198198/*
199- * calcAllAutoBins: we want all histograms on the same axes to share bin specs
200- * if they're grouped or stacked. If the user has explicitly specified differing
199+ * calcAllAutoBins: we want all histograms inside the same bingroup
200+ * (see logic in Histogram.crossTraceDefaults) to share bin specs
201+ *
202+ * If the user has explicitly specified differing
201203 * bin specs, there's nothing we can do, but if possible we will try to use the
202- * smallest bins of any of the auto values for all histograms grouped/stacked
203- * together .
204+ * smallest bins of any of the auto values for all histograms inside the same
205+ * bingroup .
204206 */
205207function calcAllAutoBins ( gd , trace , pa , mainData , _overlayEdgeCase ) {
206208 var binAttr = mainData + 'bins' ;
207209 var fullLayout = gd . _fullLayout ;
210+ var groupName = trace [ '_' + mainData + 'bingroup' ] ;
211+ var binOpts = fullLayout . _histogramBinOpts [ groupName ] ;
208212 var isOverlay = fullLayout . barmode === 'overlay' ;
209213 var i , traces , tracei , calendar , pos0 , autoVals , cumulativeSpec ;
210214
211- var cleanBound = ( pa . type === 'date' ) ?
212- function ( v ) { return ( v || v === 0 ) ? Lib . cleanDate ( v , null , pa . calendar ) : null ; } :
215+ var r2c = function ( v ) { return pa . r2c ( v , 0 , calendar ) ; } ;
216+ var c2r = function ( v ) { return pa . c2r ( v , 0 , calendar ) ; } ;
217+
218+ var cleanBound = pa . type === 'date' ?
219+ function ( v ) { return ( v || v === 0 ) ? Lib . cleanDate ( v , null , calendar ) : null ; } :
213220 function ( v ) { return isNumeric ( v ) ? Number ( v ) : null ; } ;
214221
215222 function setBound ( attr , bins , newBins ) {
@@ -222,28 +229,29 @@ function calcAllAutoBins(gd, trace, pa, mainData, _overlayEdgeCase) {
222229 }
223230 }
224231
225- var binOpts = fullLayout . _histogramBinOpts [ trace . _groupName ] ;
226-
227232 // all but the first trace in this group has already been marked finished
228233 // clear this flag, so next time we run calc we will run autobin again
229234 if ( trace . _autoBinFinished ) {
230235 delete trace . _autoBinFinished ;
231236 } else {
232237 traces = binOpts . traces ;
233- var sizeFound = binOpts . sizeFound ;
234238 var allPos = [ ] ;
235- autoVals = traces [ 0 ] . _autoBin = { } ;
239+
236240 // Note: we're including `legendonly` traces here for autobin purposes,
237241 // so that showing & hiding from the legend won't affect bins.
238242 // But this complicates things a bit since those traces don't `calc`,
239243 // hence `isFirstVisible`.
240244 var isFirstVisible = true ;
241245 for ( i = 0 ; i < traces . length ; i ++ ) {
242246 tracei = traces [ i ] ;
247+
243248 if ( tracei . visible ) {
244- pos0 = tracei . _pos0 = pa . makeCalcdata ( tracei , mainData ) ;
249+ var mainDatai = binOpts . dirs [ i ] ;
250+ pos0 = tracei [ '_' + mainDatai + 'pos0' ] = pa . makeCalcdata ( tracei , mainDatai ) ;
251+
245252 allPos = Lib . concat ( allPos , pos0 ) ;
246253 delete tracei . _autoBinFinished ;
254+
247255 if ( trace . visible === true ) {
248256 if ( isFirstVisible ) {
249257 isFirstVisible = false ;
@@ -254,10 +262,15 @@ function calcAllAutoBins(gd, trace, pa, mainData, _overlayEdgeCase) {
254262 }
255263 }
256264 }
265+
257266 calendar = traces [ 0 ] [ mainData + 'calendar' ] ;
258- var newBinSpec = Axes . autoBin (
259- allPos , pa , binOpts . nbins , false , calendar , sizeFound && binOpts . size ) ;
267+ var newBinSpec = Axes . autoBin ( allPos , pa , binOpts . nbins , false , calendar , binOpts . sizeFound && binOpts . size ) ;
268+
269+ var autoBin = traces [ 0 ] . _autoBin = { } ;
270+ autoVals = autoBin [ binOpts . dirs [ 0 ] ] = { } ;
260271
272+ // TODO how does work with bingroup ????
273+ //
261274 // Edge case: single-valued histogram overlaying others
262275 // Use them all together to calculate the bin size for the single-valued one
263276 if ( isOverlay && newBinSpec . _dataSpan === 0 &&
@@ -274,20 +287,16 @@ function calcAllAutoBins(gd, trace, pa, mainData, _overlayEdgeCase) {
274287 cumulativeSpec = tracei . cumulative ;
275288 if ( cumulativeSpec . enabled && ( cumulativeSpec . currentbin !== 'include' ) ) {
276289 if ( cumulativeSpec . direction === 'decreasing' ) {
277- newBinSpec . start = pa . c2r ( Axes . tickIncrement (
278- pa . r2c ( newBinSpec . start , 0 , calendar ) ,
279- newBinSpec . size , true , calendar
280- ) ) ;
290+ newBinSpec . start = c2r ( Axes . tickIncrement (
291+ r2c ( newBinSpec . start ) , newBinSpec . size , true , calendar ) ) ;
281292 } else {
282- newBinSpec . end = pa . c2r ( Axes . tickIncrement (
283- pa . r2c ( newBinSpec . end , 0 , calendar ) ,
284- newBinSpec . size , false , calendar
285- ) ) ;
293+ newBinSpec . end = c2r ( Axes . tickIncrement (
294+ r2c ( newBinSpec . end ) , newBinSpec . size , false , calendar ) ) ;
286295 }
287296 }
288297
289298 binOpts . size = newBinSpec . size ;
290- if ( ! sizeFound ) {
299+ if ( ! binOpts . sizeFound ) {
291300 autoVals . size = newBinSpec . size ;
292301 Lib . nestedProperty ( traces [ 0 ] , binAttr + '.size' ) . set ( newBinSpec . size ) ;
293302 }
@@ -296,8 +305,8 @@ function calcAllAutoBins(gd, trace, pa, mainData, _overlayEdgeCase) {
296305 setBound ( 'end' , binOpts , newBinSpec ) ;
297306 }
298307
299- pos0 = trace . _pos0 ;
300- delete trace . _pos0 ;
308+ pos0 = trace [ '_' + mainData + 'pos0' ] ;
309+ delete trace [ '_' + mainData + 'pos0' ] ;
301310
302311 // Each trace can specify its own start/end, or if omitted
303312 // we ensure they're beyond the bounds of this trace's data,
@@ -390,7 +399,7 @@ function handleSingleValueOverlays(gd, trace, pa, mainData, binAttr) {
390399 // so we can use this result when we get to tracei in the normal
391400 // course of events, mark it as done and put _pos0 back
392401 tracei . _autoBinFinished = 1 ;
393- tracei . _pos0 = resulti [ 1 ] ;
402+ tracei [ '_' + mainData + 'pos0' ] = resulti [ 1 ] ;
394403
395404 if ( isSingleValued ) {
396405 singleValuedTraces . push ( tracei ) ;
@@ -404,7 +413,7 @@ function handleSingleValueOverlays(gd, trace, pa, mainData, binAttr) {
404413 // hunt through pos0 for the first valid value
405414 var dataVals = new Array ( singleValuedTraces . length ) ;
406415 for ( i = 0 ; i < singleValuedTraces . length ; i ++ ) {
407- var pos0 = singleValuedTraces [ i ] . _pos0 ;
416+ var pos0 = singleValuedTraces [ i ] [ '_' + mainData + 'pos0' ] ;
408417 for ( var j = 0 ; j < pos0 . length ; j ++ ) {
409418 if ( pos0 [ j ] !== undefined ) {
410419 dataVals [ i ] = pos0 [ j ] ;
@@ -462,7 +471,6 @@ function getConnectedHistograms(gd, trace) {
462471 return out ;
463472}
464473
465-
466474function cdf ( size , direction , currentBin ) {
467475 var i , vi , prevSum ;
468476
@@ -510,3 +518,8 @@ function cdf(size, direction, currentBin) {
510518 }
511519 }
512520}
521+
522+ module . exports = {
523+ calc : calc ,
524+ calcAllAutoBins : calcAllAutoBins
525+ } ;
0 commit comments