@Override
  public void onCreate(Bundle savedInstanceState) {

    // android boilerplate stuff
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dynamicxyplot_example);

    // get handles to our View defined in layout.xml:
    dynamicPlot = (XYPlot) findViewById(R.id.dynamicXYPlot);

    plotUpdater = new MyPlotUpdater(dynamicPlot);

    // only display whole numbers in domain labels
    dynamicPlot.getGraphWidget().setDomainValueFormat(new DecimalFormat("0"));

    // getInstance and position datasets:
    data = new SampleDynamicXYDatasource();
    SampleDynamicSeries sine1Series = new SampleDynamicSeries(data, 0, "Sine 1");
    SampleDynamicSeries sine2Series = new SampleDynamicSeries(data, 1, "Sine 2");

    LineAndPointFormatter formatter1 =
        new LineAndPointFormatter(Color.rgb(0, 0, 0), null, null, null);
    formatter1.getLinePaint().setStrokeJoin(Paint.Join.ROUND);
    formatter1.getLinePaint().setStrokeWidth(10);
    dynamicPlot.addSeries(sine1Series, formatter1);

    LineAndPointFormatter formatter2 =
        new LineAndPointFormatter(Color.rgb(0, 0, 200), null, null, null);
    formatter2.getLinePaint().setStrokeWidth(10);
    formatter2.getLinePaint().setStrokeJoin(Paint.Join.ROUND);

    // formatter2.getFillPaint().setAlpha(220);
    dynamicPlot.addSeries(sine2Series, formatter2);

    // hook up the plotUpdater to the data model:
    data.addObserver(plotUpdater);

    // thin out domain tick labels so they dont overlap each other:
    dynamicPlot.setDomainStepMode(XYStepMode.INCREMENT_BY_VAL);
    dynamicPlot.setDomainStepValue(5);

    dynamicPlot.setRangeStepMode(XYStepMode.INCREMENT_BY_VAL);
    dynamicPlot.setRangeStepValue(10);

    dynamicPlot.setRangeValueFormat(new DecimalFormat("###.#"));

    // uncomment this line to freeze the range boundaries:
    dynamicPlot.setRangeBoundaries(-100, 100, BoundaryMode.FIXED);

    // create a dash effect for domain and range grid lines:
    DashPathEffect dashFx =
        new DashPathEffect(new float[] {PixelUtils.dpToPix(3), PixelUtils.dpToPix(3)}, 0);
    dynamicPlot.getGraphWidget().getDomainGridLinePaint().setPathEffect(dashFx);
    dynamicPlot.getGraphWidget().getRangeGridLinePaint().setPathEffect(dashFx);
  }
