Skip to content

Commit

Permalink
Merge pull request #238 from krzysztof-miemiec/fix-for-axis
Browse files Browse the repository at this point in the history
XAxis/YAxis crash fix
  • Loading branch information
JesperLekland committed Dec 5, 2018
2 parents c9f5c39 + d12d1f1 commit 006e633
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"peerDependencies": {
"react": ">=16.0.0-alpha.12",
"react-native": ">=0.46.0",
"react-native-svg": "^6.2.1"
"react-native-svg": "^6.2.1||^7.0.3"
},
"devDependencies": {
"date-fns": "^1.28.5",
Expand Down
50 changes: 26 additions & 24 deletions src/x-axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
import { Text, View } from 'react-native'
import * as d3Scale from 'd3-scale'
import * as array from 'd3-array'
import Svg, { Text as SVGText } from 'react-native-svg'
import Svg, { G, Text as SVGText } from 'react-native-svg'

class XAxis extends PureComponent {

Expand Down Expand Up @@ -99,29 +99,31 @@ class XAxis extends PureComponent {
height,
width,
}}>
{children}
{
// don't render labels if width isn't measured yet,
// causes rendering issues
width > 0 &&
ticks.map((value, index) => {
const { svg: valueSvg = {} } = data[ index ] || {}

return (
<SVGText
textAnchor={ 'middle' }
originX={ x(value) }
alignmentBaseline={ 'hanging' }
{ ...svg }
{ ...valueSvg }
key={ index }
x={ x(value) }
>
{formatLabel(value, index)}
</SVGText>
)
})
}
<G>
{children}
{
// don't render labels if width isn't measured yet,
// causes rendering issues
width > 0 &&
ticks.map((value, index) => {
const { svg: valueSvg = {} } = data[ index ] || {}

return (
<SVGText
textAnchor={ 'middle' }
originX={ x(value) }
alignmentBaseline={ 'hanging' }
{ ...svg }
{ ...valueSvg }
key={ index }
x={ x(value) }
>
{formatLabel(value, index)}
</SVGText>
)
})
}
</G>
</Svg>
}
</View>
Expand Down
46 changes: 24 additions & 22 deletions src/y-axis.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { Text, View } from 'react-native'
import { Svg, Text as SVGText } from 'react-native-svg'
import { Svg, G, Text as SVGText } from 'react-native-svg'
import * as d3Scale from 'd3-scale'
import * as array from 'd3-array'

Expand Down Expand Up @@ -113,27 +113,29 @@ class YAxis extends PureComponent {
height,
width,
}}>
{children}
{
// don't render labels if width isn't measured yet,
// causes rendering issues
height > 0 &&
ticks.map((value, index) => {
return (
<SVGText
originY={ y(value) }
textAnchor={ 'middle' }
x={ '50%' }
alignmentBaseline={ 'middle' }
{ ...svg }
key={ index }
y={ y(value) }
>
{formatLabel(value, index)}
</SVGText>
)
})
}
<G>
{children}
{
// don't render labels if width isn't measured yet,
// causes rendering issues
height > 0 &&
ticks.map((value, index) => {
return (
<SVGText
originY={ y(value) }
textAnchor={ 'middle' }
x={ '50%' }
alignmentBaseline={ 'middle' }
{ ...svg }
key={ index }
y={ y(value) }
>
{formatLabel(value, index)}
</SVGText>
)
})
}
</G>
</Svg>
}
</View>
Expand Down

0 comments on commit 006e633

Please sign in to comment.