private void onPlotClicked(PointF point) {

    // make sure the point lies within the graph area.  we use gridrect
    // because it accounts for margins and padding as well.
    if (plot.containsPoint(point.x, point.y)) {
      Number x = plot.getXVal(point);
      Number y = plot.getYVal(point);

      selection = null;
      double xDistance = 0;
      double yDistance = 0;

      // find the closest value to the selection:
      for (SeriesBundle<XYSeries, ? extends XYSeriesFormatter> sfPair :
          plot.getRegistry().getSeriesAndFormatterList()) {
        XYSeries series = sfPair.getSeries();
        for (int i = 0; i < series.size(); i++) {
          Number thisX = series.getX(i);
          Number thisY = series.getY(i);
          if (thisX != null && thisY != null) {
            double thisXDistance = Region.measure(x, thisX).doubleValue();
            double thisYDistance = Region.measure(y, thisY).doubleValue();
            if (selection == null) {
              selection = new Pair<>(i, series);
              xDistance = thisXDistance;
              yDistance = thisYDistance;
            } else if (thisXDistance < xDistance) {
              selection = new Pair<>(i, series);
              xDistance = thisXDistance;
              yDistance = thisYDistance;
            } else if (thisXDistance == xDistance
                && thisYDistance < yDistance
                && thisY.doubleValue() >= y.doubleValue()) {
              selection = new Pair<>(i, series);
              xDistance = thisXDistance;
              yDistance = thisYDistance;
            }
          }
        }
      }

    } else {
      // if the press was outside the graph area, deselect:
      selection = null;
    }

    if (selection == null) {
      selectionWidget.setText(NO_SELECTION_TXT);
    } else {
      selectionWidget.setText(
          "Selected: "
              + selection.second.getTitle()
              + " Value: "
              + selection.second.getY(selection.first));
    }
    plot.redraw();
  }
Beispiel #2
0
 /** @param title The title to display on this Plot. */
 public void setTitle(String title) {
   titleWidget.setText(title);
 }