like selecting a point with mouse

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
dabid
Posts: 1
Joined: Thu Oct 06, 2005 5:39 pm

like selecting a point with mouse

Post by dabid » Thu Oct 06, 2005 5:48 pm

hello, I need to eliminate points of graphic with mouse and wanted to know like selecting the points


thanks

michael75
Posts: 16
Joined: Thu Sep 15, 2005 4:42 pm

Post by michael75 » Thu Oct 06, 2005 8:34 pm

hello dabid,

afaik selecting points is not implemented in jfreechart but if u are interested in doing so, have a look at how the crosshairs can lock on data. this might be a good starting point to understand how things work inside jfreechart.

cu,
michael

Guest

Post by Guest » Thu Oct 13, 2005 4:11 pm

oK. thanks michael

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Oct 14, 2005 9:42 am

Try the addChartMouseListener() method in the ChartPanel class and look through the events generated by this mechanism. You can use this to determine when a data point is clicked - after that, you need to write your own code to do something with that information (e.g. delete an item from the dataset).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

vani_hegde
Posts: 17
Joined: Tue Oct 11, 2005 9:27 pm

Post by vani_hegde » Fri Oct 21, 2005 12:49 am

David,
with this, we can only detect if the mouse is moved over or clicked on a data point.
I want to findout if the mouse is clicked anywhere on the series irrespective of just the data points and adding a chartmouselistener, i could detect only the data points. What do i need to do if i want to know the mouse clicks at points other than data points also?

thanks,
vani

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Oct 21, 2005 12:45 pm

David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Guest

Post by Guest » Fri Oct 21, 2005 1:23 pm

As you can see in the linked thread, you just need to translate the mouse click into a chart coordinate ...

After that, what you want to do with it is quite free ; you can for example look for the closest data point and consider it as selected ...

vani_hegde
Posts: 17
Joined: Tue Oct 11, 2005 9:27 pm

Post by vani_hegde » Fri Oct 21, 2005 8:59 pm

Yes, i got the point where the mouse was clicked, it was quite straight forward. But my question is how can i find out the nearest data point and how to determine which series that point belongs to.

I have multiple series in my chart and when the mouse is clicked, i want to know which is the series that is nearer to that mouse click area. How to do this???

Please help

Cdr
Posts: 9
Joined: Thu Oct 20, 2005 8:39 am
Location: Belgium
Contact:

Post by Cdr » Mon Nov 07, 2005 3:55 pm

Here's a piece of code you could use ... I need to do something familiar.

I don't pretend it's the most efficient way though.

Code: Select all

	public void mousePressed(MouseEvent e){
		// get the chart coordinates for the mouse click
		Point2D p = ChartUtils.screenToChart(panel,e.getPoint());
		
		// try to locate the closest data point in the (first dataset of the) panel
		double min_dist = Double.MAX_VALUE; 
		double x_min=0,y_min=0 ;
		XYDataset set = panel.getChart().getXYPlot().getDataset();
		for (int i=0;i<set.getSeriesCount();i++) {
			for (int j=0;j<set.getItemCount(i);j++) {
				// evaluate the distance
				double dist = p.distance(set.getXValue(i,j),set.getYValue(i,j));
				if(dist<min_dist){
					min_dist = dist ;
					x_min = set.getXValue(i,j) ;
					y_min = set.getYValue(i,j) ;
				}
			}
		}
		
// do whatever you like with the point after
		System.out.println("("+x_min+","+y_min+")");
	}
The screenToChart method is my own, and converts a mouseclick into chart coordinates; i guess you did that already so I didn't join the code.

From there on, you can get the closest point or its series/dataset (i and j), and add your own code to handle the closest point (delete ?).

Good luck,
Cédric.
Cdr
Theory is when we know everything but nothing works.
Practice is when everything works but noone knows why.
Here we joined theory and practice : nothing works and noone knows why ...

Cdr
Posts: 9
Joined: Thu Oct 20, 2005 8:39 am
Location: Belgium
Contact:

Post by Cdr » Mon Nov 07, 2005 4:21 pm

you can also make use of

Code: Select all

double x_min = panel.getChart().getXYPlot().getDomainCrosshairValue() ;
double y_min = panel.getChart().getXYPlot().getRangeCrosshairValue() ;
after setting the crosshair to lock on data

Code: Select all

			panel.getChart().getXYPlot().setDomainCrosshairVisible(true);
			panel.getChart().getXYPlot().setDomainCrosshairLockedOnData(true);
			panel.getChart().getXYPlot().setRangeCrosshairVisible(true);
			panel.getChart().getXYPlot().setRangeCrosshairLockedOnData(true);
Cdr
Theory is when we know everything but nothing works.
Practice is when everything works but noone knows why.
Here we joined theory and practice : nothing works and noone knows why ...

Locked