Search code examples
javamouseeventjfreechartcoordinate-systems

JFreeChart interactive chart editing handling ChartMouseEvent


I'm trying to intercept ChartMouseEvent in order to modify an XYSeries of a JFreeChart object created with ChartFactory.createXYLineChart method (and displayed using a JDialog).

XYLineChart

I retrieve successfully the coordinate of the mouse event this way:

public void chartMouseMoved(ChartMouseEvent arg0) {
    int x = arg0.getTrigger().getX();
        int y = arg0.getTrigger().getY();

The origin of the coordinate system (0,0) is located at the red square in the picture. Now, I would like to calculate in which interval is the mouse in. In order to do this I need:

  1. the top-left coordinate of the grey chart (green square)
  2. height and width of the grey chart

How can I get this values?

A note: I'm a JFreeChart newbie. If I'm doing this wrong, and there is a better way to do achive these goals, please put me in the right direction.


Solution

  • Are you rendering the plot via ChartPanel?

    If so, take a look at ChartPanel.getChartRenderingInfo().getPlotInfo().getDataArea(). This should return a Rectangle2D that is easy to work with.