-
Notifications
You must be signed in to change notification settings - Fork 183
/
BpkBarchartBars.js
165 lines (149 loc) · 4.77 KB
/
BpkBarchartBars.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Backpack - Skyscanner's Design System
*
* Copyright 2016-2021 Skyscanner Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* @flow strict */
import React from 'react';
import { scaleBand, scaleLinear } from 'd3-scale';
import PropTypes from 'prop-types';
import { borderRadiusXs } from '@skyscanner/bpk-foundations-web/tokens/base.es6';
import BpkBarchartBar from './BpkBarchartBar';
import { remToPx } from './utils';
const borderRadius = remToPx(borderRadiusXs);
const getYPos = (point, { yScale, yScaleDataKey, maxYValue }) =>
yScale(Math.min(point[yScaleDataKey], maxYValue));
const getBarHeight = (
point,
{ height, margin, yScale, yScaleDataKey, maxYValue },
) => {
const barHeight =
height -
margin.top -
margin.bottom -
getYPos(point, { yScale, yScaleDataKey, maxYValue });
return Math.max(barHeight, 0);
};
const isOutlier = (point, { yScaleDataKey, maxYValue }) =>
point[yScaleDataKey] > maxYValue;
type Props = {
data: Array<any>, // We pass any here as the array can contain free form data depending on the user
xScaleDataKey: string,
yScaleDataKey: string,
height: number,
xScale: typeof scaleBand,
yScale: typeof scaleLinear,
maxYValue: number,
margin: {
top: number,
bottom: number,
left: number,
right: number,
},
getBarLabel: (any, string, string) => mixed,
BarComponent: typeof BpkBarchartBar,
getBarSelection: (any: any) => mixed,
outerPadding: number,
innerPadding: number,
onBarClick: ?(e: SyntheticEvent<any>, Object) => mixed,
onBarHover: ?(e: SyntheticEvent<any>, Object) => mixed,
onBarFocus: ?(e: SyntheticEvent<any>, Object) => mixed,
};
const BpkBarchartBars = (props: Props) => {
const {
outerPadding,
innerPadding,
onBarClick,
onBarHover,
onBarFocus,
data,
xScaleDataKey,
yScaleDataKey,
xScale,
yScale,
maxYValue,
margin,
height,
BarComponent,
getBarLabel,
getBarSelection,
...rest
} = props;
xScale.paddingOuter(outerPadding);
// `innerPadding` is not set on the scale, but instead passed to the BarComponent
// This allows us to have full-width tappable areas, but with visibly padded bars.
xScale.paddingInner(0);
const barWidth = xScale.bandwidth();
return (
<g>
{data.map((point, i) => {
const x = xScale(point[xScaleDataKey]);
const y = getYPos(point, { yScale, yScaleDataKey, maxYValue });
const outlier = isOutlier(point, props);
const barHeight = getBarHeight(point, props);
return (
// $FlowFixMe[cannot-spread-inexact] - inexact rest. See 'decisions/flowfixme.md'.
<BarComponent
key={`bar${i.toString()}`}
x={x}
y={outlier ? y - borderRadius : y}
width={barWidth}
height={outlier ? barHeight + borderRadius : barHeight}
label={getBarLabel(point, xScaleDataKey, yScaleDataKey)}
outlier={isOutlier(point, props)}
onClick={onBarClick ? e => onBarClick(e, { point }) : null}
onHover={onBarHover ? e => onBarHover(e, { point }) : null}
onFocus={onBarFocus ? e => onBarFocus(e, { point }) : null}
selected={getBarSelection(point)}
padding={innerPadding}
{...rest}
/>
);
})}
</g>
);
};
BpkBarchartBars.propTypes = {
data: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types
xScaleDataKey: PropTypes.string.isRequired,
yScaleDataKey: PropTypes.string.isRequired,
height: PropTypes.number.isRequired,
xScale: PropTypes.func.isRequired,
yScale: PropTypes.func.isRequired,
maxYValue: PropTypes.number.isRequired,
margin: PropTypes.shape({
top: PropTypes.number,
bottom: PropTypes.number,
left: PropTypes.number,
right: PropTypes.number,
}).isRequired,
getBarLabel: PropTypes.func.isRequired,
BarComponent: PropTypes.elementType.isRequired,
getBarSelection: PropTypes.func,
outerPadding: PropTypes.number,
innerPadding: PropTypes.number,
onBarClick: PropTypes.func,
onBarHover: PropTypes.func,
onBarFocus: PropTypes.func,
};
BpkBarchartBars.defaultProps = {
outerPadding: 0.35,
innerPadding: 0.35,
onBarClick: null,
onBarHover: null,
onBarFocus: null,
getBarSelection: () => false,
};
export default BpkBarchartBars;