Skip to content

Commit 4bd3f3b

Browse files
committed
Added extra props to axis children
Following the example from the charts, this adds extra props fro use in laying out axes children to both the x and y axes.
1 parent 006e633 commit 4bd3f3b

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/x-axis.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class XAxis extends PureComponent {
2626
spacingInner,
2727
spacingOuter,
2828
contentInset: {
29-
left = 0,
29+
left = 0,
3030
right = 0,
3131
},
3232
} = this.props
@@ -68,18 +68,25 @@ class XAxis extends PureComponent {
6868
const { height, width } = this.state
6969

7070
if (data.length === 0) {
71-
return <View style={ style }/>
71+
return <View style={ style } />
7272
}
7373

7474
const values = data.map((item, index) => xAccessor({ item, index }))
75-
const extent = array.extent(values)
75+
const extent = array.extent(values)
7676
const domain = scale === d3Scale.scaleBand ?
7777
values :
78-
[ min || extent[ 0 ], max || extent[ 1 ] ]
78+
[ min || extent[0], max || extent[1] ]
7979

80-
const x = this._getX(domain)
80+
const x = this._getX(domain)
8181
const ticks = numberOfTicks ? x.ticks(numberOfTicks) : values
8282

83+
const extraProps = {
84+
x,
85+
ticks,
86+
height,
87+
formatLabel,
88+
}
89+
8390
return (
8491
<View style={ style }>
8592
<View
@@ -88,7 +95,7 @@ class XAxis extends PureComponent {
8895
>
8996
{/*invisible text to allow for parent resizing*/}
9097
<Text style={{ opacity: 0, fontSize: svg.fontSize }}>
91-
{ formatLabel(ticks[0], 0) }
98+
{formatLabel(ticks[0], 0)}
9299
</Text>
93100
{
94101
height > 0 && width > 0 &&
@@ -100,13 +107,15 @@ class XAxis extends PureComponent {
100107
width,
101108
}}>
102109
<G>
103-
{children}
110+
{React.Children.map(children, child => {
111+
return React.cloneElement(child, extraProps)
112+
})}
104113
{
105114
// don't render labels if width isn't measured yet,
106115
// causes rendering issues
107116
width > 0 &&
108117
ticks.map((value, index) => {
109-
const { svg: valueSvg = {} } = data[ index ] || {}
118+
const { svg: valueSvg = {} } = data[index] || {}
110119

111120
return (
112121
<SVGText

src/y-axis.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ class YAxis extends PureComponent {
6767
const { height, width } = this.state
6868

6969
if (data.length === 0) {
70-
return <View style={ style }/>
70+
return <View style={ style } />
7171
}
7272

7373
const values = data.map((item, index) => yAccessor({ item, index }))
7474

7575
const extent = array.extent([ ...values, min, max ])
7676

7777
const {
78-
min = extent[ 0 ],
79-
max = extent[ 1 ],
78+
min = extent[0],
79+
max = extent[1],
8080
} = this.props
8181

8282
const domain = scale === d3Scale.scaleBand ? values : [ min, max ]
@@ -92,6 +92,13 @@ class YAxis extends PureComponent {
9292
.map((value, index) => formatLabel(value, index))
9393
.reduce((prev, curr) => prev.toString().length > curr.toString().length ? prev : curr, 0)
9494

95+
const extraProps = {
96+
y,
97+
ticks,
98+
width,
99+
formatLabel,
100+
}
101+
95102
return (
96103
<View style={ [ style ] }>
97104
<View
@@ -114,7 +121,9 @@ class YAxis extends PureComponent {
114121
width,
115122
}}>
116123
<G>
117-
{children}
124+
{React.Children.map(children, child => {
125+
return React.cloneElement(child, extraProps)
126+
})}
118127
{
119128
// don't render labels if width isn't measured yet,
120129
// causes rendering issues

0 commit comments

Comments
 (0)