like selecting a point with mouse
like selecting a point with mouse
hello, I need to eliminate points of graphic with mouse and wanted to know like selecting the points
thanks
thanks
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- Posts: 17
- Joined: Tue Oct 11, 2005 9:27 pm
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
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
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- Posts: 17
- Joined: Tue Oct 11, 2005 9:27 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
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
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.
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.
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+")");
}
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 ...
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 ...
you can also make use of
after setting the crosshair to lock on data
Code: Select all
double x_min = panel.getChart().getXYPlot().getDomainCrosshairValue() ;
double y_min = panel.getChart().getXYPlot().getRangeCrosshairValue() ;
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 ...
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 ...