Get closest datapoint to a mouse click

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
gsingh
Posts: 5
Joined: Mon Aug 20, 2018 10:34 am
antibot: No, of course not.

Get closest datapoint to a mouse click

Post by gsingh » Mon Sep 10, 2018 8:53 am

Hello,

I have a linechart, where I am trying to add the functionality of getting the x and y values of the plot by clicking on it. At the moment, my program will only return the x and y value if I click directly on the line plot. However, I would like for it to be able to register the closest datapoint to the mouse click wherever I click on the chart. Below is how I have implemented the current functionality of getting the datapoints and storing them in a JTable (this is just the relevant part of the method). If anyone could help me extend it it would be much appreciated:

Code: Select all

public void initMarker(DefaultTableModel model, JButton clearAll)
	{
		chartPanel.addChartMouseListener((new ChartMouseListener()
		{
			public void chartMouseClicked(ChartMouseEvent mE)
			{

				try
				{

					// Getting the series index and the point on chart clicked
					// on.
					ChartEntity ce = mE.getEntity();
					XYItemEntity e = (XYItemEntity) ce;
					XYDataset d = e.getDataset();
					int s = e.getSeriesIndex();
					int i = e.getItem();
					String param = "";
					String dset = "";

					// Defining the s parameter
					if (s == 0)
					{	
						param = "S11";
					}

					if (s == 1)
					{	
						param = "S12";
					}

					if (s == 2)
					{	
						param = "S21";
					}

					if (s == 3)
					{	
						param = "S22";
					}
					
					if (d == chart.getXYPlot().getDataset(0)) {
						dset = "Magnitude";
						
					}
					
					if (d == chart.getXYPlot().getDataset(1)) {
						dset = "Phase";
						
					}
					
					if (d == chart.getXYPlot().getDataset(2)) {
						dset = "Grp Delay";
						
					}

					// How many times the mouse has been clicked on the plot
					// is what the current row number is
					if (model.getValueAt(0, 2) == "")
					{
						clicks = 1;
					}

					else
					{
						clicks++;
					}
					
					if ( d == chart.getXYPlot().getDataset(2)) {
						
						System.out.println("gd");
						
						model.setValueAt(dset, clicks - 1, 0);

						model.setValueAt(param, clicks - 1, 1);
						model.setValueAt(new Integer((int) d.getXValue(s, i)), clicks - 1, 2);
						model.setValueAt(new Float((float) d.getYValue(s, i)), clicks - 1, 3); 
					}
					
					else {

					// Setting the values in the appropriate cells.
						
						model.setValueAt(dset, clicks - 1, 0);

						model.setValueAt(param, clicks - 1, 1);
						model.setValueAt(new Integer((int) d.getXValue(s, i)), clicks - 1, 2);
						model.setValueAt(new Double((double) round(d.getYValue(s, i), 2)), clicks - 1, 3);
					}
					//java.awt.geom.Ellipse2D.Double shape = new java.awt.geom.Ellipse2D.Double(-2.0, -2.0, 4.0, 4.0);
					//chart.getXYPlot().getRenderer().setBaseShape(shape);

Locked