Skip to content

Commit

Permalink
Fix floating point error in axis
Browse files Browse the repository at this point in the history
  • Loading branch information
xanderdeseyn committed Jan 18, 2021
1 parent 815d031 commit 8e61d42
Showing 8 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
"dependencies": {
"@yr/monotone-cubic-spline": "^1.0.3",
"deepmerge": "^4.2.2",
"fast-deep-equal": "^3.1.3",
"lodash.clamp": "^4.0.3",
"lodash.clonedeep": "^4.5.0",
"lodash.debounce": "^4.0.8",
2 changes: 1 addition & 1 deletion demo/yarn.lock
Original file line number Diff line number Diff line change
@@ -3448,7 +3448,7 @@ fancy-log@^1.3.2:
parse-node-version "^1.0.0"
time-stamp "^1.0.0"

fast-deep-equal@^3.1.1:
fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@
"dependencies": {
"@yr/monotone-cubic-spline": "^1.0.3",
"deepmerge": "^4.2.2",
"fast-deep-equal": "^3.1.3",
"lodash.clamp": "^4.0.3",
"lodash.clonedeep": "^4.5.0",
"lodash.debounce": "^4.0.8",
7 changes: 4 additions & 3 deletions src/Chart.tsx
Original file line number Diff line number Diff line change
@@ -2,13 +2,14 @@ import * as React from 'react'
import deepmerge from 'deepmerge'
import { Animated, NativeSyntheticEvent, View, ViewStyle } from 'react-native'
import { TapGestureHandler, PanGestureHandler, State } from 'react-native-gesture-handler'
import fastEqual from 'fast-deep-equal/react'
import clamp from 'lodash.clamp'
import minBy from 'lodash.minby'
import maxBy from 'lodash.maxby'
import debounce from 'lodash.debounce'
import Svg, { G, Mask, Defs, Rect } from 'react-native-svg'
import { useComponentDimensions } from './useComponentDimensions'
import { AxisDomain, ChartDataPoint, Padding, XYValue, ViewPort, TouchEvent } from './types'
import { AxisDomain, ChartDataPoint, Padding, ViewPort, TouchEvent } from './types'
import { ChartContextProvider } from './ChartContext'
import { calculateDataDimensions, calculateViewportDomain } from './Chart.utils'
import { scalePointToDimensions } from './utils'
@@ -32,7 +33,7 @@ type Props = {
padding?: Padding
}

const Chart: React.FC<Props> = (props) => {
const Chart: React.FC<Props> = React.memo((props) => {
const { style, children, data = [], padding, xDomain, yDomain, viewport, disableGestures, disableTouch } = deepmerge(computeDefaultProps(props), props)
const { dimensions, onLayout } = useComponentDimensions()
const dataDimensions = calculateDataDimensions(dimensions, padding)
@@ -183,7 +184,7 @@ const Chart: React.FC<Props> = (props) => {
)}
</View>
)
}
}, fastEqual)

export { Chart }

4 changes: 2 additions & 2 deletions src/HorizontalAxis.tsx
Original file line number Diff line number Diff line change
@@ -48,11 +48,11 @@ const HorizontalAxis: React.FC<Props> = (props) => {
return null
}

// fround is used because of potential float comparison errors, see https://github.com/N1ghtly/react-native-responsive-linechart/issues/53
const finalTickValues = calculateTickValues(tickValues, tickCount, domain.x, includeOriginTick).filter(
(v) => v >= viewportDomain.x.min && v <= viewportDomain.x.max
(v) => Math.fround(v) >= Math.fround(viewportDomain.x.min) && Math.fround(v) <= Math.fround(viewportDomain.x.max)
)

console
return (
<>
{/* Render Axis */}
2 changes: 1 addition & 1 deletion src/Line.tsx
Original file line number Diff line number Diff line change
@@ -109,7 +109,7 @@ const Line = React.forwardRef<LineHandle, Props>(function Line(props, ref) {
return () => {
clearTimeout(tooltipTimer)
}
}, [data, viewportDomain, domain, dimensions, lastTouch, hideTooltipAfter])
}, [lastTouch, hideTooltipAfter])

const scaledPoints = scalePointsToDimensions(data, viewportDomain, dimensions)
const points = adjustPointsForThickStroke(scaledPoints, stroke)
3 changes: 2 additions & 1 deletion src/VerticalAxis.tsx
Original file line number Diff line number Diff line change
@@ -48,8 +48,9 @@ const VerticalAxis: React.FC<Props> = (props) => {
return null
}

// fround is used because of potential float comparison errors, see https://github.com/N1ghtly/react-native-responsive-linechart/issues/53
const finalTickValues = calculateTickValues(tickValues, tickCount, domain.y, includeOriginTick).filter(
(v) => v >= viewportDomain.y.min && v <= viewportDomain.y.max
(v) => Math.fround(v) >= Math.fround(viewportDomain.y.min) && Math.fround(v) <= Math.fround(viewportDomain.y.max)
)

return (
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
@@ -3936,7 +3936,7 @@ fancy-log@^1.3.2:
parse-node-version "^1.0.0"
time-stamp "^1.0.0"

fast-deep-equal@^3.1.1:
fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==

0 comments on commit 8e61d42

Please sign in to comment.