private void updateGraph(Cursor cursor) {
    // set up the goal line
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    int goalCalories = Integer.parseInt(prefs.getString("preference_calorie_goal", "2000"));
    List<PointValue> goalValues = new ArrayList<>();

    // set up the data
    List<PointValue> values = new ArrayList<>();
    cursor.moveToFirst();

    if (!cursor.isAfterLast()) {
      goalValues.add(new PointValue(getTimeFromRow(cursor), goalCalories));
    }

    while (cursor.isAfterLast() == false) {
      float value = cursor.getFloat(cursor.getColumnIndex("total"));
      // Log.d("graph", "adding " + value + " on " + sdf.format(getTimeFromRow(cursor)));
      values.add(new PointValue(getTimeFromRow(cursor), value));
      cursor.moveToNext();
    }

    cursor.moveToLast();
    goalValues.add(new PointValue(getTimeFromRow(cursor), goalCalories));

    List<Line> lines = new ArrayList<Line>();

    Line goalLine = new Line(goalValues).setColor(Color.parseColor("#ff0000"));
    lines.add(goalLine);

    Line line = new Line(values).setColor(Color.parseColor("#44a134")).setCubic(true);
    // line.setFilled(true);
    // line.setHasLabels(true);

    lines.add(line);

    LineChartData data = new LineChartData();
    data.setLines(lines);

    Axis timeAxis = new Axis();
    // timeAxis.setName("");
    // timeAxis.setTextColor(ChartUtils.COLOR_ORANGE);
    timeAxis.setMaxLabelChars(5);
    timeAxis.setFormatter(new TimestampAxisFormatter());
    timeAxis.setHasLines(true);
    timeAxis.setHasTiltedLabels(true);
    data.setAxisXBottom(timeAxis);

    Axis calorieAxis = new Axis();
    // weightAxis.setName("");
    // weightAxis.setTextColor(ChartUtils.COLOR_GREEN);
    calorieAxis.setMaxLabelChars(7);
    calorieAxis.setFormatter(new SimpleAxisValueFormatter().setAppendedText(" C".toCharArray()));
    calorieAxis.setHasLines(true);
    calorieAxis.setHasTiltedLabels(true);
    data.setAxisYLeft(calorieAxis);

    hellochart.setLineChartData(data);
    hellochart.setZoomType(ZoomType.HORIZONTAL);
    hellochart.setOnValueTouchListener(new ValueTouchListener());

    // set up zoom so we can see 0 calories
    Viewport viewport = new Viewport(hellochart.getMaximumViewport());
    viewport.bottom = 0;
    hellochart.setMaximumViewport(viewport);
    hellochart.setCurrentViewport(viewport);

    hellochart.invalidate();
  }
Example #2
0
  private void setupCharts() {
    bgGraphBuilder = new BgGraphBuilder(this);
    updateStuff = false;
    chart = (LineChartView) findViewById(R.id.chart);

    if (BgGraphBuilder.isXLargeTablet(getApplicationContext())) {
      ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) chart.getLayoutParams();
      params.topMargin = 130;
      chart.setLayoutParams(params);
    }

    chart.setZoomType(ZoomType.HORIZONTAL);

    // Transmitter Battery Level
    final Sensor sensor = Sensor.currentSensor();
    if (sensor != null
        && sensor.latest_battery_level != 0
        && sensor.latest_battery_level <= Constants.TRANSMITTER_BATTERY_LOW
        && !prefs.getBoolean("disable_battery_warning", false)) {
      Drawable background =
          new Drawable() {

            @Override
            public void draw(Canvas canvas) {

              DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
              int px = (int) (30 * (metrics.densityDpi / 160f));
              Paint paint = new Paint();
              paint.setTextSize(px);
              paint.setAntiAlias(true);
              paint.setColor(Color.parseColor("#FFFFAA"));
              paint.setStyle(Paint.Style.STROKE);
              paint.setAlpha(100);
              canvas.drawText(
                  "transmitter battery", 10, chart.getHeight() / 3 - (int) (1.2 * px), paint);
              if (sensor.latest_battery_level <= Constants.TRANSMITTER_BATTERY_EMPTY) {
                paint.setTextSize((int) (px * 1.5));
                canvas.drawText("VERY LOW", 10, chart.getHeight() / 3, paint);
              } else {
                canvas.drawText("low", 10, chart.getHeight() / 3, paint);
              }
            }

            @Override
            public void setAlpha(int alpha) {}

            @Override
            public void setColorFilter(ColorFilter cf) {}

            @Override
            public int getOpacity() {
              return 0;
            }
          };
      chart.setBackground(background);
    }
    previewChart = (PreviewLineChartView) findViewById(R.id.chart_preview);
    previewChart.setZoomType(ZoomType.HORIZONTAL);

    chart.setLineChartData(bgGraphBuilder.lineData());
    chart.setOnValueTouchListener(bgGraphBuilder.getOnValueSelectTooltipListener());
    previewChart.setLineChartData(bgGraphBuilder.previewLineData());
    updateStuff = true;

    previewChart.setViewportCalculationEnabled(true);
    chart.setViewportCalculationEnabled(true);
    previewChart.setViewportChangeListener(new ViewportListener());
    chart.setViewportChangeListener(new ChartViewPortListener());
    setViewport();
  }