diff --git a/demo/package.json b/demo/package.json index 1637791..266299a 100644 --- a/demo/package.json +++ b/demo/package.json @@ -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", diff --git a/demo/yarn.lock b/demo/yarn.lock index 99fce82..9bbe1b7 100644 --- a/demo/yarn.lock +++ b/demo/yarn.lock @@ -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== diff --git a/package.json b/package.json index aa2ecda..528c588 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Chart.tsx b/src/Chart.tsx index 6cd4ec7..83c3802 100644 --- a/src/Chart.tsx +++ b/src/Chart.tsx @@ -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) => { +const Chart: React.FC = 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) => { )} ) -} +}, fastEqual) export { Chart } diff --git a/src/HorizontalAxis.tsx b/src/HorizontalAxis.tsx index 7c0963e..8d82c9d 100644 --- a/src/HorizontalAxis.tsx +++ b/src/HorizontalAxis.tsx @@ -48,11 +48,11 @@ const HorizontalAxis: React.FC = (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 */} diff --git a/src/Line.tsx b/src/Line.tsx index 048976d..2745fcb 100644 --- a/src/Line.tsx +++ b/src/Line.tsx @@ -109,7 +109,7 @@ const Line = React.forwardRef(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) diff --git a/src/VerticalAxis.tsx b/src/VerticalAxis.tsx index ec56cf6..7c0cde4 100644 --- a/src/VerticalAxis.tsx +++ b/src/VerticalAxis.tsx @@ -48,8 +48,9 @@ const VerticalAxis: React.FC = (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 ( diff --git a/yarn.lock b/yarn.lock index d587097..8eaaa53 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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==