xanderdeseyn / react-native-responsive-linechart

A customizable and responsive line or area chart for react-native
https://react-native-responsive-linechart.surge.sh
MIT License
203 stars 47 forks source link

Feature request: programmatically select dataPoint #42

Closed davidruisinger closed 3 years ago

davidruisinger commented 3 years ago

First of all, really nice library you got there!

When my chart is shown I always want top preselect the last dataPoint in the chart so it would be really great if the library would allow me to programmatically select a dataPoint.

xanderdeseyn commented 3 years ago

Sounds useful, I'll add it tomorrow!

xanderdeseyn commented 3 years ago

So I took a bit longer but it landed in v5.4.0 πŸ‘

You can add initialTooltipIndex to <Line />

davidruisinger commented 3 years ago

Thatβ€˜s what I call a Christmas present πŸ˜…

davidruisinger commented 3 years ago

@N1ghtly Unfortunately the initialTooltipIndex prop is not working for me. I think the reason behind this is that my Chart is re-rendered as soon as the data has been fetched from the BE. The dataupdate triggers the useEffect within the Line component which than tries to update the index with setTooltipIndex. Since until now the user did not touch any datapoint the newIndex will be undefined.

After adding an extra check for undefined here it seems to work for me but I'm not sure wether this would have any other implications

if (newIndex !== undefined && newIndex !== tooltipIndex) {
  setTooltipIndex(newIndex)
  ...
xanderdeseyn commented 3 years ago

Hmm I see, I'll probably have to keep a boolean whether the user has touched the chart or not. Not my favorite solution but I don't see another one atm. I'll try to get it in today πŸ‘

davidruisinger commented 3 years ago

Isn't the lastTouch from context already enough to check wether the user has touched the chart? Anyways, thanks for the effort!

xanderdeseyn commented 3 years ago

Not really, because lastTouch is set to undefined when touch is released after a pan. But maybe I'll rework that system because it's not very clear this way. Was a bit busy but should have time tomorrow!

xanderdeseyn commented 3 years ago

Hi @flavordaaave, it should work in v5.4.1 as long as the user doesn't manually place the tooltip somewhere between renders :)