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?
Drawing on top of an XYPlot
Re: Drawing on top of an XYPlot
Would you draw a sketch on paint or something showing what exactly you want?
Re: Drawing on top of an XYPlot
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
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;
}