Skip to content
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

How to get value when click mouse on the chart? #369

Open
shsen opened this issue Jun 8, 2023 · 8 comments
Open

How to get value when click mouse on the chart? #369

shsen opened this issue Jun 8, 2023 · 8 comments

Comments

@shsen
Copy link

shsen commented Jun 8, 2023

I create a line chart use DefaultCategoryDataset, and it have several lines, Y axis is number value and X axis is date.
How can I get the value of each line and date at the point when I click mouse on the chart?
Thanks.

@trashgod
Copy link
Contributor

trashgod commented Jun 8, 2023

Add a ChartMouseListener, seen here, to your view component.

@shsen
Copy link
Author

shsen commented Jun 9, 2023

Thank you for your reply, but the link is show "Human verification"...
Can you copy the sample code to here?
Thanks.

@trashgod
Copy link
Contributor

trashgod commented Jun 9, 2023

I see it, too. After verifying, refine your search according to your use case. Failing that, more examples are archived here.

@shsen
Copy link
Author

shsen commented Jun 9, 2023

I see many examples use XYPlot, and I use CategoryPlot, so it no use for me...

my code as below:

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(500, "Line1", "2023.05.01");
dataset.addValue(300, "Line2", "2023.05.01");
dataset.addValue(700, "Line1", "2023.05.02");
dataset.addValue(500, "Line2", "2023.05.02");
...
chart = ChartFactory.createLineChart("","","",dataset, PlotOrientation.VERTICAL,true,true,false);
...

//show chart Frame:
public class OverlayFrame extends JFrame
{
static JFrame frame = null;
static String strTitle = "";
static class MyPanel extends JPanel implements ChartMouseListener
{
private ChartPanel chartPanel;
public MyPanel(JFreeChart chart)
{
super(new BorderLayout());
this.chartPanel = new ChartPanel(chart);
this.chartPanel.addChartMouseListener(this);
add(this.chartPanel);
}

    public void chartMouseClicked(ChartMouseEvent event)
    {
        /*JFreeChart chart = event.getChart();
        CategoryPlot plot = chart.getCategoryPlot();
        CategoryAxis xAxis = plot.getDomainAxis();
        //plot.setDomainAxis(xAxis);
        Comparable c = plot.getDomainCrosshairColumnKey();
        if (c != null)
        {
            frame.setTitle(c.toString());
        }
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);*/
        //frame.setTitle(xAxis.getCategoryLabelToolTip(plot.getDomainCrosshairColumnKey()));
        //frame.setTitle(strTitle + "Hello");
    }

    public void chartMouseMoved(ChartMouseEvent event)
    {
        JFreeChart chart = event.getChart();
        CategoryPlot plot = chart.getCategoryPlot();
        Comparable cx = plot.getDomainCrosshairColumnKey();
        Comparable cy = plot.getDomainCrosshairRowKey();
        if (cx != null)
        {
            frame.setTitle(cx + "   " + cy);
        }
        plot.setDomainCrosshairVisible(true);
    }
}

public OverlayFrame(String title, JFreeChart chart)
{
    super(title);
    setContentPane(new MyPanel(chart));
    frame = this;
    strTitle = title;
}

}

so, how to get the value(2 Line have 2 value) and date(only one) to show in frame title???

@trashgod
Copy link
Contributor

Focusing on the mouse event's ChartEntity, the listener below displays each of the four points as the mouse is moved over them.

@Override
public void chartMouseClicked(ChartMouseEvent cme) {
	report(cme.getEntity());
}

@Override
public void chartMouseMoved(ChartMouseEvent cme) {
	report(cme.getEntity());
}

private void report(ChartEntity ce) {
	if (ce instanceof CategoryItemEntity cie) {
		chart.setTitle(cie.getRowKey() + " " + cie.getColumnKey());
	}
}

@shsen
Copy link
Author

shsen commented Jun 12, 2023

if (event.getEntity() instanceof CategoryItemEntity)
{
frame.setTitle(((CategoryItemEntity) event.getEntity()).getRowKey() + " " + ((CategoryItemEntity) event.getEntity()).getColumnKey());
}

it can work, thank you. but it is not my request, I want to show the all lines value when the mouse move aross the date column, not the mouse point move in the entity. And show the DomainCrosshair (just use plot.setDomainCrosshairVisible(true))...

@trashgod
Copy link
Contributor

Since your domain axis is a CategoryAxis, it looks like you may be able to use getCategoryStart|Middle|End methods to get the domain bound for each row key; you can then determine which column includes cme.getTrigger().getX().

@shsen
Copy link
Author

shsen commented Jun 15, 2023

Thank you!I use JSlider and barely solve the demand.

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

No branches or pull requests

2 participants