Skip to content
New issue

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

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

Already on GitHub? # to your account

Trigger event when mouse hover over chart's bar #498

Open
macdevign opened this issue Aug 15, 2020 · 6 comments
Open

Trigger event when mouse hover over chart's bar #498

macdevign opened this issue Aug 15, 2020 · 6 comments

Comments

@macdevign
Copy link

I note that currently cursor is able to show data detail as mouse hover over chart's bar, but is there a way to listen to event whenever mouse hover over chart's bar ? Basically I want to show custom detail in custom UI whenever mouse hover over chart's bar, is that possible , and if so, how to do that ?

@Mr14huashao
Copy link
Collaborator

Mr14huashao commented Aug 17, 2020

@macdevign Try adding this setting:

chart.getStyler().setToolTipsEnabled(true);

If you still have a problem, please upload all your codes for the problem as an attachment, such as Demo or test cases or the whole project, and so on,to help you analyze this problem. Thank you.

@macdevign
Copy link
Author

Does setToolTipsEnabled to true allow to listen to mouse's hover to chart's bar and how do I accomplish that (eg something like toolTip.addSelectionListener ?)

@gbl
Copy link

gbl commented Nov 27, 2020

I guess what you could do is something like

            chartPanel = new XChartPanel<>(chart);
            chartPanel.addMouseListener(listener);

and in your event handlers, use

    @Override
    public void mousePressed(MouseEvent e) {
        this.pressXPos = (long) chart.getChartXFromCoordinate(e.getX());
        this.pressYPos = (long) chart.getChartYFromCoordinate(e.getY());
        System.out.println("pressed at "+pressXPos+" / "+pressYPos);
    }

to convert the screen coords to the graph coords so you know which graph element to show info for.

@twolf2919
Copy link

twolf2919 commented May 4, 2021

I guess what you could do is something like

            chartPanel = new XChartPanel<>(chart);
            chartPanel.addMouseListener(listener);

and in your event handlers, use

    @Override
    public void mousePressed(MouseEvent e) {
        this.pressXPos = (long) chart.getChartXFromCoordinate(e.getX());
        this.pressYPos = (long) chart.getChartYFromCoordinate(e.getY());
        System.out.println("pressed at "+pressXPos+" / "+pressYPos);
    }

to convert the screen coords to the graph coords so you know which graph element to show info for.

Hi, which version of XChart has these methods (chart.getChartXFromCoordinate() and chart.getChartYFromCoordinate())? I am using 3.8.0 and my IDE complains about those methods not existing. I pulled the master branch for XChart and did a recursive grep and it didn't find them either. Thanks for any help. I'm basically looking for a click handler and a way to identify the pie slice a user clicked on.

@gbl
Copy link

gbl commented May 4, 2021

That was on 3.6.7-Snapshot, which was the bleeding edge version when I did something similar last November.

Seems like it was removed on this commit: aa13f6c#diff-9013d15fe5f4684f20473ca036c5bd33d67ba15a015e20da5f20be05337d6343

but the diff looks like you can probably get the X/Y axis, and get the coordinate from there. Not sure if that works with a pie chart though, as I used the XYChart in my code.

@twolf2919
Copy link

That was on 3.6.7-Snapshot, which was the bleeding edge version when I did something similar last November.
...
but the diff looks like you can probably get the X/Y axis, and get the coordinate from there. Not sure if that works with a pie chart though, as I used the XYChart in my code.

Thanks for the info.

Hm, you wouldn't happen to know if there's an API for querying the currently selected tooltip, would you? I just realized that if I had that in the Panel's MouseListener.mousePressed handler, if I could query that and would have the information I'm after! The PieChart definitely highlights the slice as the mouse cursor moves over it (when setTooltipsEnabled(true) that is).

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants