How to have Crosshairs on a CombinedDomainXYPlot

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
oznog
Posts: 9
Joined: Sat Aug 19, 2006 2:06 am

How to have Crosshairs on a CombinedDomainXYPlot

Post by oznog » Sat Aug 19, 2006 3:11 am

Hello,

I am working on an application that uses a CombinedDomainXYPlot to show multiple subplots. I want to have a vertical (domain) crosshair that moves with the mouse and that appears simultaneously over all of the plots.

I believe that I am getting valid mouse XY values. I used code similar to that posted by develop on 23 Mar 2005.

So far I have had no success getting the crosshair to work -- it doesn't even show up. Eventually, I'd like to have the option of having either or both domain and range crosshairs visible.

Do you have any suggestions, pointers or code samples that does this?

Thank you,

Oznog

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Post by skunk » Sat Aug 19, 2006 2:31 pm

Are you trying to set the crosshairs on the CombinedDomainXYPlot? Have you tried setting the crosshairs on each individual subplot?

oznog
Posts: 9
Joined: Sat Aug 19, 2006 2:06 am

Domain and Range Crosshairs on a CombinedDomainXYPlot

Post by oznog » Sun Aug 20, 2006 9:48 pm

Hi Skunk,

Boy, did I ever feel silly when I read your post. No, I never turned on the crosshairs for the individual subplots. Once I did, it worked like a charm.

I realized afterwards that, unconciously, I had reasoned that since the combined plot had a domain and the subplots did not, that the combined plot must own the crosshairs, and therefore, I only turned on the crosshairs for the combined plot. Turning on the crosshairs for the combined plot is unnecessary -- it doesn't do a thing.

I've included the stripped down code that I am using now (see below). Perhaps you, David or another knowledgeable person will critique it and suggest improvements.

Thank you for your help,

Oznog



When I create each subplot I turn on the range and domain crosshairs with the following:

Code: Select all

        subplot.setDomainCrosshairVisible(true);
        subplot.setDomainCrosshairLockedOnData(false);
        subplot.setRangeCrosshairVisible(true);
        subplot.setRangeCrosshairLockedOnData(false);

My view is my mouse handler/listener:

Code: Select all

        public class View implements ChartMouseListener .....
The code to position the crosshairs is in the mouse movement handler for the ChartMouseListener (in my view):

Code: Select all

public void chartMouseMoved(ChartMouseEvent event)
{
    int mouseX = event.getTrigger().getX();
    int mouseY = event.getTrigger().getY();
    Point mousePoint = new Point(mouseX, mouseY);

                    // convert the Java2D coordinate to axis coordinates... 
    CombinedDomainXYPlot cdPlot = this.mJFreeChart.getCombinedPlot(); 
    ChartRenderingInfo chartInfo = this.mChartPanel.getChartRenderingInfo();
    Point2D java2DPoint = this.mChartPanel.translateScreenToJava2D(mousePoint);
    PlotRenderingInfo plotInfo = chartInfo.getPlotInfo();

                    // see if the point is in one of the subplots; this is the
                    // intersection of the range and domain crosshairs
    int subplotIndex = plotInfo.getSubplotIndex(java2DPoint);

    if (subplotIndex >= 0)   // yep, position the crosshairs
    { 
        Rectangle2D dataArea = plotInfo.getDataArea();
        Rectangle2D panelArea = this.mChartPanel.getScreenDataArea(mouseX,mouseY); 

        List subplotsList = cdPlot.getSubplots();
        Iterator iterator = subplotsList.iterator();
        int index = 0;

        while (iterator.hasNext())   // for each subplot ...
        {
            XYPlot subplot = (XYPlot) iterator.next();
            if (subplotIndex == index)
            {
                                     // this subplot has both the range and
                                     // domain crosshairs
                                     // get the y axis positon
                double yy = subplot.getRangeAxis().java2DToValue(mousePoint.getY(), panelArea, subplot.getRangeAxisEdge());
                                     // make sure the range crosshair is on
                subplot.setRangeCrosshairVisible(true);
                                     // and plot it
                subplot.setRangeCrosshairValue(yy, true);
            }
            else
            {
                                    // this subplot does not have the range
                                    // crosshair, make sure its off
                subplot.setRangeCrosshairVisible(false);
            }

                                    // all subplots have the domain crosshair
                                    // plot it; first get the x axis coordinate; 
                                    // it is the same for all subplots
            double xx = cdPlot.getDomainAxis().java2DToValue(java2DPoint.getX(), dataArea, cdPlot.getDomainAxisEdge());
            subplot.setDomainCrosshairValue(xx, true);
            index++;
        }
    }
}

