leeoniya / uPlot

📈 A small, fast chart for time series, lines, areas, ohlc & bars
MIT License
8.48k stars 370 forks source link

Can we fire the setCursor hook when a cursor becomes locked? #921

Open jint23 opened 3 months ago

jint23 commented 3 months ago

I noticed the setCursor hook was not fired after the cursor is locked: https://github.com/leeoniya/uPlot/blob/master/src/uPlot.js#L3190

More Context:

I'm trying to implement a feature that locks the tooltip like the one in https://leeoniya.github.io/uPlot/demos/candlestick-ohlc.html I'd like the tooltip to be locked and not to be hidden even if the user exits the plot after the cursor is locked.

I'm also considering displaying more detailed information in the tooltip after the cursor is locked.

I'm more than happy to submit a PR if you think this makes sense, thanks!

leeoniya commented 3 months ago

looking back at the line history i can't find a specific reason for skipping the update during locking. maybe was before hooks existed and this was just a micro-optimization to avoid an unnecessary cursor update to the same position it was already at :shrug:. i tried to see if just removing the if broke anything with the different mode toggles here [1], and all still appears to work as before, so this change sounds good to me :+1:

it'll be few weeks before next release is out, so feel free to use the dist files until then, or set your package.json dep to "uplot": "leeoniya/uPlot#b0fd072ea34b845be434841e42bf795ab840e210

[1] https://leeoniya.github.io/uPlot/demos/sync-cursor.html

jint23 commented 3 months ago

@leeoniya Thank you very much for the quick fix. uPlot is an awesome library 👍

leeoniya commented 3 months ago

thanks :)

jint23 commented 3 months ago

@leeoniya I just realized this does not work perfectly. If you try to lock a cursor in the Sync Cursor demo then unlock, the unlocked cursor is not synced correctly. You can test reproduce this by either click on the same plot to unlock or unlock in a different plot, these two cases will both have incorrectly synced cursors, but in different ways.

jint23 commented 3 months ago

I found another minor bug related to locked cursors, u-cursor-y is always visible at the very top of a plot for locked cursors even when they should not be rendered based on cursor.sync.match. Steps to reproduce in the Sync Cursor demo:

  1. click on the RAM plot to lock the cursor
  2. see the Y cursor line appear at the top of the TCP plot