Ejemplo n.º 1
0
  public Axis yAxis() {
    Axis yAxis = new Axis();
    yAxis.setAutoGenerated(false);
    List<AxisValue> axisValues = new ArrayList<AxisValue>();

    //        for(int j = 1; j <= 12; j += 1) {
    //            if (doMgdl) {
    //                axisValues.add(new AxisValue(j * 50));
    //            } else {
    //                axisValues.add(new AxisValue(j*2));
    //            }
    //        }
    axisValues.add(new AxisValue(240));
    axisValues.add(new AxisValue((int) this.lowMark));
    axisValues.add(new AxisValue((int) this.highMark));
    yAxis.setValues(axisValues);
    yAxis.setHasLines(true);
    yAxis.setLineColor(Color.parseColor("#555555"));
    yAxis.setMaxLabelChars(5);
    yAxis.setInside(true);
    yAxis.setTextSize(axisTextSize);
    yAxis.setTextColor(Color.parseColor("#8D8D8D"));
    yAxis.setHasTiltedLabels(true);

    return yAxis;
  }
    // 产生数据 实时
    /////////////////////////////////////////////////////////////////////////////////////////
    private void generateInitialLineData() {
      int numValues = 19;

      List<AxisValue> axisValues = new ArrayList<AxisValue>();
      List<PointValue> values = new ArrayList<PointValue>();
      for (int i = 0; i < numValues; ++i) {
        values.add(new PointValue(i, 0));
        axisValues.add(new AxisValue(i).setLabel(days[i]));
      }

      Line line = new Line(values);
      line.setColor(ChartUtils.COLOR_BLUE);
      line.setHasPoints(false);
      line.getShape();
      line.setCubic(false); // 平滑

      List<Line> lines = new ArrayList<Line>();
      lines.add(line);
      //            lines.add(line1);
      //            lines.add(line2);

      Axis axisX = new Axis(axisValues);
      Axis axisY = new Axis().setHasLines(true);
      if (hasAxesNames) {
        axisX.setName("时间间隔");
        // axisY.setName("温度");
        axisX.setTextColor(ChartUtils.HOLO_BLUE_DARK);
        axisY.setTextColor(ChartUtils.HOLO_BLUE_DARK);
      }

      lineData = new LineChartData(lines);
      lineData.setAxisXBottom(axisX.setHasLines(true));
      lineData.setAxisYLeft(axisY.setMaxLabelChars(4));

      realeChartTop.setLineChartData(lineData);

      // For build-up animation you have to disable viewport recalculation.
      // 建立动画你必须禁用视图重新计算。
      realeChartTop.setViewportCalculationEnabled(false);

      // And set initial max viewport and current viewport- remember to set viewports after data.
      // 设置初始值和当前视口视口记得要在数据集视图。
      Viewport v = new Viewport(0, 50, 20, -10);
      realeChartTop.setMaximumViewport(v);
      realeChartTop.setCurrentViewport(v);
      realeChartTop.setZoomType(ZoomType.HORIZONTAL);

      // toggleFilled();
      // toggleLabels();
      toggleAxesNames();
    }
  ///////// AXIS RELATED//////////////
  public Axis yAxis() {
    Axis yAxis = new Axis();
    yAxis.setAutoGenerated(false);
    List<AxisValue> axisValues = new ArrayList<AxisValue>();

    for (int j = 1; j <= 12; j += 1) {
      if (doMgdl) {
        axisValues.add(new AxisValue(j * 50));
      } else {
        axisValues.add(new AxisValue(j * 2));
      }
    }
    yAxis.setValues(axisValues);
    yAxis.setHasLines(true);
    yAxis.setMaxLabelChars(5);
    yAxis.setInside(true);
    yAxis.setTextSize(axisTextSize);
    return yAxis;
  }
  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();
  }