Skip to content

Commit d9461e5

Browse files
author
Christoph Pader
committed
add disableSmoothing prop
1 parent 388b3e6 commit d9461e5

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

Diff for: src/AnimatedLineGraph.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function AnimatedLineGraph({
7171
verticalPadding = lineThickness,
7272
TopAxisLabel,
7373
BottomAxisLabel,
74+
disableSmoothing = false,
7475
...props
7576
}: AnimatedLineGraphProps): React.ReactElement {
7677
const [width, setWidth] = useState(0)
@@ -196,6 +197,7 @@ export function AnimatedLineGraph({
196197
verticalPadding,
197198
canvasHeight: height,
198199
canvasWidth: width,
200+
disableSmoothing,
199201
}
200202

201203
if (shouldFillGradient) {

Diff for: src/CreateGraphPath.ts

+34-21
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ type GraphPathConfig = {
4343
* Range of the graph's x and y-axis
4444
*/
4545
range: GraphPathRange
46+
/**
47+
* Disables smoothing of the graph line to increase accuracy of graph according to the dataset
48+
*/
49+
disableSmoothing: boolean
4650
}
4751

4852
type GraphPathConfigWithGradient = GraphPathConfig & {
@@ -140,6 +144,7 @@ function createGraphPathBase({
140144
canvasHeight: height,
141145
canvasWidth: width,
142146
shouldFillGradient,
147+
disableSmoothing,
143148
}: GraphPathConfigWithGradient | GraphPathConfigWithoutGradient):
144149
| SkPath
145150
| GraphPathWithGradient {
@@ -208,27 +213,35 @@ function createGraphPathBase({
208213
for (let i = 0; i < points.length; i++) {
209214
const point = points[i]!
210215

211-
// first point needs to start the path
212-
if (i === 0) path.moveTo(point.x, point.y)
213-
214-
const prev = points[i - 1]
215-
const prevPrev = points[i - 2]
216-
217-
if (prev == null) continue
218-
219-
const p0 = prevPrev ?? prev
220-
const p1 = prev
221-
const cp1x = (2 * p0.x + p1.x) / 3
222-
const cp1y = (2 * p0.y + p1.y) / 3
223-
const cp2x = (p0.x + 2 * p1.x) / 3
224-
const cp2y = (p0.y + 2 * p1.y) / 3
225-
const cp3x = (p0.x + 4 * p1.x + point.x) / 6
226-
const cp3y = (p0.y + 4 * p1.y + point.y) / 6
227-
228-
path.cubicTo(cp1x, cp1y, cp2x, cp2y, cp3x, cp3y)
229-
230-
if (i === points.length - 1) {
231-
path.cubicTo(point.x, point.y, point.x, point.y, point.x, point.y)
216+
// Start the path or add a line directly to the next point
217+
if (i === 0) {
218+
path.moveTo(point.x, point.y)
219+
} else {
220+
if (disableSmoothing) {
221+
// Direct line to the next point for no smoothing
222+
path.lineTo(point.x, point.y)
223+
} else {
224+
// Continue using smoothing
225+
const prev = points[i - 1]
226+
const prevPrev = points[i - 2]
227+
228+
if (prev == null) continue
229+
230+
const p0 = prevPrev ?? prev
231+
const p1 = prev
232+
const cp1x = (2 * p0.x + p1.x) / 3
233+
const cp1y = (2 * p0.y + p1.y) / 3
234+
const cp2x = (p0.x + 2 * p1.x) / 3
235+
const cp2y = (p0.y + 2 * p1.y) / 3
236+
const cp3x = (p0.x + 4 * p1.x + point.x) / 6
237+
const cp3y = (p0.y + 4 * p1.y + point.y) / 6
238+
239+
path.cubicTo(cp1x, cp1y, cp2x, cp2y, cp3x, cp3y)
240+
241+
if (i === points.length - 1) {
242+
path.cubicTo(point.x, point.y, point.x, point.y, point.x, point.y)
243+
}
244+
}
232245
}
233246
}
234247

Diff for: src/LineGraphProps.ts

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ interface BaseLineGraphProps extends ViewProps {
4747
* Enable the Fade-In Gradient Effect at the beginning of the Graph
4848
*/
4949
enableFadeInMask?: boolean
50+
/**
51+
* Disables smoothing of the graph line to increase accuracy of graph according to the dataset
52+
*/
53+
disableSmoothing?: boolean
5054
}
5155

5256
export type StaticLineGraphProps = BaseLineGraphProps & {

Diff for: src/StaticLineGraph.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function StaticLineGraph({
1616
color,
1717
lineThickness = 3,
1818
enableFadeInMask,
19+
disableSmoothing = false,
1920
style,
2021
...props
2122
}: StaticLineGraphProps): React.ReactElement {
@@ -49,8 +50,9 @@ export function StaticLineGraph({
4950
canvasWidth: width,
5051
horizontalPadding: lineThickness,
5152
verticalPadding: lineThickness,
53+
disableSmoothing,
5254
}),
53-
[height, lineThickness, pathRange, pointsInRange, width]
55+
[disableSmoothing, height, lineThickness, pathRange, pointsInRange, width]
5456
)
5557

5658
const gradientColors = useMemo(

0 commit comments

Comments
 (0)