@@ -26,6 +26,7 @@ interface IProps {
26
26
data ?: BarChartData [ ]
27
27
dataType ?: BarChartDataType
28
28
barWidth ?: number
29
+ minBarHeight ?: number
29
30
width ?: number
30
31
height ?: number
31
32
yCountTicks ?: number
@@ -45,6 +46,7 @@ interface IProps {
45
46
export const DEFAULT_MULTIPLIER_GRID = 5
46
47
export const DEFAULT_Y_TICKS = 8
47
48
export const DEFAULT_BAR_WIDTH = 40
49
+ export const MIN_BAR_HEIGHT = 3
48
50
let cleanedData : IDatum [ ] = [ ]
49
51
50
52
const BarChart = ( props : IProps ) => {
@@ -55,6 +57,7 @@ const BarChart = (props: IProps) => {
55
57
height : propHeight = 0 ,
56
58
barWidth = DEFAULT_BAR_WIDTH ,
57
59
yCountTicks = DEFAULT_Y_TICKS ,
60
+ minBarHeight = MIN_BAR_HEIGHT ,
58
61
dataType,
59
62
classNames,
60
63
divideLastColumn,
@@ -135,32 +138,6 @@ const BarChart = (props: IProps) => {
135
138
. domain ( [ 0 , maxY || 0 ] )
136
139
. range ( [ height , 0 ] )
137
140
138
- // bars
139
- svg
140
- . selectAll ( '.bar' )
141
- . data ( cleanedData )
142
- . enter ( )
143
- . append ( 'rect' )
144
- . attr ( 'class' , cx ( styles . bar , classNames ?. bar ) )
145
- . attr ( 'x' , ( d ) => xAxis ( d . index ) )
146
- . attr ( 'width' , barWidth )
147
- . attr ( 'y' , ( d ) => yAxis ( d . y ) )
148
- . attr ( 'height' , ( d ) => height - yAxis ( d . y ) )
149
- . attr ( 'data-testid' , ( d ) => `bar-${ d . x } -${ d . y } ` )
150
- . on ( 'mousemove mouseenter' , ( event , d ) => {
151
- tooltip . transition ( )
152
- . duration ( 200 )
153
- . style ( 'opacity' , 1 )
154
- tooltip . html ( tooltipValidation ( d . y , d . index ) )
155
- . style ( 'left' , `${ event . pageX + 16 } px` )
156
- . style ( 'top' , `${ event . pageY + 16 } px` )
157
- . attr ( 'data-testid' , 'bar-tooltip' )
158
- } )
159
- . on ( 'mouseout' , ( ) => {
160
- tooltip . transition ( )
161
- . style ( 'opacity' , 0 )
162
- } )
163
-
164
141
// divider for last column
165
142
if ( divideLastColumn ) {
166
143
svg . append ( 'line' )
@@ -210,6 +187,35 @@ const BarChart = (props: IProps) => {
210
187
yTicks . selectAll ( 'text' )
211
188
. attr ( 'x' , - 10 )
212
189
190
+ // bars
191
+ svg
192
+ . selectAll ( '.bar' )
193
+ . data ( cleanedData )
194
+ . enter ( )
195
+ . append ( 'rect' )
196
+ . attr ( 'class' , cx ( styles . bar , classNames ?. bar ) )
197
+ . attr ( 'x' , ( d ) => xAxis ( d . index ) )
198
+ . attr ( 'width' , barWidth )
199
+ // set minimal height for Bar
200
+ . attr ( 'y' , ( d ) => ( d . y && height - yAxis ( d . y ) < minBarHeight ? height - minBarHeight : yAxis ( d . y ) ) )
201
+ . attr ( 'height' , ( d ) => {
202
+ const initialHeight = height - yAxis ( d . y )
203
+ return initialHeight && initialHeight < minBarHeight ? minBarHeight : initialHeight
204
+ } )
205
+ . attr ( 'data-testid' , ( d ) => `bar-${ d . x } -${ d . y } ` )
206
+ . on ( 'mouseenter mousemove' , ( event , d ) => {
207
+ tooltip
208
+ . style ( 'opacity' , 1 )
209
+ tooltip . html ( tooltipValidation ( d . y , d . index ) )
210
+ . style ( 'left' , `${ event . pageX + 16 } px` )
211
+ . style ( 'top' , `${ event . pageY + 16 } px` )
212
+ . attr ( 'data-testid' , 'bar-tooltip' )
213
+ } )
214
+ . on ( 'mouseleave' , ( ) => {
215
+ tooltip
216
+ . style ( 'opacity' , 0 )
217
+ } )
218
+
213
219
return ( ) => {
214
220
tooltip . remove ( )
215
221
}
0 commit comments