@@ -43,6 +43,10 @@ type GraphPathConfig = {
43
43
* Range of the graph's x and y-axis
44
44
*/
45
45
range : GraphPathRange
46
+ /**
47
+ * Disables smoothing of the graph line to increase accuracy of graph according to the dataset
48
+ */
49
+ disableSmoothing : boolean
46
50
}
47
51
48
52
type GraphPathConfigWithGradient = GraphPathConfig & {
@@ -140,6 +144,7 @@ function createGraphPathBase({
140
144
canvasHeight : height ,
141
145
canvasWidth : width ,
142
146
shouldFillGradient,
147
+ disableSmoothing,
143
148
} : GraphPathConfigWithGradient | GraphPathConfigWithoutGradient ) :
144
149
| SkPath
145
150
| GraphPathWithGradient {
@@ -208,27 +213,35 @@ function createGraphPathBase({
208
213
for ( let i = 0 ; i < points . length ; i ++ ) {
209
214
const point = points [ i ] !
210
215
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
+ }
232
245
}
233
246
}
234
247
0 commit comments