@obitrain/charts 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +125 -0
- package/lib/commonjs/Charts/BottomAxis/AxisLine.js +38 -0
- package/lib/commonjs/Charts/BottomAxis/AxisLine.js.map +1 -0
- package/lib/commonjs/Charts/BottomAxis/BottomAxis.js +45 -0
- package/lib/commonjs/Charts/BottomAxis/BottomAxis.js.map +1 -0
- package/lib/commonjs/Charts/BottomAxis/Tick.js +43 -0
- package/lib/commonjs/Charts/BottomAxis/Tick.js.map +1 -0
- package/lib/commonjs/Charts/BottomAxis/index.js +27 -0
- package/lib/commonjs/Charts/BottomAxis/index.js.map +1 -0
- package/lib/commonjs/Charts/Cursor.js +50 -0
- package/lib/commonjs/Charts/Cursor.js.map +1 -0
- package/lib/commonjs/Charts/Linechart/index.js +55 -0
- package/lib/commonjs/Charts/Linechart/index.js.map +1 -0
- package/lib/commonjs/Charts/ZoomableLinechart/ScalablePath.js +43 -0
- package/lib/commonjs/Charts/ZoomableLinechart/ScalablePath.js.map +1 -0
- package/lib/commonjs/Charts/ZoomableLinechart/index.js +43 -0
- package/lib/commonjs/Charts/ZoomableLinechart/index.js.map +1 -0
- package/lib/commonjs/Charts/gesture.js +185 -0
- package/lib/commonjs/Charts/gesture.js.map +1 -0
- package/lib/commonjs/Charts/graphUtils.js +131 -0
- package/lib/commonjs/Charts/graphUtils.js.map +1 -0
- package/lib/commonjs/Charts/index.js +151 -0
- package/lib/commonjs/Charts/index.js.map +1 -0
- package/lib/commonjs/Charts/maths.js +195 -0
- package/lib/commonjs/Charts/maths.js.map +1 -0
- package/lib/commonjs/Charts/types.js +6 -0
- package/lib/commonjs/Charts/types.js.map +1 -0
- package/lib/commonjs/dateUtils.js +151 -0
- package/lib/commonjs/dateUtils.js.map +1 -0
- package/lib/commonjs/index.js +39 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/textUtils.js +21 -0
- package/lib/commonjs/textUtils.js.map +1 -0
- package/lib/module/Charts/BottomAxis/AxisLine.js +34 -0
- package/lib/module/Charts/BottomAxis/AxisLine.js.map +1 -0
- package/lib/module/Charts/BottomAxis/BottomAxis.js +40 -0
- package/lib/module/Charts/BottomAxis/BottomAxis.js.map +1 -0
- package/lib/module/Charts/BottomAxis/Tick.js +37 -0
- package/lib/module/Charts/BottomAxis/Tick.js.map +1 -0
- package/lib/module/Charts/BottomAxis/index.js +6 -0
- package/lib/module/Charts/BottomAxis/index.js.map +1 -0
- package/lib/module/Charts/Cursor.js +46 -0
- package/lib/module/Charts/Cursor.js.map +1 -0
- package/lib/module/Charts/Linechart/index.js +50 -0
- package/lib/module/Charts/Linechart/index.js.map +1 -0
- package/lib/module/Charts/ZoomableLinechart/ScalablePath.js +39 -0
- package/lib/module/Charts/ZoomableLinechart/ScalablePath.js.map +1 -0
- package/lib/module/Charts/ZoomableLinechart/index.js +33 -0
- package/lib/module/Charts/ZoomableLinechart/index.js.map +1 -0
- package/lib/module/Charts/gesture.js +176 -0
- package/lib/module/Charts/gesture.js.map +1 -0
- package/lib/module/Charts/graphUtils.js +121 -0
- package/lib/module/Charts/graphUtils.js.map +1 -0
- package/lib/module/Charts/index.js +10 -0
- package/lib/module/Charts/index.js.map +1 -0
- package/lib/module/Charts/maths.js +187 -0
- package/lib/module/Charts/maths.js.map +1 -0
- package/lib/module/Charts/types.js +4 -0
- package/lib/module/Charts/types.js.map +1 -0
- package/lib/module/dateUtils.js +142 -0
- package/lib/module/dateUtils.js.map +1 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/textUtils.js +16 -0
- package/lib/module/textUtils.js.map +1 -0
- package/lib/typescript/src/Charts/BottomAxis/AxisLine.d.ts +15 -0
- package/lib/typescript/src/Charts/BottomAxis/AxisLine.d.ts.map +1 -0
- package/lib/typescript/src/Charts/BottomAxis/BottomAxis.d.ts +17 -0
- package/lib/typescript/src/Charts/BottomAxis/BottomAxis.d.ts.map +1 -0
- package/lib/typescript/src/Charts/BottomAxis/Tick.d.ts +14 -0
- package/lib/typescript/src/Charts/BottomAxis/Tick.d.ts.map +1 -0
- package/lib/typescript/src/Charts/BottomAxis/index.d.ts +4 -0
- package/lib/typescript/src/Charts/BottomAxis/index.d.ts.map +1 -0
- package/lib/typescript/src/Charts/Cursor.d.ts +14 -0
- package/lib/typescript/src/Charts/Cursor.d.ts.map +1 -0
- package/lib/typescript/src/Charts/Linechart/index.d.ts +25 -0
- package/lib/typescript/src/Charts/Linechart/index.d.ts.map +1 -0
- package/lib/typescript/src/Charts/ZoomableLinechart/ScalablePath.d.ts +14 -0
- package/lib/typescript/src/Charts/ZoomableLinechart/ScalablePath.d.ts.map +1 -0
- package/lib/typescript/src/Charts/ZoomableLinechart/index.d.ts +23 -0
- package/lib/typescript/src/Charts/ZoomableLinechart/index.d.ts.map +1 -0
- package/lib/typescript/src/Charts/gesture.d.ts +54 -0
- package/lib/typescript/src/Charts/gesture.d.ts.map +1 -0
- package/lib/typescript/src/Charts/graphUtils.d.ts +43 -0
- package/lib/typescript/src/Charts/graphUtils.d.ts.map +1 -0
- package/lib/typescript/src/Charts/index.d.ts +14 -0
- package/lib/typescript/src/Charts/index.d.ts.map +1 -0
- package/lib/typescript/src/Charts/maths.d.ts +19 -0
- package/lib/typescript/src/Charts/maths.d.ts.map +1 -0
- package/lib/typescript/src/Charts/types.d.ts +14 -0
- package/lib/typescript/src/Charts/types.d.ts.map +1 -0
- package/lib/typescript/src/dateUtils.d.ts +37 -0
- package/lib/typescript/src/dateUtils.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +5 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/textUtils.d.ts +3 -0
- package/lib/typescript/src/textUtils.d.ts.map +1 -0
- package/package.json +226 -0
- package/src/Charts/BottomAxis/AxisLine.tsx +40 -0
- package/src/Charts/BottomAxis/BottomAxis.tsx +41 -0
- package/src/Charts/BottomAxis/Tick.tsx +54 -0
- package/src/Charts/BottomAxis/index.tsx +3 -0
- package/src/Charts/Cursor.tsx +51 -0
- package/src/Charts/Linechart/index.tsx +79 -0
- package/src/Charts/ZoomableLinechart/ScalablePath.tsx +49 -0
- package/src/Charts/ZoomableLinechart/index.tsx +47 -0
- package/src/Charts/gesture.ts +254 -0
- package/src/Charts/graphUtils.ts +198 -0
- package/src/Charts/index.tsx +32 -0
- package/src/Charts/maths.ts +245 -0
- package/src/Charts/types.ts +16 -0
- package/src/dateUtils.ts +163 -0
- package/src/index.tsx +4 -0
- package/src/textUtils.ts +21 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Canvas, Group, Path } from '@shopify/react-native-skia';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
6
|
+
const LineChart = function (props) {
|
|
7
|
+
const {
|
|
8
|
+
width,
|
|
9
|
+
height,
|
|
10
|
+
style,
|
|
11
|
+
path,
|
|
12
|
+
children,
|
|
13
|
+
color,
|
|
14
|
+
paths,
|
|
15
|
+
background,
|
|
16
|
+
offsetX = 0,
|
|
17
|
+
offsetY = 0
|
|
18
|
+
} = props;
|
|
19
|
+
const _paths = paths !== undefined ? paths : path !== undefined ? [{
|
|
20
|
+
path: path,
|
|
21
|
+
color: color ?? 'red'
|
|
22
|
+
}] : [];
|
|
23
|
+
if (_paths.length === 0) {
|
|
24
|
+
console.warn('Specify either a path or multiple paths.');
|
|
25
|
+
return null;
|
|
26
|
+
// throw new Error('Specify either a path or multiple paths.');
|
|
27
|
+
}
|
|
28
|
+
return /*#__PURE__*/_jsx(Canvas, {
|
|
29
|
+
style: [{
|
|
30
|
+
width,
|
|
31
|
+
height
|
|
32
|
+
}, style],
|
|
33
|
+
children: /*#__PURE__*/_jsxs(Group, {
|
|
34
|
+
transform: [{
|
|
35
|
+
translateY: offsetY
|
|
36
|
+
}, {
|
|
37
|
+
translateX: offsetX
|
|
38
|
+
}],
|
|
39
|
+
children: [background, _paths.map((_path, i) => /*#__PURE__*/_jsx(Path, {
|
|
40
|
+
style: "stroke",
|
|
41
|
+
..._path,
|
|
42
|
+
strokeWidth: 2,
|
|
43
|
+
strokeJoin: "round",
|
|
44
|
+
strokeCap: "round"
|
|
45
|
+
}, `${_path.id ?? i}`)), children]
|
|
46
|
+
})
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
export { LineChart };
|
|
50
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Canvas","Group","Path","React","jsx","_jsx","jsxs","_jsxs","LineChart","props","width","height","style","path","children","color","paths","background","offsetX","offsetY","_paths","undefined","length","console","warn","transform","translateY","translateX","map","_path","i","strokeWidth","strokeJoin","strokeCap","id"],"sourceRoot":"../../../../src","sources":["Charts/Linechart/index.tsx"],"mappings":";;AAAA,SAASA,MAAM,EAAEC,KAAK,EAAEC,IAAI,QAAqB,4BAA4B;AAE7E,OAAOC,KAAK,MAAM,OAAO;AAAC,SAAAC,GAAA,IAAAC,IAAA,EAAAC,IAAA,IAAAC,KAAA;AAuB1B,MAAMC,SAA6B,GAAG,SAAAA,CAAUC,KAAK,EAAE;EACrD,MAAM;IACJC,KAAK;IACLC,MAAM;IACNC,KAAK;IACLC,IAAI;IACJC,QAAQ;IACRC,KAAK;IACLC,KAAK;IACLC,UAAU;IACVC,OAAO,GAAG,CAAC;IACXC,OAAO,GAAG;EACZ,CAAC,GAAGV,KAAK;EAET,MAAMW,MAAkB,GACtBJ,KAAK,KAAKK,SAAS,GACfL,KAAK,GACLH,IAAI,KAAKQ,SAAS,GAClB,CAAC;IAAER,IAAI,EAAEA,IAAI;IAAEE,KAAK,EAAEA,KAAK,IAAI;EAAM,CAAC,CAAC,GACvC,EAAE;EACR,IAAIK,MAAM,CAACE,MAAM,KAAK,CAAC,EAAE;IACvBC,OAAO,CAACC,IAAI,CAAC,0CAA0C,CAAC;IACxD,OAAO,IAAI;IACX;EACF;EACA,oBACEnB,IAAA,CAACL,MAAM;IACLY,KAAK,EAAE,CACL;MACEF,KAAK;MACLC;IACF,CAAC,EACDC,KAAK,CACL;IAAAE,QAAA,eAEFP,KAAA,CAACN,KAAK;MAACwB,SAAS,EAAE,CAAC;QAAEC,UAAU,EAAEP;MAAQ,CAAC,EAAE;QAAEQ,UAAU,EAAET;MAAQ,CAAC,CAAE;MAAAJ,QAAA,GAClEG,UAAU,EACVG,MAAM,CAACQ,GAAG,CAAC,CAACC,KAAK,EAAEC,CAAC,kBACnBzB,IAAA,CAACH,IAAI;QAEHU,KAAK,EAAC,QAAQ;QAAA,GACViB,KAAK;QACTE,WAAW,EAAE,CAAE;QACfC,UAAU,EAAC,OAAO;QAClBC,SAAS,EAAC;MAAO,GALZ,GAAGJ,KAAK,CAACK,EAAE,IAAIJ,CAAC,EAMtB,CACF,CAAC,EACDhB,QAAQ;IAAA,CACJ;EAAC,CACF,CAAC;AAEb,CAAC;AAED,SAASN,SAAS","ignoreList":[]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Path, Skia } from '@shopify/react-native-skia';
|
|
4
|
+
import { useDerivedValue } from 'react-native-reanimated';
|
|
5
|
+
import { scaleCommands } from '../graphUtils';
|
|
6
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
7
|
+
const ScalablePath = function (props) {
|
|
8
|
+
const {
|
|
9
|
+
path,
|
|
10
|
+
scale,
|
|
11
|
+
focalX,
|
|
12
|
+
offsetX,
|
|
13
|
+
pathProps,
|
|
14
|
+
color = 'red'
|
|
15
|
+
} = props;
|
|
16
|
+
const animatedPath = useDerivedValue(() => {
|
|
17
|
+
let _cmds = [];
|
|
18
|
+
try {
|
|
19
|
+
_cmds = scaleCommands(path.value?.toCmds() ?? [], scale, focalX, offsetX);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
console.error('Got error while scaling path: ', typeof e);
|
|
22
|
+
_cmds = [];
|
|
23
|
+
}
|
|
24
|
+
const _path = Skia.Path.MakeFromCmds(_cmds);
|
|
25
|
+
if (!_path) throw new Error('Path is null');
|
|
26
|
+
return _path;
|
|
27
|
+
}, [scale]);
|
|
28
|
+
return /*#__PURE__*/_jsx(Path, {
|
|
29
|
+
style: "stroke",
|
|
30
|
+
path: animatedPath,
|
|
31
|
+
strokeWidth: 2,
|
|
32
|
+
strokeJoin: "round",
|
|
33
|
+
strokeCap: "round",
|
|
34
|
+
color: color,
|
|
35
|
+
...pathProps
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
export { ScalablePath };
|
|
39
|
+
//# sourceMappingURL=ScalablePath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Path","Skia","useDerivedValue","scaleCommands","jsx","_jsx","ScalablePath","props","path","scale","focalX","offsetX","pathProps","color","animatedPath","_cmds","value","toCmds","e","console","error","_path","MakeFromCmds","Error","style","strokeWidth","strokeJoin","strokeCap"],"sourceRoot":"../../../../src","sources":["Charts/ZoomableLinechart/ScalablePath.tsx"],"mappings":";;AAAA,SACEA,IAAI,EACJC,IAAI,QAGC,4BAA4B;AAEnC,SAASC,eAAe,QAA0B,yBAAyB;AAC3E,SAASC,aAAa,QAAQ,eAAe;AAAC,SAAAC,GAAA,IAAAC,IAAA;AAW9C,MAAMC,YAAmC,GAAG,SAAAA,CAAUC,KAAK,EAAE;EAC3D,MAAM;IAAEC,IAAI;IAAEC,KAAK;IAAEC,MAAM;IAAEC,OAAO;IAAEC,SAAS;IAAEC,KAAK,GAAG;EAAM,CAAC,GAAGN,KAAK;EAExE,MAAMO,YAAY,GAAGZ,eAAe,CAAC,MAAM;IACzC,IAAIa,KAAoB,GAAG,EAAE;IAC7B,IAAI;MACFA,KAAK,GAAGZ,aAAa,CAACK,IAAI,CAACQ,KAAK,EAAEC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAER,KAAK,EAAEC,MAAM,EAAEC,OAAO,CAAC;IAC3E,CAAC,CAAC,OAAOO,CAAC,EAAE;MACVC,OAAO,CAACC,KAAK,CAAC,gCAAgC,EAAE,OAAOF,CAAC,CAAC;MACzDH,KAAK,GAAG,EAAE;IACZ;IACA,MAAMM,KAAK,GAAGpB,IAAI,CAACD,IAAI,CAACsB,YAAY,CAACP,KAAK,CAAC;IAC3C,IAAI,CAACM,KAAK,EAAE,MAAM,IAAIE,KAAK,CAAC,cAAc,CAAC;IAC3C,OAAOF,KAAK;EACd,CAAC,EAAE,CAACZ,KAAK,CAAC,CAAC;EAEX,oBACEJ,IAAA,CAACL,IAAI;IACHwB,KAAK,EAAC,QAAQ;IACdhB,IAAI,EAAEM,YAAa;IACnBW,WAAW,EAAE,CAAE;IACfC,UAAU,EAAC,OAAO;IAClBC,SAAS,EAAC,OAAO;IACjBd,KAAK,EAAEA,KAAM;IAAA,GACTD;EAAS,CACd,CAAC;AAEN,CAAC;AAED,SAASN,YAAY","ignoreList":[]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Canvas, Group } from '@shopify/react-native-skia';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { ScalablePath } from './ScalablePath';
|
|
6
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
7
|
+
export { ScalablePath };
|
|
8
|
+
const ZoomableLineChart = function (props) {
|
|
9
|
+
const {
|
|
10
|
+
width,
|
|
11
|
+
height,
|
|
12
|
+
style,
|
|
13
|
+
children,
|
|
14
|
+
offsetY = 0,
|
|
15
|
+
...rest
|
|
16
|
+
} = props;
|
|
17
|
+
return /*#__PURE__*/_jsx(Canvas, {
|
|
18
|
+
style: [{
|
|
19
|
+
width,
|
|
20
|
+
height
|
|
21
|
+
}, style],
|
|
22
|
+
children: /*#__PURE__*/_jsxs(Group, {
|
|
23
|
+
transform: [{
|
|
24
|
+
translateY: offsetY
|
|
25
|
+
}],
|
|
26
|
+
children: [/*#__PURE__*/_jsx(ScalablePath, {
|
|
27
|
+
...rest
|
|
28
|
+
}), children]
|
|
29
|
+
})
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
export { ZoomableLineChart };
|
|
33
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Canvas","Group","React","ScalablePath","jsx","_jsx","jsxs","_jsxs","ZoomableLineChart","props","width","height","style","children","offsetY","rest","transform","translateY"],"sourceRoot":"../../../../src","sources":["Charts/ZoomableLinechart/index.tsx"],"mappings":";;AAAA,SAASA,MAAM,EAAEC,KAAK,QAAqB,4BAA4B;AAEvE,OAAOC,KAAK,MAAM,OAAO;AAIzB,SAASC,YAAY,QAAgC,gBAAgB;AAAC,SAAAC,GAAA,IAAAC,IAAA,EAAAC,IAAA,IAAAC,KAAA;AAEtE,SAASJ,YAAY;AAiBrB,MAAMK,iBAA6C,GAAG,SAAAA,CAAUC,KAAK,EAAE;EACrE,MAAM;IAAEC,KAAK;IAAEC,MAAM;IAAEC,KAAK;IAAEC,QAAQ;IAAEC,OAAO,GAAG,CAAC;IAAE,GAAGC;EAAK,CAAC,GAAGN,KAAK;EAEtE,oBACEJ,IAAA,CAACL,MAAM;IACLY,KAAK,EAAE,CACL;MACEF,KAAK;MACLC;IACF,CAAC,EACDC,KAAK,CACL;IAAAC,QAAA,eAEFN,KAAA,CAACN,KAAK;MAACe,SAAS,EAAE,CAAC;QAAEC,UAAU,EAAEH;MAAQ,CAAC,CAAE;MAAAD,QAAA,gBAC1CR,IAAA,CAACF,YAAY;QAAA,GAAKY;MAAI,CAAG,CAAC,EACzBF,QAAQ;IAAA,CACJ;EAAC,CACF,CAAC;AAEb,CAAC;AAED,SAASL,iBAAiB","ignoreList":[]}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { clamp } from '@shopify/react-native-skia';
|
|
4
|
+
import { useCallback } from 'react';
|
|
5
|
+
import { Gesture } from 'react-native-gesture-handler';
|
|
6
|
+
import { runOnJS, useAnimatedReaction, useDerivedValue, useSharedValue, withTiming } from 'react-native-reanimated';
|
|
7
|
+
/**
|
|
8
|
+
* Get the position of a point on the axis,
|
|
9
|
+
* given the current focal, scale and offset
|
|
10
|
+
*/
|
|
11
|
+
export const getPositionWl = function (position, focalX, scale, offsetX) {
|
|
12
|
+
'worklet';
|
|
13
|
+
|
|
14
|
+
return (position - focalX) * scale + focalX + offsetX;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Return utilities for zooming and panning the axis
|
|
18
|
+
*/
|
|
19
|
+
export const useScalableGesture = props => {
|
|
20
|
+
const {
|
|
21
|
+
width
|
|
22
|
+
} = props;
|
|
23
|
+
const startOffset = props.startOffset ?? 0;
|
|
24
|
+
const startScale = props.startScale ?? 1;
|
|
25
|
+
// For zooming
|
|
26
|
+
const scale = useSharedValue(startScale);
|
|
27
|
+
const lastScale = useSharedValue(startScale);
|
|
28
|
+
const focalX = useSharedValue(0);
|
|
29
|
+
const lastFocalX = useSharedValue(0);
|
|
30
|
+
|
|
31
|
+
// For panning
|
|
32
|
+
const offsetX = useSharedValue(startOffset);
|
|
33
|
+
const lastOffsetX = useSharedValue(startOffset);
|
|
34
|
+
const reset = useCallback(() => {
|
|
35
|
+
scale.value = startScale;
|
|
36
|
+
lastScale.value = startScale;
|
|
37
|
+
focalX.value = 0;
|
|
38
|
+
lastFocalX.value = 0;
|
|
39
|
+
offsetX.value = startOffset;
|
|
40
|
+
lastOffsetX.value = startOffset;
|
|
41
|
+
}, [focalX, lastFocalX, lastOffsetX, lastScale, offsetX, scale, startOffset, startScale]);
|
|
42
|
+
const animateFocalPoint = newFocalX => {
|
|
43
|
+
'worklet';
|
|
44
|
+
|
|
45
|
+
focalX.value = withTiming(newFocalX, {
|
|
46
|
+
duration: 300
|
|
47
|
+
}); // Smooth transition with duration
|
|
48
|
+
};
|
|
49
|
+
const panGesture = Gesture.Pan().onUpdate(event => {
|
|
50
|
+
const newOffsetX = lastOffsetX.value + event.translationX;
|
|
51
|
+
offsetX.value = newOffsetX;
|
|
52
|
+
}).onEnd(() => {
|
|
53
|
+
lastScale.value = scale.value;
|
|
54
|
+
lastFocalX.value = focalX.value;
|
|
55
|
+
const leftBound = getPositionWl(0, focalX.value, scale.value, offsetX.value);
|
|
56
|
+
const rightBound = getPositionWl(width, focalX.value, scale.value, offsetX.value);
|
|
57
|
+
let newOffset;
|
|
58
|
+
if (leftBound > startOffset) {
|
|
59
|
+
// console.log(`Left Boundary reached`);
|
|
60
|
+
newOffset = offsetX.value - leftBound + startOffset;
|
|
61
|
+
} else if (rightBound < width) {
|
|
62
|
+
// console.log(`Right Boundary reached`);
|
|
63
|
+
newOffset = offsetX.value + width - rightBound + startOffset;
|
|
64
|
+
} else {
|
|
65
|
+
newOffset = offsetX.value;
|
|
66
|
+
}
|
|
67
|
+
if (newOffset === undefined) throw new Error('newOffset is undefined');
|
|
68
|
+
offsetX.value = withTiming(newOffset, {
|
|
69
|
+
duration: 300
|
|
70
|
+
});
|
|
71
|
+
lastOffsetX.value = newOffset;
|
|
72
|
+
});
|
|
73
|
+
const pinchGesture = Gesture.Pinch().onUpdate(event => {
|
|
74
|
+
const newScale = lastScale.value * event.scale;
|
|
75
|
+
scale.value = clamp(newScale, 1, Infinity); // Clamp the scale value
|
|
76
|
+
animateFocalPoint(event.focalX);
|
|
77
|
+
}).onEnd(() => {
|
|
78
|
+
lastScale.value = scale.value;
|
|
79
|
+
lastFocalX.value = focalX.value;
|
|
80
|
+
});
|
|
81
|
+
return {
|
|
82
|
+
scale,
|
|
83
|
+
focalX,
|
|
84
|
+
pinchGesture,
|
|
85
|
+
panGesture,
|
|
86
|
+
offsetX,
|
|
87
|
+
reset
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
export const useUpdateAxis = function (props) {
|
|
91
|
+
const {
|
|
92
|
+
scale,
|
|
93
|
+
scales,
|
|
94
|
+
onScaleChange
|
|
95
|
+
} = props;
|
|
96
|
+
const currentIndex = useSharedValue(0);
|
|
97
|
+
useAnimatedReaction(() => scale.value, (currentScale, _) => {
|
|
98
|
+
for (let i = 0; i < scales.length; i++) {
|
|
99
|
+
const _prevScale = scales[i - 1] ?? 0;
|
|
100
|
+
const _curScale = scales[i];
|
|
101
|
+
if (_curScale === undefined || _prevScale === undefined) throw new Error('Got undefined scale');
|
|
102
|
+
if (currentScale >= _prevScale && currentScale < _curScale && currentIndex.value !== i) {
|
|
103
|
+
currentIndex.value = i;
|
|
104
|
+
if (onScaleChange !== undefined) runOnJS(onScaleChange)(i);
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}, []);
|
|
109
|
+
return {
|
|
110
|
+
currentIndex
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Get the closest point for a given x value
|
|
115
|
+
*/
|
|
116
|
+
export const getClosestPoint = function (x, dataPoints) {
|
|
117
|
+
'worklet';
|
|
118
|
+
|
|
119
|
+
let closestPoint = dataPoints[0];
|
|
120
|
+
if (closestPoint === undefined) throw new Error('dataPoints array cannot be empty');
|
|
121
|
+
let minDistance = Math.abs(x - closestPoint.x);
|
|
122
|
+
for (let i = 1; i < dataPoints.length; i++) {
|
|
123
|
+
const point = dataPoints[i];
|
|
124
|
+
if (point === undefined) throw new Error('Point cannot be undefined');
|
|
125
|
+
const distance = Math.abs(x - point.x);
|
|
126
|
+
if (distance < minDistance) {
|
|
127
|
+
minDistance = distance;
|
|
128
|
+
closestPoint = point;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return closestPoint;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Get gesture utilities for the cursor
|
|
136
|
+
*/
|
|
137
|
+
export const useCursorGesture = function (props) {
|
|
138
|
+
const {
|
|
139
|
+
width,
|
|
140
|
+
height,
|
|
141
|
+
points,
|
|
142
|
+
closestDataPoint,
|
|
143
|
+
isContinuous = true
|
|
144
|
+
} = props;
|
|
145
|
+
const xPosition = useSharedValue(0);
|
|
146
|
+
const yPosition = useSharedValue(height); // GRAPH_HEIGHT
|
|
147
|
+
|
|
148
|
+
const panGesture = Gesture.Pan().onBegin(event => {
|
|
149
|
+
xPosition.value = clamp(event.x, 0, width);
|
|
150
|
+
yPosition.value = clamp(event.y, 0, height);
|
|
151
|
+
}).onUpdate(event => {
|
|
152
|
+
xPosition.value = clamp(event.x, 0, width);
|
|
153
|
+
yPosition.value = clamp(event.y, 0, height);
|
|
154
|
+
});
|
|
155
|
+
const tapGesture = Gesture.Tap().onBegin(event => {
|
|
156
|
+
xPosition.value = clamp(event.x, 0, width);
|
|
157
|
+
yPosition.value = clamp(event.y, 0, height);
|
|
158
|
+
});
|
|
159
|
+
useDerivedValue(() => {
|
|
160
|
+
if (isContinuous) return;
|
|
161
|
+
if (points === undefined) {
|
|
162
|
+
console.warn('Points must be defined for non-continuous mode');
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const _closestDot = getClosestPoint(xPosition.value, points.value);
|
|
166
|
+
if (closestDataPoint !== undefined) closestDataPoint.value = _closestDot;
|
|
167
|
+
xPosition.value = _closestDot.x;
|
|
168
|
+
}, [xPosition, isContinuous]);
|
|
169
|
+
return {
|
|
170
|
+
panGesture,
|
|
171
|
+
tapGesture,
|
|
172
|
+
xPosition,
|
|
173
|
+
yPosition
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
//# sourceMappingURL=gesture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["clamp","useCallback","Gesture","runOnJS","useAnimatedReaction","useDerivedValue","useSharedValue","withTiming","getPositionWl","position","focalX","scale","offsetX","useScalableGesture","props","width","startOffset","startScale","lastScale","lastFocalX","lastOffsetX","reset","value","animateFocalPoint","newFocalX","duration","panGesture","Pan","onUpdate","event","newOffsetX","translationX","onEnd","leftBound","rightBound","newOffset","undefined","Error","pinchGesture","Pinch","newScale","Infinity","useUpdateAxis","scales","onScaleChange","currentIndex","currentScale","_","i","length","_prevScale","_curScale","getClosestPoint","x","dataPoints","closestPoint","minDistance","Math","abs","point","distance","useCursorGesture","height","points","closestDataPoint","isContinuous","xPosition","yPosition","onBegin","y","tapGesture","Tap","console","warn","_closestDot"],"sourceRoot":"../../../src","sources":["Charts/gesture.ts"],"mappings":";;AAAA,SAASA,KAAK,QAAQ,4BAA4B;AAClD,SAASC,WAAW,QAAQ,OAAO;AACnC,SAASC,OAAO,QAA4C,8BAA8B;AAC1F,SACIC,OAAO,EACPC,mBAAmB,EACnBC,eAAe,EACfC,cAAc,EACdC,UAAU,QAEP,yBAAyB;AAGhC;AACA;AACA;AACA;AACA,OAAO,MAAMC,aAAa,GAAG,SAAAA,CAC3BC,QAAgB,EAChBC,MAAc,EACdC,KAAa,EACbC,OAAe,EACf;EACA,SAAS;;EACT,OAAO,CAACH,QAAQ,GAAGC,MAAM,IAAIC,KAAK,GAAGD,MAAM,GAAGE,OAAO;AACvD,CAAC;AAiBD;AACA;AACA;AACA,OAAO,MAAMC,kBAAkB,GAAIC,KAAuB,IAAsB;EAC9E,MAAM;IAAEC;EAAM,CAAC,GAAGD,KAAK;EACvB,MAAME,WAAW,GAAGF,KAAK,CAACE,WAAW,IAAI,CAAC;EAC1C,MAAMC,UAAU,GAAGH,KAAK,CAACG,UAAU,IAAI,CAAC;EACxC;EACA,MAAMN,KAAK,GAAGL,cAAc,CAACW,UAAU,CAAC;EACxC,MAAMC,SAAS,GAAGZ,cAAc,CAACW,UAAU,CAAC;EAC5C,MAAMP,MAAM,GAAGJ,cAAc,CAAC,CAAC,CAAC;EAChC,MAAMa,UAAU,GAAGb,cAAc,CAAC,CAAC,CAAC;;EAEpC;EACA,MAAMM,OAAO,GAAGN,cAAc,CAACU,WAAW,CAAC;EAC3C,MAAMI,WAAW,GAAGd,cAAc,CAACU,WAAW,CAAC;EAE/C,MA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MQ,UAAU,GAAGpE,OAAO,CAACqE,GAAG,CAAC,CAAC,CAACH,OAAO,CAAEvC,KAAK,IAAK;IAClDqC,SAAS,CAAC5C,KAAK,GAAGtB,KAAK,CAAC6B,KAAK,CAACwB,CAAC,EAAE,CAAC,EAAEtC,KAAK,CAAC;IAC1CoD,SAAS,CAAC7C,KAAK,GAAGtB,KAAK,CAAC6B,KAAK,CAACwC,CAAC,EAAE,CAAC,EAAEP,MAAM,CAAC;EAC7C,CAAC,CAAC;EAEFzD,eAAe,CAAC,MAAM;IACpB,IAAI4D,YAAY,EAAE;IAClB,IAAIF,MAAM,KAAK3B,SAAS,EAAE;MACxBoC,OAAO,CAACC,IAAI,CAAC,gDAAgD,CAAC;MAC9D;IACF;IACA,MAAMC,WAAW,GAAGtB,eAAe,CAACc,SAAS,CAAC5C,KAAK,EAAEyC,MAAM,CAACzC,KAAK,CAAC;IAClE,IAAI0C,gBAAgB,KAAK5B,SAAS,EAAE4B,gBAAgB,CAAC1C,KAAK,GAAGoD,WAAW;IACxER,SAAS,CAAC5C,KAAK,GAAGoD,WAAW,CAACrB,CAAC;EACjC,CAAC,EAAE,CAACa,SAAS,EAAED,YAAY,CAAC,CAAC;EAE7B,OAAO;IAAEvC,UAAU;IAAE4C,UAAU;IAAEJ,SAAS;IAAEC;EAAU,CAAC;AACzD,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { PathVerb, Skia } from '@shopify/react-native-skia';
|
|
4
|
+
import { scaleLinear } from 'd3-scale';
|
|
5
|
+
import * as shape from 'd3-shape';
|
|
6
|
+
import { useAnimatedReaction, withTiming } from 'react-native-reanimated';
|
|
7
|
+
import { getPositionWl } from './gesture';
|
|
8
|
+
import { getYForX } from './maths';
|
|
9
|
+
export const buildGraph = function (data, width, height, config) {
|
|
10
|
+
const minX = config?.minX ?? Math.min(...data.map(x => x[0]));
|
|
11
|
+
const maxX = config?.maxX ?? Math.max(...data.map(x => x[0]));
|
|
12
|
+
const scaleX = scaleLinear().domain([minX, maxX]).range([0, width]);
|
|
13
|
+
const minY = config?.minY ?? Math.min(...data.map(x => x[1]));
|
|
14
|
+
const maxY = config?.maxY ?? Math.max(...data.map(x => x[1]));
|
|
15
|
+
const scaleY = scaleLinear().domain([minY, maxY]).range([height, 0]);
|
|
16
|
+
const fmtValues = data.map(x => [x[1], x[0]]);
|
|
17
|
+
const path = shape.line().x(([, x]) => scaleX(x)).y(([y]) => scaleY(y)).curve(config?.curve ?? shape.curveBasis)(fmtValues);
|
|
18
|
+
const dataPoints = fmtValues.map(([y, x]) => ({
|
|
19
|
+
x: scaleX(x),
|
|
20
|
+
y: scaleY(y),
|
|
21
|
+
value: y
|
|
22
|
+
}));
|
|
23
|
+
const skiaPath = Skia.Path.MakeFromSVGString(path);
|
|
24
|
+
if (skiaPath == null) throw new Error('Path not found');
|
|
25
|
+
return {
|
|
26
|
+
data: fmtValues,
|
|
27
|
+
minY,
|
|
28
|
+
maxY,
|
|
29
|
+
path,
|
|
30
|
+
dataPoints,
|
|
31
|
+
skiaPath,
|
|
32
|
+
scaleX,
|
|
33
|
+
scaleY
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* For now, only support commands of type:
|
|
39
|
+
* - PathVerb.Move
|
|
40
|
+
* - PathVerb.Cubic
|
|
41
|
+
* - PathVerb.Quad
|
|
42
|
+
* - PathVerb.Line
|
|
43
|
+
*/
|
|
44
|
+
export const scaleCommands = function (commands, scaleX, focalX, offsetX, scaleY) {
|
|
45
|
+
'worklet';
|
|
46
|
+
|
|
47
|
+
const _scaleY = scaleY ? scaleY.value : 1;
|
|
48
|
+
return commands.map(command => {
|
|
49
|
+
const commandType = command[0];
|
|
50
|
+
if (commandType === undefined) throw new Error('Got undefined command type');
|
|
51
|
+
if (commandType === PathVerb.Line) {
|
|
52
|
+
return [commandType, getPositionWl(command[1], focalX.value, scaleX.value, offsetX.value), command[2] * _scaleY];
|
|
53
|
+
}
|
|
54
|
+
if (commandType === PathVerb.Move) {
|
|
55
|
+
return [commandType, getPositionWl(command[1], focalX.value, scaleX.value, offsetX.value), command[2] * _scaleY];
|
|
56
|
+
}
|
|
57
|
+
if (commandType === PathVerb.Quad) return [commandType, getPositionWl(command[1], focalX.value, scaleX.value, offsetX.value), command[2] * _scaleY, getPositionWl(command[3], focalX.value, scaleX.value, offsetX.value), command[4] * _scaleY];
|
|
58
|
+
if (commandType === PathVerb.Cubic) {
|
|
59
|
+
return [commandType,
|
|
60
|
+
// c1
|
|
61
|
+
getPositionWl(command[1], focalX.value, scaleX.value, offsetX.value), command[2] * _scaleY,
|
|
62
|
+
// c2
|
|
63
|
+
getPositionWl(command[3], focalX.value, scaleX.value, offsetX.value), command[4] * _scaleY,
|
|
64
|
+
// to
|
|
65
|
+
getPositionWl(command[5], focalX.value, scaleX.value, offsetX.value), command[6] * _scaleY];
|
|
66
|
+
}
|
|
67
|
+
if (commandType === PathVerb.Close) return command;
|
|
68
|
+
if (commandType === PathVerb.Move) return command;
|
|
69
|
+
throw new Error(`Unsupported command type ${commandType}`);
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
export const defaultOpacityTransitionWl = function (opacity) {
|
|
73
|
+
'worklet';
|
|
74
|
+
|
|
75
|
+
return withTiming(opacity, {
|
|
76
|
+
duration: 200
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
export const defaultTranslateTransitionWl = function (position) {
|
|
80
|
+
'worklet';
|
|
81
|
+
|
|
82
|
+
return withTiming(position, {
|
|
83
|
+
duration: 1000
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
export const useDotsTransition = function (props) {
|
|
87
|
+
const {
|
|
88
|
+
currentGraph,
|
|
89
|
+
path,
|
|
90
|
+
dataPoints,
|
|
91
|
+
dots,
|
|
92
|
+
opacityWl = defaultOpacityTransitionWl,
|
|
93
|
+
translateWl = defaultTranslateTransitionWl
|
|
94
|
+
} = props;
|
|
95
|
+
useAnimatedReaction(() => ({
|
|
96
|
+
_currentGraph: currentGraph.value,
|
|
97
|
+
_currentCommands: path.value.toCmds()
|
|
98
|
+
}), ({
|
|
99
|
+
_currentGraph,
|
|
100
|
+
_currentCommands
|
|
101
|
+
}) => {
|
|
102
|
+
const newDataPoints = dataPoints[_currentGraph];
|
|
103
|
+
if (newDataPoints === undefined) throw new Error('Data points cannot be undefined');
|
|
104
|
+
dots.map((_dot, i) => {
|
|
105
|
+
const _newDot = newDataPoints[i];
|
|
106
|
+
if (!_newDot) {
|
|
107
|
+
_dot.opacity.value = opacityWl(0);
|
|
108
|
+
return;
|
|
109
|
+
} else {
|
|
110
|
+
_dot.x.value = translateWl(_newDot.x);
|
|
111
|
+
const newY = getYForX(_currentCommands, _dot.x.value);
|
|
112
|
+
if (newY !== undefined) {
|
|
113
|
+
_dot.y.value = newY;
|
|
114
|
+
}
|
|
115
|
+
// _dot.y.value = withTiming(newDots[i]!.y, { duration: 1000 });
|
|
116
|
+
_dot.opacity.value = opacityWl(1);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
//# sourceMappingURL=graphUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["PathVerb","Skia","scaleLinear","shape","useAnimatedReaction","withTiming","getPositionWl","getYForX","buildGraph","data","width","height","config","minX","Math","min","map","x","maxX","max","scaleX","domain","range","minY","maxY","scaleY","fmtValues","path","line","y","curve","curveBasis","dataPoints","value","skiaPath","Path","MakeFromSVGString","Error","scaleCommands","commands","focalX","offsetX","_scaleY","command","commandType","undefined","Line","Move","Quad","Cubic","Close","defaultOpacityTransitionWl","opacity","duration","defaultTranslateTransitionWl","position","useDotsTransition","props","currentGraph","dots","opacityWl","translateWl","_currentGraph","_currentCommands","toCmds","newDataPoints","_dot","i","_newDot","newY"],"sourceRoot":"../../../src","sources":["Charts/graphUtils.ts"],"mappings":";;AAAA,S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I;IACJC,SAAS,GAAGT,0BAA0B;IACtCU,WAAW,GAAGP;EAChB,CAAC,GAAGG,KAAK;EAETrD,mBAAmB,CACjB,OAAO;IACL0D,aAAa,EAAEJ,YAAY,CAACzB,KAAK;IACjC8B,gBAAgB,EAAEpC,IAAI,CAACM,KAAK,CAAC+B,MAAM,CAAC;EACtC,CAAC,CAAC,EACF,CAAC;IAAEF,aAAa;IAAEC;EAAiB,CAAC,KAAK;IACvC,MAAME,aAAa,GAAGjC,UAAU,CAAC8B,aAAa,CAAC;IAC/C,IAAIG,aAAa,KAAKpB,SAAS,EAC7B,MAAM,IAAIR,KAAK,CAAC,iCAAiC,CAAC;IACpDsB,IAAI,CAAC3C,GAAG,CAAC,CAACkD,IAAI,EAAEC,CAAC,KAAK;MACpB,MAAMC,OAAO,GAAGH,aAAa,CAACE,CAAC,CAAC;MAChC,IAAI,CAACC,OAAO,EAAE;QACZF,IAAI,CAACd,OAAO,CAACnB,KAAK,GAAG2B,SAAS,CAAC,CAAC,CAAC;QACjC;MACF,CAAC,MAAM;QACLM,IAAI,CAACjD,CAAC,CAACgB,KAAK,GAAG4B,WAAW,CAACO,OAAO,CAACnD,CAAC,CAAC;QACrC,MAAMoD,IAAI,GAAG9D,QAAQ,CAACwD,gBAAgB,EAAEG,IAAI,CAACjD,CAAC,CAACgB,KAAK,CAAC;QACrD,IAAIoC,IAAI,KAAKxB,SAAS,EAAE;UACtBqB,IAAI,CAACrC,CAAC,CAACI,KAAK,GAAGoC,IAAI;QACrB;QACA;QACAH,IAAI,CAACd,OAAO,CAACnB,KAAK,GAAG2B,SAAS,CAAC,CAAC,CAAC;MACnC;IACF,CAAC,CAAC;EACJ,CACF,CAAC;AACH,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
export { AxisLine, BottomAxis, Tick } from './BottomAxis';
|
|
4
|
+
export { Cursor } from './Cursor';
|
|
5
|
+
export { LineChart } from './Linechart';
|
|
6
|
+
export { ScalablePath, ZoomableLineChart } from './ZoomableLinechart';
|
|
7
|
+
export { getClosestPoint, getPositionWl, useCursorGesture, useScalableGesture, useUpdateAxis } from './gesture';
|
|
8
|
+
export { buildGraph, defaultOpacityTransitionWl, defaultTranslateTransitionWl, scaleCommands, useDotsTransition } from './graphUtils';
|
|
9
|
+
export { commandsToBezier, cubicBezierYForX, getYForX, magnitude, normalize, selectCurve } from './maths';
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["AxisLine","BottomAxis","Tick","Cursor","LineChart","ScalablePath","ZoomableLineChart","getClosestPoint","getPositionWl","useCursorGesture","useScalableGesture","useUpdateAxis","buildGraph","defaultOpacityTransitionWl","defaultTranslateTransitionWl","scaleCommands","useDotsTransition","commandsToBezier","cubicBezierYForX","getYForX","magnitude","normalize","selectCurve"],"sourceRoot":"../../../src","sources":["Charts/index.tsx"],"mappings":";;AAAA,SAASA,QAAQ,EAAEC,UAAU,EAAEC,IAAI,QAAQ,cAAc;AACzD,SAASC,MAAM,QAAQ,UAAU;AAEjC,SAASC,SAAS,QAAQ,aAAa;AAEvC,SAASC,YAAY,EAAEC,iBAAiB,QAAQ,qBAAqB;AAErE,SACEC,eAAe,EACfC,aAAa,EACbC,gBAAgB,EAChBC,kBAAkB,EAClBC,aAAa,QACR,WAAW;AAElB,SACEC,UAAU,EACVC,0BAA0B,EAC1BC,4BAA4B,EAC5BC,aAAa,EACbC,iBAAiB,QACZ,cAAc;AAErB,SACEC,gBAAgB,EAChBC,gBAAgB,EAChBC,QAAQ,EACRC,SAAS,EACTC,SAAS,EACTC,WAAW,QACN,SAAS","ignoreList":[]}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { PathVerb, vec } from '@shopify/react-native-skia';
|
|
4
|
+
|
|
5
|
+
// code from William Candillon
|
|
6
|
+
|
|
7
|
+
const round = (value, precision = 0) => {
|
|
8
|
+
'worklet';
|
|
9
|
+
|
|
10
|
+
const p = Math.pow(10, precision);
|
|
11
|
+
return Math.round(value * p) / p;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// https://stackoverflow.com/questions/27176423/function-to-solve-cubic-equation-analytically
|
|
15
|
+
const cuberoot = x => {
|
|
16
|
+
'worklet';
|
|
17
|
+
|
|
18
|
+
const y = Math.pow(Math.abs(x), 1 / 3);
|
|
19
|
+
return x < 0 ? -y : y;
|
|
20
|
+
};
|
|
21
|
+
const solveCubic = (a, b, c, d) => {
|
|
22
|
+
'worklet';
|
|
23
|
+
|
|
24
|
+
if (Math.abs(a) < 1e-8) {
|
|
25
|
+
// Quadratic case, ax^2+bx+c=0
|
|
26
|
+
a = b;
|
|
27
|
+
b = c;
|
|
28
|
+
c = d;
|
|
29
|
+
if (Math.abs(a) < 1e-8) {
|
|
30
|
+
// Linear case, ax+b=0
|
|
31
|
+
a = b;
|
|
32
|
+
b = c;
|
|
33
|
+
if (Math.abs(a) < 1e-8) {
|
|
34
|
+
// Degenerate case
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
return [-b / a];
|
|
38
|
+
}
|
|
39
|
+
const D = b * b - 4 * a * c;
|
|
40
|
+
if (Math.abs(D) < 1e-8) return [-b / (2 * a)];
|
|
41
|
+
if (D > 0) return [(-b + Math.sqrt(D)) / (2 * a), (-b - Math.sqrt(D)) / (2 * a)];
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Convert to depressed cubic t^3+pt+q = 0 (subst x = t - b/3a)
|
|
46
|
+
const p = (3 * a * c - b * b) / (3 * a * a);
|
|
47
|
+
const q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a);
|
|
48
|
+
let roots = [];
|
|
49
|
+
if (Math.abs(p) < 1e-8) {
|
|
50
|
+
// p = 0 -> t^3 = -q -> t = -q^1/3
|
|
51
|
+
roots = [cuberoot(-q)];
|
|
52
|
+
} else if (Math.abs(q) < 1e-8) {
|
|
53
|
+
// q = 0 -> t^3 + pt = 0 -> t(t^2+p)=0
|
|
54
|
+
roots = [0].concat(p < 0 ? [Math.sqrt(-p), -Math.sqrt(-p)] : []);
|
|
55
|
+
} else {
|
|
56
|
+
const D = q * q / 4 + p * p * p / 27;
|
|
57
|
+
if (Math.abs(D) < 1e-8) {
|
|
58
|
+
// D = 0 -> two roots
|
|
59
|
+
roots = [-1.5 * q / p, 3 * q / p];
|
|
60
|
+
} else if (D > 0) {
|
|
61
|
+
// Only one real root
|
|
62
|
+
const u = cuberoot(-q / 2 - Math.sqrt(D));
|
|
63
|
+
roots = [u - p / (3 * u)];
|
|
64
|
+
} else {
|
|
65
|
+
// D < 0, three roots, but needs to use complex numbers/trigonometric solution
|
|
66
|
+
const u = 2 * Math.sqrt(-p / 3);
|
|
67
|
+
const t = Math.acos(3 * q / p / u) / 3; // D < 0 implies p < 0 and acos argument in [-1..1]
|
|
68
|
+
const k = 2 * Math.PI / 3;
|
|
69
|
+
roots = [u * Math.cos(t), u * Math.cos(t - k), u * Math.cos(t - 2 * k)];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Convert back from depressed cubic
|
|
74
|
+
for (let i = 0; i < roots.length; i++) roots[i] -= b / (3 * a);
|
|
75
|
+
return roots;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// const cubicBezier = (
|
|
79
|
+
// t: number,
|
|
80
|
+
// from: number,
|
|
81
|
+
// c1: number,
|
|
82
|
+
// c2: number,
|
|
83
|
+
// to: number
|
|
84
|
+
// ): number => {
|
|
85
|
+
// 'worklet';
|
|
86
|
+
|
|
87
|
+
// const term = 1 - t;
|
|
88
|
+
// const a = 1 * term ** 3 * t ** 0 * from;
|
|
89
|
+
// const b = 3 * term ** 2 * t ** 1 * c1;
|
|
90
|
+
// const c = 3 * term ** 1 * t ** 2 * c2;
|
|
91
|
+
// const d = 1 * term ** 0 * t ** 3 * to;
|
|
92
|
+
// return a + b + c + d;
|
|
93
|
+
// };
|
|
94
|
+
|
|
95
|
+
const cubicSolve = function (t, p0, p1, p2, p3) {
|
|
96
|
+
'worklet';
|
|
97
|
+
|
|
98
|
+
return (1 - t) * (1 - t) * (1 - t) * p0 + 3 * (1 - t) * (1 - t) * t * p1 + 3 * (1 - t) * t * t * p2 + t * t * t * p3;
|
|
99
|
+
};
|
|
100
|
+
export const cubicBezierYForX = (x, a, b, c, d, precision = 2) => {
|
|
101
|
+
'worklet';
|
|
102
|
+
|
|
103
|
+
const pa = -a.x + 3 * b.x - 3 * c.x + d.x;
|
|
104
|
+
const pb = 3 * a.x - 6 * b.x + 3 * c.x;
|
|
105
|
+
const pc = -3 * a.x + 3 * b.x;
|
|
106
|
+
const pd = a.x - x;
|
|
107
|
+
const ts = solveCubic(pa, pb, pc, pd).map(root => round(root, precision)).filter(root => root >= 0 && root <= 1);
|
|
108
|
+
const t = ts[0];
|
|
109
|
+
if (t == null) return 0;
|
|
110
|
+
// return cubicBezier(t, a.y, b.y, c.y, d.y);
|
|
111
|
+
return cubicSolve(t, a.y, b.y, c.y, d.y);
|
|
112
|
+
};
|
|
113
|
+
export const selectCurve = (cmds, x) => {
|
|
114
|
+
'worklet';
|
|
115
|
+
|
|
116
|
+
let from = vec(0, 0);
|
|
117
|
+
for (let i = 0; i < cmds.length; i++) {
|
|
118
|
+
const cmd = cmds[i];
|
|
119
|
+
if (cmd == null) {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
if (cmd[0] === PathVerb.Move) {
|
|
123
|
+
from = vec(cmd[1], cmd[2]);
|
|
124
|
+
} else if (cmd[0] === PathVerb.Cubic) {
|
|
125
|
+
const c1 = vec(cmd[1], cmd[2]);
|
|
126
|
+
const c2 = vec(cmd[3], cmd[4]);
|
|
127
|
+
const to = vec(cmd[5], cmd[6]);
|
|
128
|
+
if (x >= from.x && x <= to.x) {
|
|
129
|
+
return {
|
|
130
|
+
from,
|
|
131
|
+
c1,
|
|
132
|
+
c2,
|
|
133
|
+
to
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
from = to;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return undefined;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Convert commands to cubic bezier
|
|
144
|
+
*/
|
|
145
|
+
export const commandsToBezier = cmds => {
|
|
146
|
+
'worklet';
|
|
147
|
+
|
|
148
|
+
const normCmds = [];
|
|
149
|
+
let x = 0,
|
|
150
|
+
y = 0;
|
|
151
|
+
for (let i = 0; i < cmds.length; i++) {
|
|
152
|
+
const currCmd = cmds[i];
|
|
153
|
+
if (currCmd === undefined) throw new Error(`No command found at index ${i}`);
|
|
154
|
+
let normCmd;
|
|
155
|
+
const cmd = currCmd[0];
|
|
156
|
+
if (cmd === undefined) throw new Error('Got undefined command');
|
|
157
|
+
if (cmd === PathVerb.Line) {
|
|
158
|
+
normCmd = [PathVerb.Cubic, x, y, currCmd[1], currCmd[2], currCmd[1], currCmd[2]];
|
|
159
|
+
} else {
|
|
160
|
+
normCmd = currCmd;
|
|
161
|
+
}
|
|
162
|
+
x = normCmd[normCmd.length - 2];
|
|
163
|
+
y = normCmd[normCmd.length - 1];
|
|
164
|
+
normCmds.push(normCmd);
|
|
165
|
+
}
|
|
166
|
+
return normCmds;
|
|
167
|
+
};
|
|
168
|
+
export const getYForX = (cmds, x, precision = 2) => {
|
|
169
|
+
'worklet';
|
|
170
|
+
|
|
171
|
+
const cmdsNorm = commandsToBezier(cmds);
|
|
172
|
+
const c = selectCurve(cmdsNorm, x);
|
|
173
|
+
if (c == null) return undefined;
|
|
174
|
+
return cubicBezierYForX(x, c.from, c.c1, c.c2, c.to, precision);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// See Path/getPath2D
|
|
178
|
+
|
|
179
|
+
export const magnitude = p => Math.sqrt(p.x * p.x + p.y * p.y);
|
|
180
|
+
export const normalize = p => {
|
|
181
|
+
const m = magnitude(p);
|
|
182
|
+
return {
|
|
183
|
+
x: p.x / m,
|
|
184
|
+
y: p.y / m
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
//# sourceMappingURL=maths.js.map
|