Java Code Examples for org.jfree.chart.plot.XYPlot#setRangeCrosshairValue()

The following examples show how to use org.jfree.chart.plot.XYPlot#setRangeCrosshairValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FunctionPanel.java    From opensim-gui with Apache License 2.0 4 votes vote down vote up
public void updateCrosshairs(int screenX, int screenY) {
   XYPlot xyPlot = getChart().getXYPlot();
   if (showCrosshairs == true) {
      RectangleEdge xAxisLocation = xyPlot.getDomainAxisEdge();
      RectangleEdge yAxisLocation = xyPlot.getRangeAxisEdge();
      Rectangle2D dataArea = getScreenDataArea();
      double crosshairX = xyPlot.getDomainAxis().java2DToValue((double)screenX, dataArea, xAxisLocation);
      double crosshairY = xyPlot.getRangeAxis().java2DToValue((double)screenY, dataArea, yAxisLocation);
      if (crosshairX < xyPlot.getDomainAxis().getLowerBound() ||
          crosshairX > xyPlot.getDomainAxis().getUpperBound() ||
          crosshairY < xyPlot.getRangeAxis().getLowerBound() ||
          crosshairY > xyPlot.getRangeAxis().getUpperBound()) {
         xyPlot.setDomainCrosshairVisible(false);
         xyPlot.setRangeCrosshairVisible(false);
         crosshairAnnotation.setText("");
      } else {
         xyPlot.setDomainCrosshairVisible(true);
         xyPlot.setRangeCrosshairVisible(true);
         /** The xyPlot's crosshairs are updated with the screen coordinates of the XY location.
          * The annotation's text and location is updated with the data coordinates.
          * The format used for displaying the XY data coordinates is taken from the
          * format used for the X and Y axis tick labels. This format is not normally
          * accessible here, but a method was added to NumberTickUnit to provide it.
          * If this turns out to be problematic, the format could always be changed
          * to use a fixed number of significant digits.
          */
         NumberAxis dna = (NumberAxis)xyPlot.getDomainAxis();
         NumberFormat dnf = (NumberFormat)dna.getTickUnit().getFormatter().clone();
         dnf.setMaximumFractionDigits(dnf.getMaximumFractionDigits()+3);
         String xString = dnf.format(crosshairX);
         NumberAxis rna = (NumberAxis)xyPlot.getRangeAxis();
         NumberFormat rnf = (NumberFormat)rna.getTickUnit().getFormatter().clone();
         rnf.setMaximumFractionDigits(rnf.getMaximumFractionDigits()+3);
         String yString = rnf.format(crosshairY);
         crosshairAnnotation.setText("(" + xString + ", " + yString + ")");
         crosshairAnnotation.setX(crosshairX);
         crosshairAnnotation.setY(crosshairY);
         xyPlot.setDomainCrosshairValue(crosshairX);
         xyPlot.setRangeCrosshairValue(crosshairY);
         // JPL 11/19/09: the chart's plotInfo does not appear to be updated when the
         // FunctionPanel is resized. So the following call to handleClick will pass in
         // the wrong plot area for calculating crosshair coordinates. So instead, set
         // the crosshair directly with the two lines above this comment.
         //xyPlot.handleClick(screenX, screenY, this.getChartRenderingInfo().getPlotInfo());
      }
   }
}