11'use strict' ;
22
3+ // We need to discuss moving these 'utility' functions to a depedency we can inject
4+ // since they are needed across services and directives.
5+ function abbreviateNumber ( value ) {
6+ var newValue = value ;
7+ if ( value >= 1000 ) {
8+ var suffixes = [ '' , 'K' , 'M' , 'B' , 'T' ] ;
9+ var suffixNum = Math . floor ( ( ( '' + value ) . length - 1 ) / 3 ) ;
10+ var shortValue = '' ;
11+ for ( var precision = 2 ; precision >= 1 ; precision -- ) {
12+ shortValue = parseFloat ( ( suffixNum !== 0 ? ( value / Math . pow ( 1000 , suffixNum ) ) : value ) . toPrecision ( precision ) ) ;
13+ var dotLessShortValue = ( shortValue + '' ) . replace ( / [ ^ a - z A - Z 0 - 9 ] + / g, '' ) ;
14+ if ( dotLessShortValue . length <= 3 ) { break ; }
15+ }
16+
17+ newValue = shortValue + suffixes [ suffixNum ] ;
18+ }
19+ return newValue ;
20+ }
21+
22+ function labelfy ( num ) {
23+ return '$' + abbreviateNumber ( num ) ;
24+ }
25+
326angular . module ( 'crunchinatorApp.directives' ) . directive ( 'd3Bars' , [ '$rootScope' ,
427 function ( $rootScope ) {
528 return {
629 restrict : 'EA' ,
730 scope : {
831 data : '=' ,
932 title : '@' ,
10- selected : '@'
33+ selected : '@' ,
34+ ranges : '@'
1135 } ,
1236 templateUrl : 'views/d3-chart.tpl.html' ,
1337 link : function ( scope , element ) {
@@ -17,7 +41,7 @@ angular.module('crunchinatorApp.directives').directive('d3Bars', ['$rootScope',
1741 var parent = angular . element ( element [ 0 ] ) . parent ( ) ;
1842 element = angular . element ( element [ 0 ] ) . find ( '.chart' ) ;
1943
20- var bars_fore , bars_back ;
44+ var bars_fore , bars_back , range ;
2145 var margin = { top : 0 , right : 10 , bottom : 20 , left : 0 } ;
2246 var width = element . width ( ) - margin . left - margin . right ;
2347 var height = parent . height ( ) - margin . top - margin . bottom - 124 ;
@@ -40,30 +64,38 @@ angular.module('crunchinatorApp.directives').directive('d3Bars', ['$rootScope',
4064 . attr ( 'width' , width )
4165 . attr ( 'height' , height ) ;
4266
43- var range = [ Infinity , - Infinity ] ;
67+ var set_min_max = function ( extent ) {
68+ range = [ Infinity , - Infinity ] ;
69+
70+ bars_fore . each ( function ( d ) {
71+ var point = x ( d . label ) ;
72+ if ( extent [ 0 ] <= point && point <= extent [ 1 ] ) {
73+ if ( d . start < range [ 0 ] ) {
74+ range [ 0 ] = d . start ;
75+ }
76+
77+ if ( d . end > range [ 1 ] ) {
78+ range [ 1 ] = d . end ;
79+ }
80+ }
81+ } ) ;
82+
83+ scope . min = labelfy ( range [ 0 ] ) ;
84+ scope . max = labelfy ( range [ 1 ] ) ;
85+ } ;
86+
4487 var brush = d3 . svg . brush ( )
4588 . x ( x )
4689 . extent ( [ 10 , width ] )
4790 . on ( 'brush' , function ( ) {
48-
49- range = [ Infinity , - Infinity ] ;
5091 var extent = brush . extent ( ) ;
5192
5293 svg . selectAll ( '#clip-' + id + ' rect' )
5394 . attr ( 'x' , extent [ 0 ] )
5495 . attr ( 'width' , extent [ 1 ] - extent [ 0 ] ) ;
5596
56- bars_fore . each ( function ( d ) {
57- var point = x ( d . label ) ;
58- if ( extent [ 0 ] <= point && point <= extent [ 1 ] ) {
59- if ( d . start < range [ 0 ] ) {
60- range [ 0 ] = d . start ;
61- }
62-
63- if ( d . end > range [ 1 ] ) {
64- range [ 1 ] = d . end ;
65- }
66- }
97+ scope . $parent . $apply ( function ( ) {
98+ set_min_max ( extent ) ;
6799 } ) ;
68100 } )
69101 . on ( 'brushend' , function ( ) {
@@ -164,6 +196,8 @@ angular.module('crunchinatorApp.directives').directive('d3Bars', ['$rootScope',
164196 'M' + ( 4.5 * x ) + ',' + ( y + 8 ) +
165197 'V' + ( 2 * y - 8 ) ;
166198 } ) ;
199+
200+ set_min_max ( brush . extent ( ) ) ;
167201 } ;
168202 }
169203 } ;
0 commit comments