@@ -876,13 +876,13 @@ d3.layout.histogram = function() {
876876 var frequency = true ,
877877 valuer = Number ,
878878 ranger = d3_layout_histogramRange ,
879- binner = d3_layout_histogramBins10 ;
879+ binner = d3_layout_histogramBinSturges ;
880880
881881 function histogram ( data , i ) {
882882 var bins = [ ] ,
883883 values = data . map ( valuer , this ) ,
884884 range = ranger . call ( this , values , i ) ,
885- thresholds = binner . call ( this , range , i ) ,
885+ thresholds = binner . call ( this , range , values , i ) ,
886886 bin ,
887887 i = - 1 ,
888888 n = values . length ,
@@ -897,7 +897,7 @@ d3.layout.histogram = function() {
897897 bin . y = 0 ;
898898 }
899899
900- // Fill the bins.
900+ // Fill the bins, ignoring values outside the range .
901901 i = - 1 ; while ( ++ i < n ) {
902902 x = values [ i ] ;
903903 if ( ( x >= range [ 0 ] ) && ( x <= range [ 1 ] ) ) {
@@ -935,12 +935,15 @@ d3.layout.histogram = function() {
935935 // uniformly into the given number of bins. Or, `x` may be an array of
936936 // threshold values, defining the bins; the specified array must contain the
937937 // rightmost (upper) value, thus specifying n + 1 values for n bins. Or, `x`
938- // may be a function which is evaluated, being passed the array of values and
939- // the current index `i`, returning an array of thresholds. The default bin
940- // function will divide the values into ten uniform bins.
938+ // may be a function which is evaluated, being passed the range, the array of
939+ // values, and the current index `i`, returning an array of thresholds. The
940+ // default bin function will divide the values into uniform bins using
941+ // Sturges' formula.
941942 histogram . bins = function ( x ) {
942943 if ( ! arguments . length ) return binner ;
943- binner = typeof x === "number" ? d3_layout_histogramBins ( x ) : d3 . functor ( x ) ;
944+ binner = typeof x === "number"
945+ ? function ( range ) { return d3_layout_histogramBinFixed ( range , x ) ; }
946+ : d3 . functor ( x ) ;
944947 return histogram ;
945948 } ;
946949
@@ -955,17 +958,17 @@ d3.layout.histogram = function() {
955958 return histogram ;
956959} ;
957960
958- var d3_layout_histogramBins10 = d3_layout_histogramBins ( 10 ) ;
961+ function d3_layout_histogramBinSturges ( range , values ) {
962+ return d3_layout_histogramBinFixed ( range , Math . ceil ( Math . log ( values . length ) / Math . LN2 + 1 ) ) ;
963+ }
959964
960- function d3_layout_histogramBins ( n ) {
961- return function ( range ) {
962- var x = - 1 ,
963- b = + range [ 0 ] ,
964- m = ( range [ 1 ] - b ) / n ,
965- f = [ ] ;
966- while ( ++ x <= n ) f [ x ] = m * x + b ;
967- return f ;
968- } ;
965+ function d3_layout_histogramBinFixed ( range , n ) {
966+ var x = - 1 ,
967+ b = + range [ 0 ] ,
968+ m = ( range [ 1 ] - b ) / n ,
969+ f = [ ] ;
970+ while ( ++ x <= n ) f [ x ] = m * x + b ;
971+ return f ;
969972}
970973
971974function d3_layout_histogramRange ( values ) {
0 commit comments