@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
3
3
import { format } from 'd3-format' ;
4
4
import maxBy from 'lodash/maxBy' ;
5
5
import max from 'lodash/max' ;
6
+ import cx from 'classnames' ;
6
7
import {
7
8
Line ,
8
9
Bar ,
@@ -18,6 +19,7 @@ import {
18
19
19
20
import ChartToolTip from '../components/chart-tooltip' ;
20
21
import CustomTick from './custom-tick-component' ;
22
+ import CustomBackground from './custom-background-component' ;
21
23
import './composed-chart-styles.scss' ;
22
24
23
25
class CustomComposedChart extends PureComponent {
@@ -48,6 +50,8 @@ class CustomComposedChart extends PureComponent {
48
50
render ( ) {
49
51
const {
50
52
xKey,
53
+ yKey,
54
+ xKeys,
51
55
yKeys,
52
56
xAxis,
53
57
yAxis,
@@ -58,21 +62,26 @@ class CustomComposedChart extends PureComponent {
58
62
height,
59
63
margin
60
64
} = this . props . config ;
65
+
61
66
const {
62
67
className,
63
68
data,
64
69
config,
65
70
simple,
66
71
handleMouseMove,
67
- handleMouseLeave
72
+ handleMouseLeave,
73
+ handleClick,
74
+ barBackground
68
75
} = this . props ;
69
76
70
- const { lines, bars, areas } = yKeys ;
77
+ const isVertical = ! ! xKeys ;
78
+ const dataKeys = yKeys || xKeys ;
79
+ const { lines, bars, areas } = dataKeys ;
71
80
const maxYValue = this . findMaxValue ( data , config ) ;
72
81
73
82
return (
74
83
< div
75
- className = { ` c-composed-chart ${ className } ` }
84
+ className = { cx ( ' c-composed-chart' , className ) }
76
85
style = { { height : simple ? 100 : height || 250 } }
77
86
>
78
87
< ResponsiveContainer width = "99%" >
@@ -81,14 +90,16 @@ class CustomComposedChart extends PureComponent {
81
90
margin = {
82
91
margin || {
83
92
top : ! simple ? 15 : 0 ,
84
- right : 0 ,
85
- left : ! simple ? 42 : 0 ,
93
+ right : isVertical ? 10 : 0 ,
94
+ left : simple || isVertical ? 0 : 42 ,
86
95
bottom : 0
87
96
}
88
97
}
89
98
padding = { { left : 50 } }
90
99
onMouseMove = { handleMouseMove }
91
100
onMouseLeave = { handleMouseLeave }
101
+ onClick = { handleClick }
102
+ layout = { isVertical ? 'vertical' : 'horizontal' }
92
103
>
93
104
< defs >
94
105
{ gradients &&
@@ -121,35 +132,50 @@ class CustomComposedChart extends PureComponent {
121
132
/>
122
133
{ ! simple && (
123
134
< YAxis
135
+ dataKey = { yKey || '' }
136
+ tickLine = { ! isVertical }
124
137
axisLine = { false }
125
- strokeDasharray = "3 4"
126
- tickSize = { - 42 }
127
- mirror
128
- tickMargin = { 0 }
138
+ { ...( ! isVertical
139
+ ? {
140
+ strokeDasharray : '3 4' ,
141
+ tickSize : - 42 ,
142
+ mirror : true ,
143
+ tickMargin : 0
144
+ }
145
+ : { } ) }
129
146
tick = {
130
147
< CustomTick
131
- dataMax = { maxYValue }
148
+ dataMax = { xKeys && maxYValue }
132
149
unit = { unit || '' }
133
150
unitFormat = {
134
151
unitFormat ||
135
152
( value =>
136
153
( value < 1 ? format ( '.2r' ) ( value ) : format ( '.2s' ) ( value ) ) )
137
154
}
138
155
fill = "#555555"
156
+ vertical = { isVertical }
139
157
/>
140
158
}
141
159
{ ...yAxis }
142
160
/>
143
161
) }
144
162
{ ! simple && (
145
- < CartesianGrid vertical = { false } strokeDasharray = "3 4" />
163
+ < CartesianGrid
164
+ vertical = { isVertical }
165
+ horizontal = { ! isVertical }
166
+ strokeDasharray = "3 4"
167
+ />
146
168
) }
169
+
147
170
< Tooltip
148
171
simple = { simple }
149
172
cursor = { {
150
173
opacity : 0.5 ,
151
174
stroke : '#d6d6d9' ,
152
- ...( ! ! bars && { strokeWidth : `${ 1.2 * ( 100 / data . length ) } %` } )
175
+ ...( ! ! bars && {
176
+ strokeWidth : `${ 1.2 *
177
+ ( ( isVertical ? 45 : 100 ) / data . length ) } %`
178
+ } )
153
179
} }
154
180
content = { < ChartToolTip settings = { tooltip } /> }
155
181
/>
@@ -169,7 +195,20 @@ class CustomComposedChart extends PureComponent {
169
195
) ) }
170
196
{ bars &&
171
197
Object . keys ( bars ) . map ( key => (
172
- < Bar key = { key } dataKey = { key } dot = { false } { ...bars [ key ] } >
198
+ < Bar
199
+ key = { key }
200
+ dataKey = { key }
201
+ dot = { false }
202
+ background = { d =>
203
+ barBackground && (
204
+ < CustomBackground
205
+ { ...d }
206
+ activeIndex = { barBackground . activeIndex }
207
+ />
208
+ )
209
+ }
210
+ { ...bars [ key ] }
211
+ >
173
212
{ bars [ key ] . itemColor &&
174
213
data . map ( item => (
175
214
< Cell key = { `c_${ item . color } ` } fill = { item . color } />
@@ -190,7 +229,9 @@ CustomComposedChart.propTypes = {
190
229
simple : PropTypes . bool ,
191
230
handleMouseMove : PropTypes . func ,
192
231
handleMouseLeave : PropTypes . func ,
193
- backgroundColor : PropTypes . string
232
+ handleClick : PropTypes . func ,
233
+ backgroundColor : PropTypes . string ,
234
+ barBackground : PropTypes . object
194
235
} ;
195
236
196
237
export default CustomComposedChart ;
0 commit comments