@ -9,8 +9,9 @@ nv.models.discreteBar = function() {
getX = function ( d ) { return d . x } ,
getY = function ( d ) { return d . y } ,
forceY = [ 0 ] , // 0 is forced by default.. this makes sense for the majority of bar graphs... user can always do chart.forceY([]) to remove
clipEdge = true ,
color = d3 . scale . category20 ( ) . range ( ) ,
showValues = false ,
valueFormat = d3 . format ( ',.2f' ) ,
xDomain , yDomain ,
x0 , y0 ;
@ -51,8 +52,10 @@ nv.models.discreteBar = function() {
. rangeBands ( [ 0 , availableWidth ] , . 1 ) ;
y . domain ( yDomain || d3 . extent ( d3 . merge ( seriesData ) . map ( function ( d ) { return d . y } ) . concat ( forceY ) ) )
. range ( [ availableHeight , 0 ] ) ;
//.range([availableHeight, 0]);
if ( showValues ) y . range ( [ availableHeight - ( y . domain ( ) [ 0 ] < 0 ? 12 : 0 ) , y . domain ( ) [ 1 ] < 0 ? 0 : 12 ] ) ;
else y . range ( [ availableHeight , 0 ] ) ;
var wrap = d3 . select ( this ) . selectAll ( 'g.wrap.discretebar' ) . data ( [ data ] ) ;
@ -67,15 +70,6 @@ nv.models.discreteBar = function() {
defsEnter . append ( 'clipPath' )
. attr ( 'id' , 'edge-clip-' + id )
. append ( 'rect' ) ;
wrap . select ( '#edge-clip-' + id + ' rect' )
. attr ( 'width' , availableWidth )
. attr ( 'height' , availableHeight ) ;
g . attr ( 'clip-path' , clipEdge ? 'url(#edge-clip-' + id + ')' : '' ) ;
//TODO: by definiteion, the discrete bar should not have multiple groups, will modify/remove later
@ -91,32 +85,22 @@ nv.models.discreteBar = function() {
groups
. attr ( 'class' , function ( d , i ) { return 'group series-' + i } )
. classed ( 'hover' , function ( d ) { return d . hover } )
//.style('fill', function(d,i){ return color[i % 10] })
//.style('stroke', function(d,i){ return color[i % 10] });
d3 . transition ( groups )
. style ( 'stroke-opacity' , 1 )
. style ( 'fill-opacity' , . 75 ) ;
var bars = groups . selectAll ( ' rect .bar')
var bars = groups . selectAll ( ' g .bar')
. data ( function ( d ) { return d . values } ) ;
bars . exit ( ) . remove ( ) ;
var barsEnter = bars . enter ( ) . append ( ' rect ')
var barsEnter = bars . enter ( ) . append ( ' g ')
. attr ( 'class' , function ( d , i ) { return getY ( d , i ) < 0 ? 'bar negative' : 'bar positive' } )
//.attr('fill', function(d,i) { return color[0]; })
. attr ( 'x' , function ( d , i , j ) {
return j * x . rangeBand ( ) / data . length
. attr ( 'transform' , function ( d , i , j ) {
return 'translate(' + x ( getX ( d , i ) ) + ', ' + y ( 0 ) + ')'
} )
//.attr('y', function(d,i) { return y(Math.max(0, getY(d,i))) })
//.attr('height', function(d,i) { return Math.abs(y(getY(d,i)) - y(0)) })
. attr ( 'y' , function ( d ) { return y0 ( 0 ) } )
. attr ( 'height' , 0 )
. attr ( 'width' , x . rangeBand ( ) / data . length )
. style ( 'fill' , function ( d , i ) { return d . color || color [ i % 10 ] } ) //this is a 'hack' to allow multiple colors in a single series... will need to rethink this methodology
. style ( 'stroke' , function ( d , i ) { return d . color || color [ i % 10 ] } )
. on ( 'mouseover' , function ( d , i ) { //TODO: figure out why j works above, but not here
d3 . select ( this ) . classed ( 'hover' , true ) ;
dispatch . elementMouseover ( {
@ -164,26 +148,35 @@ nv.models.discreteBar = function() {
} ) ;
d3 . event . stopPropagation ( ) ;
} ) ;
barsEnter . append ( 'rect' )
. attr ( 'height' , 0 )
. attr ( 'width' , x . rangeBand ( ) / data . length )
. style ( 'fill' , function ( d , i ) { return d . color || color [ i % 10 ] } ) //this is a 'hack' to allow multiple colors in a single series... will need to rethink this methodology
. style ( 'stroke' , function ( d , i ) { return d . color || color [ i % 10 ] } )
if ( showValues ) {
barsEnter . append ( 'text' )
. attr ( 'text-anchor' , 'middle' )
bars . selectAll ( 'text' )
. attr ( 'dx' , x . rangeBand ( ) / 2 )
. attr ( 'dy' , function ( d , i ) { return getY ( d , i ) < 0 ? y ( getY ( d , i ) ) - y ( 0 ) + 12 : - 4 } )
. text ( function ( d , i ) { return valueFormat ( getY ( d , i ) ) } )
}
bars
. attr ( 'class' , function ( d , i ) { return getY ( d , i ) < 0 ? 'bar negative' : 'bar positive' } )
. attr ( 'transform' , function ( d , i ) { return 'translate(' + x ( getX ( d , i ) ) + ',0)' ; } )
//.attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',0)'; })
d3 . transition ( bars )
. delay ( function ( d , i ) { return i * 1200 / data [ 0 ] . values . length } )
. attr ( 'x' , function ( d , i ) {
return d . series * x . rangeBand ( ) / data . length
. attr ( ' transform ', function ( d , i ) {
return 'translate(' + x ( getX ( d , i ) ) + ', ' + ( getY ( d , i ) < 0 ? y ( 0 ) : y ( getY ( d , i ) ) ) + ')'
} )
. selectAll ( 'rect' )
. attr ( 'width' , x . rangeBand ( ) / data . length )
. each ( 'end' , function ( ) {
d3 . transition ( d3 . select ( this ) )
. attr ( 'y' , function ( d , i ) {
return getY ( d , i ) < 0 ?
y ( 0 ) :
y ( getY ( d , i ) )
} )
. attr ( 'height' , function ( d , i ) {
return Math . abs ( y ( getY ( d , i ) ) - y ( 0 ) )
} ) ;
} )
. attr ( 'height' , function ( d , i ) {
return Math . abs ( y ( getY ( d , i ) ) - y ( 0 ) )
} ) ;
@ -265,12 +258,6 @@ nv.models.discreteBar = function() {
return chart ;
} ;
chart . clipEdge = function ( _ ) {
if ( ! arguments . length ) return clipEdge ;
clipEdge = _ ;
return chart ;
} ;
chart . color = function ( _ ) {
if ( ! arguments . length ) return color ;
color = _ ;
@ -278,11 +265,22 @@ nv.models.discreteBar = function() {
} ;
chart . id = function ( _ ) {
if ( ! arguments . length ) return id ;
id = _ ;
return chart ;
if ( ! arguments . length ) return id ;
id = _ ;
return chart ;
} ;
chart . showValues = function ( _ ) {
if ( ! arguments . length ) return showValues ;
showValues = _ ;
return chart ;
} ;
chart . valuesFormat = function ( _ ) {
if ( ! arguments . length ) return valueFormat ;
valueFormat = _ ;
return chart ;
} ;
return chart ;