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
Variations on nearest
interaction mode
#9961
Comments
Just a quick comment about |
Another note FYI, we are missing a mode similar to nearest, but considering value instead of coordinates (or something similar), which would hit all the bars in a category + any other elements having the same x and/or y value. |
User defined interaction mode(s) are supported and I see no reason not to try making it as easy as possible. As both me and @etimberg are quite busy with other stuff currently, I'd encourage you to draft a PR for consideration. To minimize repeated work, I think it would be best to start with code changes, leaving documentation and types out from the first draft. |
Yup, I'm happy to look at a PR. I agree with starting with just the code changes since there will probably be some iterations to get to the final design |
This is tracked in #9974, if I'm understanding correctly. FWIW, that's not quite what I was looking for with |
Oddly, I still seem to be getting multiple matching points from one dataset in my tooltip, which I solved by hiding every other point from interactions. It's hacky but it does the trick.
|
Feature Proposal
Either add some sort of
nearestPerDataset
mode and some sort ofintersectRadius
ormaxDistance
option, or make it easier to create new interaction modes by exporting more functions from core.interaction.js.Feature Use Case
I have some dense data with uneven X values and would like for the tooltip to display the nearest value for each dataset. The current Chart.js interaction modes don't do what I want:
x
displays multiple values per dataset, because the data is dense enough that there's more than one value within range.nearest
looks for the single nearest X coordinate. Since the datasets have uneven X values, one or more datasets are often excluded.See https://codesandbox.io/s/chartjs-interaction-modes-pkf2b?file=/src/index.js for a demo.
To solve this, I'm locally experimenting with a variation of
nearest
that determines the nearest element per dataset and returns all nearest-per-dataset elements whose distance is close enough to minDistance.Unrelated to this, I was also experimenting with a variation on
{mode: 'nearest', axis: 'xy'}
that only shows items if the cursor is relatively close to a data point. I wanted something that's more tolerant thanintersect: true
but not so broad that it pops up a tooltip at the other end of the chart if you mouse over a large, sparse chart. (In other words, it's identical to core.interaction.js'sgetNearestItems
with!intersect
, except that it would also checkminDistance
against some sort ofintersectRadius
ormaxDistance
option.)I suspect these options are specialized enough that they shouldn't necessarily go into Chart.js, and I'm fine with implementing them myself. However, it feels hard to implement them myself: if I want behavior similar to
nearest
, I have to reference private members and duplicate a lot of non-exported functionality from core.interaction.js.So, instead of adding more specialized interaction modes and options, what about exporting more of core.interaction.js?
Possible Implementation
A simple exported function could look like this:
There may also be room to consolidate some of the core.interaction.js modes (so they share more implementation and so their implementation is closer to a hypothetical exported
evaluateItems
).I can work on a PR if this seems reasonable / desirable.
The text was updated successfully, but these errors were encountered: