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(); }
@Override protected void onPostExecute(Character result) { XYSeries series = new SimpleXYSeries(Arrays.asList(labels), Arrays.asList(downdata), "Downloaded"); XYSeries seriestotal = new SimpleXYSeries(Arrays.asList(labels), Arrays.asList(totaldata), "Uploaded"); BarFormatter downbarformatter = new BarFormatter(0xFF42D692, Color.BLACK); BarFormatter upbarformatter = new BarFormatter(0xFF388DFF, Color.BLACK); downbarformatter.getBorderPaint().setStrokeWidth(0); upbarformatter.getBorderPaint().setStrokeWidth(0); Paint gridpaint = new Paint(); gridpaint.setColor(Color.DKGRAY); gridpaint.setAntiAlias(true); gridpaint.setStyle(Paint.Style.STROKE); graph.getGraphWidget().setDomainGridLinePaint(gridpaint); graph.getGraphWidget().setRangeGridLinePaint(gridpaint); graph.setTicksPerDomainLabel(4); graph.addSeries(seriestotal, upbarformatter); graph.addSeries(series, downbarformatter); graph.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1800000); graph.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 30); graph.calculateMinMaxVals(); minXY = new PointD( graph.getCalculatedMinX().doubleValue(), graph.getCalculatedMinY().doubleValue()); // initial minimum data point absMinX = minXY.x; // absolute minimum data point // TODO: this crashes with a NPE sometimes. ?? // absolute minimum value for the domain boundary maximum Number temp = series.getX(1); if (temp == null) { errorMsg = "There was an error fetching or displaying your data usage."; detv.setText(errorMsg); d_pb_ll.setVisibility(View.GONE); dell.setVisibility(View.VISIBLE); return; } minNoError = Math.round(temp.doubleValue() + 2); maxXY = new PointD( graph.getCalculatedMaxX().doubleValue(), graph.getCalculatedMaxY().doubleValue()); // initial maximum data point graph.setTicksPerRangeLabel(maxXY.y > 61 ? 2 : 1); absMaxX = maxXY.x; // absolute maximum data point // absolute maximum value for the domain boundary minimum maxNoError = (double) Math.round(series.getX(series.size() - 1).doubleValue()) - 2; minDif = 3000000; maxDif = 33000000; graph.setRangeUpperBoundary(maxXY.y > 31 ? maxXY.y : 31, BoundaryMode.FIXED); graph.setRangeLowerBoundary(0, BoundaryMode.FIXED); graph.setDomainUpperBoundary(maxXY.x, BoundaryMode.FIXED); graph.setDomainLowerBoundary(minXY.x, BoundaryMode.FIXED); checkBoundaries(); graph.setDomainValueFormat(new TimeFormat()); graph.redraw(); graph.setVisibility(View.VISIBLE); d_pb_ll.setVisibility(View.GONE); Toast.makeText( DataUsageActivity.this, "Swipe up and down to zoom in and out", Toast.LENGTH_SHORT) .show(); }