oznog
Posts: 9
Joined: Sat Aug 19, 2006 2:06 am

Domain and Range Crosshairs on a CombinedDomainXYPlot

Post by oznog » Sun Aug 20, 2006 10:10 pm

Oops, sorry. There is an editing mistake in the previous code: determining the xx value is not part of the iterator loop. Here is a more accurate version:

Code: Select all


public void chartMouseMoved(ChartMouseEvent event) 
{ 
    int mouseX = event.getTrigger().getX(); 
    int mouseY = event.getTrigger().getY(); 
    Point mousePoint = new Point(mouseX, mouseY); 

                    // convert the Java2D coordinate to axis coordinates... 
    CombinedDomainXYPlot cdPlot = this.mJFreeChart.getCombinedPlot(); 
    ChartRenderingInfo chartInfo = this.mChartPanel.getChartRenderingInfo(); 
    Point2D java2DPoint = this.mChartPanel.translateScreenToJava2D(mousePoint); 
    PlotRenderingInfo plotInfo = chartInfo.getPlotInfo(); 

                    // see if the point is in one of the subplots; this is the 
                    // intersection of the range and domain crosshairs 
    int subplotIndex = plotInfo.getSubplotIndex(java2DPoint); 

    if (subplotIndex >= 0)   // yep, position the crosshairs 
    { 
                                    // all subplots have the domain crosshair 
                                    // the x coordinate is the same for all subplots 
        Rectangle2D dataArea = plotInfo.getDataArea(); 
        double xx = cdPlot.getDomainAxis().java2DToValue(java2DPoint.getX(), dataArea, cdPlot.getDomainAxisEdge()); 

        Rectangle2D panelArea = this.mChartPanel.getScreenDataArea(mouseX,mouseY); 

        List subplotsList = cdPlot.getSubplots(); 
        Iterator iterator = subplotsList.iterator(); 
        int index = 0; 

        while (iterator.hasNext())   // for each subplot ... 
        { 
            XYPlot subplot = (XYPlot) iterator.next(); 
                                               // set domain crosshair for each plot
            subplot.setDomainCrosshairValue(xx, true); 

            if (subplotIndex == index) 
            { 
                                     // this subplot has the range crosshair 
                                     // get the y axis positon 
                double yy = subplot.getRangeAxis().java2DToValue(mousePoint.getY(), panelArea, subplot.getRangeAxisEdge()); 
                                     // make sure the range crosshair is on 
                subplot.setRangeCrosshairVisible(true); 
                                     // and plot it 
                subplot.setRangeCrosshairValue(yy, true); 
            } 
            else 
            { 
                                    // this subplot does not have the range 
                                    // crosshair, make sure its off 
                subplot.setRangeCrosshairVisible(false); 
            } 

            index++; 
        } 
    } 
} 


nasr25
Posts: 8
Joined: Mon Jun 15, 2015 6:01 am
antibot: No, of course not.

Re: How to have Crosshairs on a CombinedDomainXYPlot

Post by nasr25 » Tue Sep 08, 2015 7:15 am

i have problem with

Code: Select all

CombinedDomainXYPlot cdPlot = this.chart.getCombinedPlot(); 
i get cannot find symbol
symbol : method getCombinedPlot()

pls what is solution ?

Naxter
Posts: 45
Joined: Thu Jun 26, 2014 8:24 am
antibot: No, of course not.
Location: Germany, Aachen

Re: How to have Crosshairs on a CombinedDomainXYPlot

Post by Naxter » Tue Sep 08, 2015 7:24 am

We need more than just one line of code to help you. Probably your "chart" variable does not have the this method.

Locked