@@ -5,6 +5,7 @@ Raphael.fn.g.barchart = function (x, y, width, height, values, isVertical, opts)
55 chart = this . set ( ) ,
66 bars = this . set ( ) ,
77 covers = this . set ( ) ,
8+ covers2 = this . set ( ) ,
89 total = Math . max . apply ( Math , values ) ,
910 stacktotal = [ ] ,
1011 paper = this ,
@@ -40,12 +41,16 @@ Raphael.fn.g.barchart = function (x, y, width, height, values, isVertical, opts)
4041
4142 total = ( opts . to ) || total ;
4243 if ( ! isVertical ) {
43- var barwidth = Math . round ( width / ( len * ( 100 + gutter ) + gutter ) * 100 ) ,
44+ var barwidth = width / ( len * ( 100 + gutter ) + gutter ) * 100 ,
4445 barhgutter = barwidth * gutter / 100 ,
45- barvgutter = 20 ,
46+ barvgutter = typeof opts . vgutter == "undefined" ? 20 : opts . vgutter ,
4647 stack = [ ] ,
4748 X = x + barhgutter ,
4849 Y = ( height - 2 * barvgutter ) / total ;
50+ if ( ! opts . stretch ) {
51+ barhgutter = Math . round ( barhgutter ) ;
52+ barwidth = Math . floor ( barwidth ) ;
53+ }
4954 ! opts . stacked && ( barwidth /= multi || 1 ) ;
5055 for ( var i = 0 ; i < len ; i ++ ) {
5156 stack = [ ] ;
@@ -67,6 +72,12 @@ Raphael.fn.g.barchart = function (x, y, width, height, values, isVertical, opts)
6772 }
6873 }
6974 if ( opts . stacked ) {
75+ var cvr ;
76+ covers2 . push ( cvr = this . rect ( stack [ 0 ] . x - stack [ 0 ] . w / 2 , y , barwidth , height ) . attr ( { stroke : "none" , fill : "#000" , opacity : 0 } ) ) ;
77+ cvr . bars = [ ] ;
78+ for ( var s = 0 , ss = stack . length ; s < ss ; s ++ ) {
79+ cvr . bars . push ( stack [ s ] ) ;
80+ }
7081 stack . sort ( function ( a , b ) {
7182 return a . value - b . value ;
7283 } ) ;
@@ -87,10 +98,12 @@ Raphael.fn.g.barchart = function (x, y, width, height, values, isVertical, opts)
8798 cover . bar = bar ;
8899 size += bar . value ;
89100 }
101+
90102 X += barwidth ;
91103 }
92104 X += barhgutter ;
93105 }
106+ covers2 . toFront ( ) ;
94107 X = x + barhgutter ;
95108 if ( ! opts . stacked ) {
96109 for ( var i = 0 ; i < len ; i ++ ) {
@@ -106,10 +119,38 @@ Raphael.fn.g.barchart = function (x, y, width, height, values, isVertical, opts)
106119 chart . label = function ( labels , isBottom ) {
107120 labels = labels || [ ] ;
108121 this . labels = paper . set ( ) ;
109- for ( var i = 0 ; i < len ; i ++ ) {
110- for ( var j = 0 ; j < multi ; j ++ ) {
111- var label = paper . g . labelise ( multi ? labels [ j ] && labels [ j ] [ i ] : labels [ i ] , multi ? values [ j ] [ i ] : values [ i ] , total ) ;
112- this . labels . push ( paper . g . label ( bars [ i * ( multi || 1 ) + j ] . x , isBottom ? y + height - barvgutter / 2 : bars [ i * ( multi || 1 ) + j ] . y - 10 , label ) . attr ( [ { fill : "none" } ] ) . insertBefore ( covers [ 0 ] ) ) ;
122+ var L , l = - Infinity ;
123+ if ( opts . stacked ) {
124+ for ( var i = 0 ; i < len ; i ++ ) {
125+ var tot = 0 ;
126+ for ( var j = 0 ; j < multi ; j ++ ) {
127+ tot += multi ? values [ j ] [ i ] : values [ i ] ;
128+ if ( j == multi - 1 ) {
129+ var label = paper . g . labelise ( labels [ i ] , tot , total ) ;
130+ L = paper . text ( bars [ i * ( multi || 1 ) + j ] . x , y + height - barvgutter / 2 , label ) . insertBefore ( covers [ i * ( multi || 1 ) + j ] ) ;
131+ var bb = L . getBBox ( ) ;
132+ if ( bb . x - 7 < l ) {
133+ L . remove ( ) ;
134+ } else {
135+ this . labels . push ( L ) ;
136+ l = bb . x + bb . width ;
137+ }
138+ }
139+ }
140+ }
141+ } else {
142+ for ( var i = 0 ; i < len ; i ++ ) {
143+ for ( var j = 0 ; j < multi ; j ++ ) {
144+ var label = paper . g . labelise ( multi ? labels [ j ] && labels [ j ] [ i ] : labels [ i ] , multi ? values [ j ] [ i ] : values [ i ] , total ) ;
145+ L = paper . text ( bars [ i * ( multi || 1 ) + j ] . x , isBottom ? y + height - barvgutter / 2 : bars [ i * ( multi || 1 ) + j ] . y - 10 , label ) . insertBefore ( covers [ i * ( multi || 1 ) + j ] ) ;
146+ var bb = L . getBBox ( ) ;
147+ if ( bb . x - 7 < l ) {
148+ L . remove ( ) ;
149+ } else {
150+ this . labels . push ( L ) ;
151+ l = bb . x + bb . width ;
152+ }
153+ }
113154 }
114155 }
115156 return this ;
@@ -151,7 +192,7 @@ Raphael.fn.g.barchart = function (x, y, width, height, values, isVertical, opts)
151192 var bar = stack [ s ] ,
152193 cover ,
153194 val = Math . round ( ( size + bar . value ) * X ) ,
154- path = this . g . finger ( x /* + !!size * .5*/ , bar . y , val , barheight - 1 , false , type , 1 ) ;
195+ path = this . g . finger ( x , bar . y , val , barheight - 1 , false , type , 1 ) ;
155196 size && opts . init && bar . animate ( { path : path } , ( + opts . init - 1 ) || 1000 , ">" ) ;
156197 size && ! opts . init && bar . attr ( { path : path } ) ;
157198 bar . w = val ;
@@ -195,13 +236,25 @@ Raphael.fn.g.barchart = function (x, y, width, height, values, isVertical, opts)
195236 } ;
196237 }
197238 chart . hover = function ( fin , fout ) {
239+ covers2 . hide ( ) ;
240+ covers . show ( ) ;
198241 fout = fout || function ( ) { } ;
199242 var that = this ;
200243 for ( var i = 0 , ii = covers . length ; i < ii ; i ++ ) {
201244 covers [ i ] . mouseover ( fin ) . mouseout ( fout ) ;
202245 }
203246 return this ;
204247 } ;
248+ chart . hoverColumn = function ( fin , fout ) {
249+ covers . hide ( ) ;
250+ covers2 . show ( ) ;
251+ fout = fout || function ( ) { } ;
252+ var that = this ;
253+ for ( var i = 0 , ii = covers2 . length ; i < ii ; i ++ ) {
254+ covers2 [ i ] . mouseover ( fin ) . mouseout ( fout ) ;
255+ }
256+ return this ;
257+ } ;
205258 chart . push ( bars ) ;
206259 chart . bars = bars ;
207260 chart . covers = covers ;
0 commit comments