[reactjs] zoom is resetting on render #589
Replies: 5 comments 5 replies
-
You're using The issue seems to have to do with the See this code in
The way react-chart-js-2 works, a new options object is being set every time state updates. The way I've solved it is
|
Beta Was this translation helpful? Give feedback.
-
I'm going to move this to discussion, as it seems this is an issue with react-chartjs-2 |
Beta Was this translation helpful? Give feedback.
-
I am using custom tooltip with zoom, but showing tooltip trigger render, hence reset zoom. const showTooltip = useCallback(({ chart, tooltip }) => {
if (tooltip.opacity === 0) {
setTooltipPosition(null);
return;
}
const position = chart.canvas.getBoundingClientRect();
const top = tooltip.caretY + position.y + 5;
const left = tooltip.caretX + position.x + 5;
if (top === tooltipPosition?.y) {
return;
}
const getActiveElementRawData = () =>
tooltip.getContext().tooltipItems.at(-1).raw;
console.log(getActiveElementRawData());
setTooltipPosition({ y: top, x: left });
setTooltipData(getActiveElementRawData());
console.log(tooltipData);
}, []);
const options = useMemo(
() => ({
plugins: {
responsive: true,
legend: {
onHover: customHoverHandler,
position: "right",
display: true,
},
tooltip: {
enabled: false,
external: showTooltip,
},
zoom: {
pan: {
enabled: true,
},
zoom: {
wheel: {
enabled: true,
},
mode: "y",
},
},
},
scales: {
y: {
// beginAtZero: true,
grace: "1",
},
x: {
grace: "5%",
ticks: {
maxRotation: 90,
padding: 10,
},
},
},
}),
[showTooltip]
); |
Beta Was this translation helpful? Give feedback.
-
From Chart.js docs (https://www.chartjs.org/docs/latest/developers/updates.html): "To update the options, mutating the options property in place or passing in a new options object are supported.
Don't immutably update options or data. Instead, mutate the original objects and get a ref to the chart component with which you can manually call I.e. chartOptions.interaction.mode = "x";
chart.current.update() |
Beta Was this translation helpful? Give feedback.
-
Wrap the child component in a memo. e.g. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey guys,
in advance: part of the reason, why I'm filing this issue is probably due to my missing knowledge of reactjs and chartjs, and I understand if that is kinda a no-go for you guys, who implemented this great plugin and are maybe annoyed by having to answer questions like this. however I did a lot of googling, but I could not find an answer or a link to the right direction. The only thing that is coming close to the problem I have is this github issue (reactchartjs/react-chartjs-2#107), where OP has the same issue but with a different graph-library
I implemented a Line-Chart in a functional component, and zooming kinda works, but as soon as the component is rendered again, the zoom resets to default (making zoom unusable). Any Idea, how to prevent this behavior?
I have the feeling that one option would be to safe the current zoom and then pass the current zoom to the Chart-Component in the return statement, but a) I would need some help on passing the current or stored zoom to the component, as I couldn't find any instructions on how to do that and b) it seems a bit overkill, that I need to do that, and I was hoping (and expecting) a more straight forward solution.
Here is a code snippet, I can provide the full code, if needed.
Beta Was this translation helpful? Give feedback.
All reactions