Beispiel #2
0
  void generatePlot() {
    TextView tv = (TextView) findViewById(R.id.plotViewTitle);
    tv.setText(
        "Avistamientos durante " + meses[actualMonth - 1] + " del " + String.valueOf(actualYear));

    SQLiteDatabase myDB = null;

    myDB = this.openOrCreateDatabase(DB_NAME, 1, null);

    myDB.execSQL(
        "CREATE TABLE IF NOT EXISTS "
            + TABLE
            + " ("
            + ADDR_DB
            + " TEXT NOT NULL PRIMARY KEY, "
            + COORD_X_DB
            + " INTEGER NOT NULL, "
            + COORD_Y_DB
            + " INTEGER NOT NULL, "
            + INF_DB
            + " TEXT, "
            + DAY_DB
            + " INTEGER, "
            + MONTH_DB
            + " INTEGER, "
            + YEAR_DB
            + " INTEGER);");

    /*
     * Solo obtendremos los días donde se han visto avistamientos ordenados por menor a mayor.
     */
    String[] FROM = {DAY_DB};
    String ORDER_BY = YEAR_DB + " DESC, " + MONTH_DB + " DESC, " + DAY_DB + " DESC";

    Cursor c =
        myDB.query(
            TABLE,
            FROM,
            MONTH_DB + "=" + actualMonth + " AND " + YEAR_DB + "=" + actualYear,
            null,
            null,
            null,
            ORDER_BY);

    /*
     * Preparamos el vector de Number dependiendo del mes que tengamos que mostrar
     */
    Number[] series1Numbers;
    int daysOfMonth = 0;

    if (actualMonth == 1
        || actualMonth == 3
        || actualMonth == 5
        || actualMonth == 7
        || actualMonth == 8
        || actualMonth == 10
        || actualMonth == 12) {
      daysOfMonth = 32;
      series1Numbers = new Number[daysOfMonth];
      for (int i = 0; i < daysOfMonth; ++i) series1Numbers[i] = 0;
    } else if (actualMonth == 2) {
      daysOfMonth = 29;
      series1Numbers = new Number[daysOfMonth];
      for (int i = 0; i < daysOfMonth; ++i) series1Numbers[i] = 0;
    } else {
      daysOfMonth = 31;
      series1Numbers = new Number[daysOfMonth];
      for (int i = 0; i < daysOfMonth; ++i) series1Numbers[i] = 0;
    }

    int max = 0; // sirve para establecer el máximo Y valor del gráfico
    int aux = 0;
    int dayAux = 0;

    startManagingCursor(c);
    while (c.moveToNext()) {
      int dayDB = c.getInt(0);
      if (max < aux) max = aux;
      if (dayAux == 0) {
        dayAux = dayDB;
        ++aux;
      } else {
        if (dayAux == dayDB) ++aux;
        else {
          series1Numbers[dayAux] = aux;
          aux = 1;
          dayAux = dayDB;
        }
      }
    }
    if (dayAux != 0) series1Numbers[dayAux] = aux;

    c.close();

    if (myDB != null) {
      myDB.close();
    }

    // Inicializamos el objeto XYPlot búscandolo desde el layout:
    mySimpleXYPlot = (XYPlot) findViewById(R.id.plot);
    mySimpleXYPlot.clear();
    mySimpleXYPlot.setDomainLabel("Días de " + meses[actualMonth - 1]);
    mySimpleXYPlot.setRangeLabel("Número de avistamientos");

    // Añadimos Línea Número UNO:
    XYSeries series1 =
        new SimpleXYSeries(
            Arrays.asList(series1Numbers), // Array de datos
            SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Valores verticales
            "Número de avistamientos"); // Nombre de la serie

    // Modificamos los colores de la primera serie
    LineAndPointFormatter series1Format =
        new LineAndPointFormatter(
            Color.rgb(200, 0, 0), // Color de la l�nea
            Color.rgb(0, 0, 0), // Color del punto
            Color.rgb(190, 150, 150)); // Relleno

    // Una vez definida la serie (datos y estilo), la añadimos al panel
    mySimpleXYPlot.addSeries(series1, series1Format);

    if (max == 0) ++max;

    // Tuneamos el grafico
    mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("#"));
    mySimpleXYPlot.setDomainBoundaries(1, daysOfMonth - 1, BoundaryMode.FIXED);
    mySimpleXYPlot.setDomainStepValue(daysOfMonth);
    if (daysOfMonth == 32) {
      mySimpleXYPlot.setTicksPerDomainLabel(2);
    } else if (daysOfMonth == 31) {
      mySimpleXYPlot.setTicksPerDomainLabel(3);
    } else if (daysOfMonth == 28) {
      mySimpleXYPlot.setTicksPerDomainLabel(2);
    }
    mySimpleXYPlot.setRangeUpperBoundary(max + 2, BoundaryMode.FIXED);
    mySimpleXYPlot.setRangeStepValue(max + 3);
    mySimpleXYPlot.setTicksPerRangeLabel(1);
    mySimpleXYPlot.disableAllMarkup();

    /*
     * Generamos la vista de nueva para mostrar la nueva gráfica
     */
    ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
    sv.postInvalidate();
  }