예제 #1
0
 /**
  * Sets the x axis range so all the values fit the way I expect
  *
  * @param index
  */
 public void fitData(int index) {
   XYSeries series = mChart.getDataset().getSeriesAt(index);
   double barLength = (series.getMaxX() - series.getMinX()) / series.getItemCount();
   mChart.getRenderer().setXAxisMax(series.getMaxX() + barLength / 2);
   mChart.getRenderer().setXAxisMin(series.getMinX() - barLength / 2);
 }
  private void setGraphicalView(@Nullable PlotBoundaries plotBoundaries) {
    double minValue = plotBoundaries == null ? DEFAULT_MIN_NUMBER : plotBoundaries.xMin;
    double maxValue = plotBoundaries == null ? DEFAULT_MAX_NUMBER : plotBoundaries.xMax;

    final ViewGroup graphContainer = (ViewGroup) findViewById(R.id.plot_view_container);

    if (graphicalView != null) {
      graphContainer.removeView(graphicalView);
    }

    chart = prepareChart(minValue, maxValue, expression, variable);

    // reverting boundaries (as in prepareChart() we add some cached values )
    double minX = Double.MAX_VALUE;
    double minY = Double.MAX_VALUE;

    double maxX = Double.MIN_VALUE;
    double maxY = Double.MIN_VALUE;

    for (XYSeries series : chart.getDataset().getSeries()) {
      minX = Math.min(minX, series.getMinX());
      minY = Math.min(minY, series.getMinY());
      maxX = Math.max(maxX, series.getMaxX());
      maxY = Math.max(maxY, series.getMaxY());
    }

    Log.d(
        CalculatorPlotActivity.class.getName(),
        "min x: " + minX + ", min y: " + minY + ", max x: " + maxX + ", max y: " + maxY);
    Log.d(CalculatorPlotActivity.class.getName(), "Plot boundaries are " + plotBoundaries);

    if (plotBoundaries == null) {
      chart.getRenderer().setXAxisMin(Math.max(minX, minValue));
      chart.getRenderer().setYAxisMin(Math.max(minY, minValue));
      chart.getRenderer().setXAxisMax(Math.min(maxX, maxValue));
      chart.getRenderer().setYAxisMax(Math.min(maxY, maxValue));
    } else {
      chart.getRenderer().setXAxisMin(plotBoundaries.xMin);
      chart.getRenderer().setYAxisMin(plotBoundaries.yMin);
      chart.getRenderer().setXAxisMax(plotBoundaries.xMax);
      chart.getRenderer().setYAxisMax(plotBoundaries.yMax);
    }

    graphicalView = new GraphicalView(this, chart);

    graphicalView.addZoomListener(
        new ZoomListener() {
          @Override
          public void zoomApplied(ZoomEvent e) {
            updateDataSets(chart);
          }

          @Override
          public void zoomReset() {
            updateDataSets(chart);
          }
        },
        true,
        true);

    graphicalView.addPanListener(
        new PanListener() {
          @Override
          public void panApplied() {
            Log.d(TAG, "org.achartengine.tools.PanListener.panApplied");
            updateDataSets(chart);
          }
        });
    graphContainer.addView(graphicalView);

    updateDataSets(chart, 50);
  }