3

I am using JFreeChart in Java to draw an XYLineChart containing 50+ x, y points. The chart is in a ChartPanel which is in 1 of the JPanels of my JFrame.

I know JFreeChart already has zoom functionality built-in whereby on a mouse click and drag, it zooms in/ out and displays the points under the selected "dragged" area.

I want to know if there is any way to have it return the x, y co-ordinates of the points if we zoom while pressing the CTRL, ALT or SHIFT key, i.e., if a user clicks a point on the chart and then drags the mouse, it should zoom as usual. But if the user clicks on a point and drags while pressing either SHIFT or ALT or CTRL keys, we should get a list of all the points selected on the Chart [the points underneath the dragged area]. Is this possible ? Kindly let me know how to go about it.

I did try to add a simple MouseListener to the ChartPanel to detect when the mouse is pressed & released but the x, y position values returned by the Mouse event correspond to the location of the Click on screen & not to the actual x, y co-ordinates of the points on the chart. I need the actual co-ordinates of all points that the user clicks and drags the mouse over, so as to calculate before which point was the mouse pressed and dragged & where the mouse was released and use that information to select multiple points underneath the dragged area.

If anyone has any idea about this, please let me know.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Ajit Singh
  • 115
  • 11
  • Cross-posted [here](http://www.jfree.org/forum/viewtopic.php?f=3&t=116768). – trashgod Jan 21 '14 at 13:00
  • Sorry for cross-posting it but I have been struggling with this problem for a while now and wanted someone to at least send me their thoughts about how to solve this. – Ajit Singh Jan 21 '14 at 13:40
  • I tried to implement this feature in the older version of JFreeChart: JFreeChart 1.0.16 but am not able to do so. I can use a ChartMouseListener on my ChartPanel to get x, y values of a point when it is clicked. However, to implement what I want (multiple points selected & their plotted x, y values displayed), I need to have a regular MouseListener on the ChartPanel which would do this by using mousePressed() & mouseReleased() which use the MouseEvent and not ChartMouseEvent. How do I get the actual plotted x, y values and not the x, y point on screen using the MouseListener ? – Ajit Singh Jan 21 '14 at 13:46
  • No problem cross-posting; the link lets others know of existing answers. – trashgod Jan 21 '14 at 14:43

2 Answers2

4

How do I get the actual plotted x, y values and not the x, y point on screen using the MouseListener?

Add a ChartMouseListener, as shown here and here.

Outside of JFreeChart, multiple selection with a lasso is illustrated in the example cited here. A marching ants rectangle is shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Hi, thanks for the reply. But, I want to know how to get the plotted x, y values using a regular `MouseListener`. I know how to do this with the ChartMouseListener (via ChartMouseEvent) but it only has 2 methods: mouseClicked() & mouseMoved() whereas I'm interested in using the MouseListener's `MouseEvent` & the mousePressed() & mouseReleased() methods to find out where the mouse began dragging from & till where. That way I can get all the points within that horizontal range and retrieve & select them all to create a **lasso** effect. Is there any way to achieve this ? – Ajit Singh Jan 21 '14 at 16:42
  • Is there any way to do what I mentioned above ? Since using a ChartMouseEvent inside a regular MouseListener instead of MouseEvent OR creating a custom method in a `ChartMouseListener` that gets called when a mouse is pressed or released (like the MouseListener's mousePressed(), mouseReleased() methods) are both impossible to do, the only possible solution is if there's a way to calculate the actual plotted x, y values (based on out Chart's x, y axis values) within a standard `MouseListener` instead of getting having it return x, y based on screen dimensions]. How can this be done ? – Ajit Singh Jan 21 '14 at 17:51
  • You can see the original event using `getTrigger()`, as suggested [here](http://stackoverflow.com/a/20900152/230513). – trashgod Jan 21 '14 at 18:13
4

This is the answer I gave in the JFreeChart forum:

It is not implemented in JFreeChart 1.0.17, but this feature has been implemented in the JFreeChart-FSE ("future state edition") project on GitHub:

https://github.com/jfree/jfreechart-fse

There has not been a release from this repo, but you can build it from the source. Feedback is welcome, I have not had time recently to work on this but I would like to get a release done from this repo some time in the coming months (my aim is to have JFreeChart 2.0 in 2014). The lasso selection came from some code that I wrote some years ago now, but Michael Zinsmaier from KNIME.org has done a big chunk of work to structure this and the dataset selection state.

David Gilbert
  • 4,427
  • 14
  • 22