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
How to have Crosshairs on a CombinedDomainXYPlot
Domain and Range Crosshairs on a CombinedDomainXYPlot
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:
My view is my mouse handler/listener:
The code to position the crosshairs is in the mouse movement handler for the ChartMouseListener (in my view):
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 .....
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++;
}
}
}
Domain and Range Crosshairs on a CombinedDomainXYPlot
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++;
}
}
}
Re: How to have Crosshairs on a CombinedDomainXYPlot
i have problem with
i get cannot find symbol
symbol : method getCombinedPlot()
pls what is solution ?
Code: Select all
CombinedDomainXYPlot cdPlot = this.chart.getCombinedPlot();
symbol : method getCombinedPlot()
pls what is solution ?
-
- 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
We need more than just one line of code to help you. Probably your "chart" variable does not have the this method.