Drawing on top of an XYPlot

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Ted Hill
Posts: 54
Joined: Mon Jun 12, 2006 7:39 pm

Drawing on top of an XYPlot

Post by Ted Hill » Fri Dec 03, 2010 5:11 pm

Hello,

I have an XYPlot with two curves on it.

Here's what I want to do:

Move the mouse along one of the curves.

As the mouse moves,
1. draw a horizontal line connecting the two curves.
2. draw a vertical line connecting the two curves.

As the mouse is moved the horizontal and vertical lines should keep moving to track the mouse.

Any suggestions how to get started with this?
Ted Hill

barbarius
Posts: 74
Joined: Tue Jul 27, 2010 9:33 am
antibot: No, of course not.

Re: Drawing on top of an XYPlot

Post by barbarius » Fri Dec 03, 2010 5:47 pm

Would you draw a sketch on paint or something showing what exactly you want?

Ted Hill
Posts: 54
Joined: Mon Jun 12, 2006 7:39 pm

Re: Drawing on top of an XYPlot

Post by Ted Hill » Fri Dec 03, 2010 6:23 pm

Sure, but how do I attach a sketch on this forum?
Ted Hill

barbarius
Posts: 74
Joined: Tue Jul 27, 2010 9:33 am
antibot: No, of course not.

Re: Drawing on top of an XYPlot

Post by barbarius » Mon Dec 06, 2010 11:21 am

upload it on somewhere (i.e. http://www.imageshack.com) and just send the link here

btw i read the question again and i might have an idea now (even though a sketch would be better)
On chartMouseMoved event (where you will get by creating a class which implements ChartMouseListener interface and added to the listeners for the chart) use and algorithm to find where your curves are and till where your lines will go to and then you might use an XYLineAndShapeRenderer to draw a 3 point line graph OR you can use 2 XYLineAnnotation s to show the point of your cursor with lines to curves.

here s my finding closest point algorithm (which is not the best on performance) i hope you can find something useful for you inside

Code: Select all

private int findClosestIndex(DataPoint2D point) {
        double distance = Double.MAX_VALUE, distanceOld;
        boolean up=true,down=true;
        int count = dataSet2D.getDataCount(), best = oldPointIndex;    //private int oldPointIndex; this variable is the boost the performance, its starts searching for a new closest point from this last index
        XDataset = dataSet2D.getX();   //private double[] XDataset;
        
        distanceOld = Math.abs( Math.abs(point.getX()) - Math.abs(XDataset[oldPointIndex]) );
        
        for(int i = 1; i<=oldPointIndex; i++) {
            if(down) {
                distance = Math.abs( point.getX() - XDataset[oldPointIndex - i] );
                if(distanceOld < distance) {
                    down = false;
                }
                else {
                    distanceOld = distance;
                    up = false;
                    best = oldPointIndex - i;
                }
            }
            else break;
        }
        
        for(int i = 1; i<count-oldPointIndex; i++) {        
            if(up) {
                distance = Math.abs( point.getX() - XDataset[oldPointIndex + i] );
                if(distanceOld < distance) {
                    up = false;
                }
                else {
                    distanceOld = distance;
                    best = oldPointIndex + i;
                }
            }
            else break;
        }
        
        oldPointIndex = best;
        return best;
    }

Ted Hill
Posts: 54
Joined: Mon Jun 12, 2006 7:39 pm

Re: Drawing on top of an XYPlot

Post by Ted Hill » Mon Dec 06, 2010 3:29 pm

Thanks for the advice; I'll give it a try.
Ted Hill

Locked