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(); }
private void updatePlot(SeriesSize seriesSize) { // Remove all current series from each plot plot.clear(); // Setup our Series with the selected number of elements series1 = new SimpleXYSeries( Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Us"); series2 = new SimpleXYSeries( Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Them"); plot.setDomainBoundaries(-1, series1.size(), BoundaryMode.FIXED); plot.setRangeUpperBoundary( SeriesUtils.minMax(series1, series2).getMaxY().doubleValue() + 1, BoundaryMode.FIXED); if (seriesSize != null) { switch (seriesSize) { case TEN: plot.setDomainStep(StepMode.INCREMENT_BY_VAL, 2); break; case TWENTY: plot.setDomainStep(StepMode.INCREMENT_BY_VAL, 4); break; case SIXTY: plot.setDomainStep(StepMode.INCREMENT_BY_VAL, 6); break; } } // add a new series' to the xyplot: if (series1CheckBox.isChecked()) plot.addSeries(series1, formatter1); if (series2CheckBox.isChecked()) plot.addSeries(series2, formatter2); // Setup the BarRenderer with our selected options MyBarRenderer renderer = plot.getRenderer(MyBarRenderer.class); renderer.setBarOrientation((BarRenderer.BarOrientation) spRenderStyle.getSelectedItem()); final BarRenderer.BarGroupWidthMode barGroupWidthMode = (BarRenderer.BarGroupWidthMode) spWidthStyle.getSelectedItem(); renderer.setBarGroupWidth( barGroupWidthMode, barGroupWidthMode == BarRenderer.BarGroupWidthMode.FIXED_WIDTH ? sbFixedWidth.getProgress() : sbVariableWidth.getProgress()); if (BarRenderer.BarOrientation.STACKED.equals(spRenderStyle.getSelectedItem())) { plot.getInnerLimits().setMaxY(15); } else { plot.getInnerLimits().setMaxY(0); } plot.redraw(); }