Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: usage of layoutSize.value doesn't contain width or height causin… #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 27 additions & 50 deletions src/charts/linear/ChartPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ export default function ChartPathProvider({
proceededData,
} = useContext(ChartContext) || generateValues();

const height = rest.height || layoutSize.value.height;
const width = rest.width || layoutSize.value.width;

const prevData = useSharedValue(valuesStore.current.prevData, 'prevData');
const currData = useSharedValue(valuesStore.current.currData, 'currData');
const curroriginalData = useSharedValue(
Expand Down Expand Up @@ -287,16 +290,12 @@ export default function ChartPathProvider({
}
isStarted.value = true;

const eventX = positionXWithMargin(
event.x,
hitSlopValue.value,
layoutSize.value.width
);
const eventX = positionXWithMargin(event.x, hitSlopValue.value, width);

let idx = 0;
const ss = smoothingStrategy;
for (let i = 0; i < currData.value.length; i++) {
if (getValue(currData, i, ss).x > eventX / layoutSize.value.width) {
if (getValue(currData, i, ss).x > eventX / width) {
idx = i;
break;
}
Expand All @@ -308,38 +307,32 @@ export default function ChartPathProvider({
if (
ss.value === 'bezier' &&
currData.value.length > 30 &&
eventX / layoutSize.value.width >=
currData.value[currData.value.length - 2].x
eventX / width >= currData.value[currData.value.length - 2].x
) {
const prevLastY = currData.value[currData.value.length - 2].y;
const prevLastX = currData.value[currData.value.length - 2].x;
const lastY = currData.value[currData.value.length - 1].y;
const lastX = currData.value[currData.value.length - 1].x;
const progress =
(eventX / layoutSize.value.width - prevLastX) / (lastX - prevLastX);
positionY.value =
(prevLastY + progress * (lastY - prevLastY)) *
layoutSize.value.height;
const progress = (eventX / width - prevLastX) / (lastX - prevLastX);
positionY.value = (prevLastY + progress * (lastY - prevLastY)) * height;
} else if (idx === 0) {
positionY.value =
getValue(currData, idx, ss).y * layoutSize.value.height;
positionY.value = getValue(currData, idx, ss).y * height;
} else {
// prev + diff over X
positionY.value =
(getValue(currData, idx - 1, ss).y +
(getValue(currData, idx, ss).y -
getValue(currData, idx - 1, ss).y) *
((eventX / layoutSize.value.width -
getValue(currData, idx - 1, ss).x) /
((eventX / width - getValue(currData, idx - 1, ss).x) /
(getValue(currData, idx, ss).x -
getValue(currData, idx - 1, ss).x))) *
layoutSize.value.height;
height;
}

setoriginalXYAccordingToPosition(
originalX,
originalY,
eventX / layoutSize.value.width,
eventX / width,
curroriginalData
);
positionX.value = eventX;
Expand Down Expand Up @@ -425,17 +418,13 @@ export default function ChartPathProvider({
}
isStarted.value = true;

const eventX = positionXWithMargin(
event.x,
hitSlopValue.value,
layoutSize.value.width
);
const eventX = positionXWithMargin(event.x, hitSlopValue.value, width);

progress.value = 1;
let idx = 0;
const ss = smoothingStrategy;
for (let i = 0; i < currData.value.length; i++) {
if (getValue(currData, i, ss).x > eventX / layoutSize.value.width) {
if (getValue(currData, i, ss).x > eventX / width) {
idx = i;
break;
}
Expand All @@ -447,38 +436,32 @@ export default function ChartPathProvider({
if (
ss.value === 'bezier' &&
currData.value.length > 30 &&
eventX / layoutSize.value.width >=
currData.value[currData.value.length - 2].x
eventX / width >= currData.value[currData.value.length - 2].x
) {
const prevLastY = currData.value[currData.value.length - 2].y;
const prevLastX = currData.value[currData.value.length - 2].x;
const lastY = currData.value[currData.value.length - 1].y;
const lastX = currData.value[currData.value.length - 1].x;
const progress =
(eventX / layoutSize.value.width - prevLastX) / (lastX - prevLastX);
positionY.value =
(prevLastY + progress * (lastY - prevLastY)) *
layoutSize.value.height;
const progress = (eventX / width - prevLastX) / (lastX - prevLastX);
positionY.value = (prevLastY + progress * (lastY - prevLastY)) * height;
} else if (idx === 0) {
positionY.value =
getValue(currData, idx, ss).y * layoutSize.value.height;
positionY.value = getValue(currData, idx, ss).y * height;
} else {
// prev + diff over X
positionY.value =
(getValue(currData, idx - 1, ss).y +
(getValue(currData, idx, ss).y -
getValue(currData, idx - 1, ss).y) *
((eventX / layoutSize.value.width -
getValue(currData, idx - 1, ss).x) /
((eventX / width - getValue(currData, idx - 1, ss).x) /
(getValue(currData, idx, ss).x -
getValue(currData, idx - 1, ss).x))) *
layoutSize.value.height;
height;
}

setoriginalXYAccordingToPosition(
originalX,
originalY,
eventX / layoutSize.value.width,
eventX / width,
curroriginalData
);
positionX.value = eventX;
Expand Down Expand Up @@ -506,7 +489,6 @@ export default function ChartPathProvider({
data,
dotStyle,
extremes,
layoutSize,
onLongPressGestureEvent,
originalX,
originalY,
Expand Down Expand Up @@ -542,7 +524,6 @@ function ChartPath({
currSmoothing,
pathOpacity,
progress,
layoutSize,
__disableRendering,
children,
...props
Expand All @@ -553,10 +534,6 @@ function ChartPath({
const selectedStrokeWidthValue = useSharedValue(selectedStrokeWidth);
const strokeWidthValue = useSharedValue(strokeWidth);

useEffect(() => {
layoutSize.value = { height, width };
}, [height, layoutSize, width]);

const path = useDerivedValue(() => {
let fromValue = prevData.value;
let toValue = currData.value;
Expand Down Expand Up @@ -605,16 +582,16 @@ function ChartPath({

res = fromValue.map(({ x, y }, i) => {
const { x: nX, y: nY } = toValue[i];
const mX = (x + (nX - x) * progress.value) * layoutSize.value.width;
const mY = (y + (nY - y) * progress.value) * layoutSize.value.height;
const mX = (x + (nX - x) * progress.value) * width;
const mY = (y + (nY - y) * progress.value) * height;
return { x: mX, y: mY };
});
} else {
smoothing = currSmoothing.value;
res = toValue.map(({ x, y }) => {
return {
x: x * layoutSize.value.width,
y: y * layoutSize.value.height,
x: x * width,
y: y * height,
};
});
}
Expand All @@ -632,7 +609,7 @@ function ChartPath({
{ x: -res[4].x, y: res[0].y },
].concat(res);
}
if (lastValue.x === layoutSize.value.width && strategy !== 'bezier') {
if (lastValue.x === width && strategy !== 'bezier') {
// extrapolate the last points
res[res.length - 1].x = lastValue.x + 20;
if (res.length > 2) {
Expand All @@ -656,7 +633,7 @@ function ChartPath({
})
.join(' ')
.replace('L', 'M');
});
}, [currData.value, prevData.value, smoothingStrategy.value, width, height]);

const animatedProps = useAnimatedStyle(() => {
const props = {
Expand Down