/
x-axis.js
157 lines (133 loc) 路 4.35 KB
/
x-axis.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
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { StyleSheet, Text, View } from 'react-native'
import * as scale from 'd3-scale'
class XAxis extends PureComponent {
state = {
width: 0,
height: 0,
}
_onLayout(event) {
const { nativeEvent: { layout: { width } } } = event
if (width !== this.state.width) {
this.setState({ width })
}
}
render() {
const {
style,
values,
labelStyle,
spacing,
chartType,
formatLabel,
contentInset: {
left = 0,
right = 0,
},
} = this.props
const { width } = this.state
if (values.length === 0) {
return <View style={ style }/>
}
let labelWidth
let x
let transform
const domain = [ 0, values.length - 1 ]
switch (chartType) {
case 'bar': {
// use index as domain identifier instead of value since
// same value can occur at several places in dataPoints
x = scale.scaleBand()
.domain(values.map((_, index) => index))
.range([ left, width - right ])
.paddingInner([ spacing ])
.paddingOuter([ spacing ])
labelWidth = x.bandwidth()
transform = []
break
}
case 'line': {
labelWidth = ((width - left - right) / values.length )
transform = [ { translateX: -labelWidth / 2 } ]
x = scale.scaleLinear()
.domain(domain)
.range([ left, width - right ])
break
}
default:
console.warn(
`invalid chartType "${chartType}"
Must be one of "XAxis.Type.LINE" or "XAxis.type.BAR `,
)
labelWidth = 0
x = () => {}
transform = []
}
return (
<View style={ style }>
<View
style={ { flexGrow: 1 } }
onLayout={ event => this._onLayout(event) }
>
{/*invisible text to allow for parent resizing*/}
<Text style={ [ labelStyle, { color: 'transparent' } ] }>
{ formatLabel(values[ 0 ], 0) }
</Text>
{values.map((value, index) => {
return (
<Text
numberOfLines={1}
//'clip' not supported on android
// ellipsizeMode={'clip'}
key={`${value}-${index}`}
style={[
styles.text,
labelStyle,
{
width: labelWidth,
position: 'absolute',
left: x(index),
transform,
},
]}
>
{formatLabel(value, index)}
</Text>
)
})}
</View>
</View>
)
}
}
XAxis.Type = {
LINE: 'line',
BAR: 'bar',
}
XAxis.propTypes = {
values: PropTypes.array.isRequired,
labelStyle: PropTypes.any,
chartType: PropTypes.oneOf([ XAxis.Type.LINE, XAxis.Type.BAR ]),
spacing: PropTypes.number,
formatLabel: PropTypes.func,
contentInset: PropTypes.shape({
left: PropTypes.number,
right: PropTypes.number,
}),
}
XAxis.defaultProps = {
type: 'line',
spacing: 0.05,
chartType: XAxis.Type.LINE,
contentInset: {},
formatLabel: (value, index) => index,
}
const styles = StyleSheet.create({
text: {
backgroundColor: 'transparent',
textAlign: 'center',
fontSize: 10,
},
})
export default XAxis