@@ -462,4 +462,117 @@ d3.time.years = d3_time_range(d3.time.year, function(date) {
462462d3 . time . years . utc = d3_time_range ( d3 . time . year . utc , function ( date ) {
463463 date . setUTCFullYear ( date . getUTCFullYear ( ) + 1 ) ;
464464} ) ;
465+ // TODO nice
466+ d3 . time . scale = function ( ) {
467+ var linear = d3 . scale . linear ( ) ;
468+
469+ function scale ( x ) {
470+ return linear ( x ) ;
471+ }
472+
473+ scale . invert = function ( x ) {
474+ return d3_scale_time ( linear . invert ( x ) ) ;
475+ } ;
476+
477+ scale . domain = function ( x ) {
478+ if ( ! arguments . length ) return linear . domain ( ) . map ( d3_scale_time ) ;
479+ linear . domain ( x ) ;
480+ return scale ;
481+ } ;
482+
483+ scale . ticks = function ( m ) {
484+ var extent = d3_scale_timeExtent ( scale . domain ( ) ) ,
485+ span = extent [ 1 ] - extent [ 0 ] ,
486+ target = span / m ;
487+ i = d3 . bisect ( d3_scale_timeSteps , target , 1 , d3_scale_timeSteps . length - 1 ) ;
488+ if ( Math . log ( target / d3_scale_timeSteps [ i - 1 ] ) < Math . log ( d3_scale_timeSteps [ i ] / target ) ) -- i ;
489+ return d3_scale_timeMethods [ i ] ( extent [ 0 ] , extent [ 1 ] ) ;
490+ } ;
491+
492+ scale . tickFormat = function ( ) {
493+ return d3_scale_timeFormat ;
494+ } ;
495+
496+ // TOOD expose d3_scale_linear_rebind?
497+ scale . range = d3 . rebind ( scale , linear . range ) ;
498+ scale . rangeRound = d3 . rebind ( scale , linear . rangeRound ) ;
499+ scale . interpolate = d3 . rebind ( scale , linear . interpolate ) ;
500+ scale . clamp = d3 . rebind ( scale , linear . clamp ) ;
501+
502+ return scale ;
503+ } ;
504+
505+ // TODO expose d3_scaleExtent?
506+ function d3_scale_timeExtent ( domain ) {
507+ var start = domain [ 0 ] , stop = domain [ domain . length - 1 ] ;
508+ return start < stop ? [ start , stop ] : [ stop , start ] ;
509+ }
510+
511+ function d3_scale_time ( t ) {
512+ return new Date ( t ) ;
513+ }
514+
515+ var d3_scale_timeFormats = [
516+ [ d3 . time . format ( "%Y" ) , function ( d ) { return true ; } ] ,
517+ [ d3 . time . format ( "%B" ) , function ( d ) { return d . getMonth ( ) ; } ] ,
518+ [ d3 . time . format ( "%b %d" ) , function ( d ) { return d . getDate ( ) != 1 ; } ] ,
519+ [ d3 . time . format ( "%a %d" ) , function ( d ) { return d . getDay ( ) && d . getDate ( ) != 1 ; } ] ,
520+ [ d3 . time . format ( "%I %p" ) , function ( d ) { return d . getHours ( ) ; } ] ,
521+ [ d3 . time . format ( "%I:%M" ) , function ( d ) { return d . getMinutes ( ) ; } ] ,
522+ [ d3 . time . format ( ":%S" ) , function ( d ) { return d . getSeconds ( ) || d . getMilliseconds ( ) ; } ]
523+ ] ;
524+
525+ var d3_scale_timeFormat = function ( date ) {
526+ var i = d3_scale_timeFormats . length - 1 , f = d3_scale_timeFormats [ i ] ;
527+ while ( ! f [ 1 ] ( date ) ) f = d3_scale_timeFormats [ -- i ] ;
528+ return f [ 0 ] ( date ) ;
529+ } ;
530+
531+ var d3_scale_timeSteps = [
532+ 1e3 , // 1-second
533+ 5e3 , // 5-second
534+ 15e3 , // 15-second
535+ 3e4 , // 30-second
536+ 6e4 , // 1-minute
537+ 3e5 , // 5-minute
538+ 9e5 , // 15-minute
539+ 18e5 , // 30-minute
540+ 36e5 , // 1-hour
541+ 108e5 , // 3-hour
542+ 216e5 , // 6-hour
543+ 432e5 , // 12-hour
544+ 864e5 , // 1-day
545+ 1728e5 , // 2-day
546+ 6048e5 , // 1-week
547+ 1728e6 , // 1-month
548+ 7776e6 , // 3-month
549+ 31536e6 // 1-year
550+ ] ;
551+
552+ var d3_scale_timeMethods = [
553+ d3 . time . seconds ,
554+ function ( a , b ) { return d3 . time . seconds ( a , b ) . filter ( function ( t ) { return ! ( t . getSeconds ( ) % 5 ) ; } ) ; } ,
555+ function ( a , b ) { return d3 . time . seconds ( a , b ) . filter ( function ( t ) { return ! ( t . getSeconds ( ) % 15 ) ; } ) ; } ,
556+ function ( a , b ) { return d3 . time . seconds ( a , b ) . filter ( function ( t ) { return ! ( t . getSeconds ( ) % 30 ) ; } ) ; } ,
557+ d3 . time . minutes ,
558+ function ( a , b ) { return d3 . time . minutes ( a , b ) . filter ( function ( t ) { return ! ( t . getMinutes ( ) % 5 ) ; } ) ; } ,
559+ function ( a , b ) { return d3 . time . minutes ( a , b ) . filter ( function ( t ) { return ! ( t . getMinutes ( ) % 15 ) ; } ) ; } ,
560+ function ( a , b ) { return d3 . time . minutes ( a , b ) . filter ( function ( t ) { return ! ( t . getMinutes ( ) % 30 ) ; } ) ; } ,
561+ d3 . time . hours ,
562+ function ( a , b ) { return d3 . time . hours ( a , b ) . filter ( function ( t ) { return ! ( t . getHours ( ) % 3 ) ; } ) ; } ,
563+ function ( a , b ) { return d3 . time . hours ( a , b ) . filter ( function ( t ) { return ! ( t . getHours ( ) % 6 ) ; } ) ; } ,
564+ function ( a , b ) { return d3 . time . hours ( a , b ) . filter ( function ( t ) { return ! ( t . getHours ( ) % 12 ) ; } ) ; } ,
565+ d3 . time . days ,
566+ function ( a , b ) { return d3 . time . days ( a , b ) . filter ( function ( t ) { return t . getDate ( ) % 2 ; } ) ; } ,
567+ d3 . time . weeks ,
568+ d3 . time . months ,
569+ function ( a , b ) { return d3 . time . months ( a , b ) . filter ( function ( t ) { return ! ( t . getMonth ( ) % 3 ) ; } ) ; } ,
570+ d3 . time . years
571+ ] ;
572+
573+ function d3_scale_timeFilter ( method , filter ) {
574+ return function ( a , b ) {
575+ return method ( a , b ) . filter ( filter ) ;
576+ } ;
577+ }
465578} ) ( ) ;
0 commit comments