// 产生数据 实时
    /////////////////////////////////////////////////////////////////////////////////////////
    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();
    }
        @Override
        public void onDataReceived(DataReadResult dataReadResult) {
          Log.d(TAG, "Received activity result");
          final String DATE_FORMAT = "dd/MM";
          SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

          List<Line> lines = new ArrayList<>(); // all lines
          List<PointValue> calories; // a single line
          calories = new ArrayList<>();
          int x_val = 0;
          List<AxisValue> xAxisValues = new ArrayList<>();

          if (dataReadResult.getBuckets().size() > 0) {
            for (Bucket bucket : dataReadResult.getBuckets()) {
              String date = dateFormat.format(bucket.getStartTime(TimeUnit.MILLISECONDS));
              DataSet dataSet = bucket.getDataSet(DataType.AGGREGATE_CALORIES_EXPENDED);
              Log.d(TAG, "New databucket with date: " + date);

              // Increase counter
              x_val = x_val + 1;

              // Set axis labels
              xAxisValues.add(new AxisValue(x_val).setLabel(date));

              // Add a zero measurement point
              calories.add(new PointValue(x_val, 0));

              List<DataPoint> dataPointList = dataSet.getDataPoints();
              for (DataPoint dp : dataPointList) {
                String timestamp = dateFormat.format(dp.getTimestamp(TimeUnit.MILLISECONDS));
                float calorie = dp.getValue(Field.FIELD_CALORIES).asFloat();

                // Set graph values
                calories.set(x_val - 1, new PointValue(x_val, calorie));
                Log.d(TAG, "timestamp: " + timestamp + ", calories: " + calorie);
              }
            }
            Line line = new Line(calories);
            line.setColor(ChartUtils.COLOR_BLUE);
            line.setHasLines(true);
            line.setHasPoints(false);
            line.setCubic(false);
            line.setHasLabels(false);
            line.setHasLabelsOnlyForSelected(true);
            lines.add(line);

            fitnessFragment.setCalorieData(xAxisValues, lines);
          }
        }
Beispiel #3
0
 public Line setSquare(boolean isSquare) {
   this.isSquare = isSquare;
   if (isCubic) setCubic(false);
   return this;
 }
        @Override
        public void onDataReceived(DataReadResult dataReadResult) {
          Log.d(TAG, "Received activity result");
          final String DATE_FORMAT = "dd/MM";
          SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
          // GoogleApiSubscription.printData(dataReadResult);

          List<Line> lines = new ArrayList<>(); // all lines
          List<PointValue> walking = new ArrayList<>(); // a single line
          List<PointValue> biking = new ArrayList<>();
          List<PointValue> in_vehicle = new ArrayList<>();
          List<AxisValue> xAxisValues = new ArrayList<>();

          int x_val = 0;

          if (dataReadResult.getBuckets().size() > 0) {
            for (Bucket bucket : dataReadResult.getBuckets()) {
              String date = dateFormat.format(bucket.getStartTime(TimeUnit.MILLISECONDS));
              DataSet dataSet = bucket.getDataSet(DataType.AGGREGATE_ACTIVITY_SUMMARY);
              Log.d(TAG, "New databucket with date: " + date);

              // Increase counter
              x_val = x_val + 1;

              // Set axis labels
              xAxisValues.add(new AxisValue(x_val).setLabel(date));

              // Add a zero measurement point
              walking.add(new PointValue(x_val, 0));
              biking.add(new PointValue(x_val, 0));
              in_vehicle.add(new PointValue(x_val, 0));

              List<DataPoint> dataPointList = dataSet.getDataPoints();
              for (DataPoint dp : dataPointList) {
                String timestamp = dateFormat.format(dp.getTimestamp(TimeUnit.MILLISECONDS));
                String activity = dp.getValue(Field.FIELD_ACTIVITY).asActivity();
                int duration = dp.getValue(Field.FIELD_DURATION).asInt();

                if (dp.getValue(Field.FIELD_ACTIVITY).asActivity() == "walking") {
                  walking.set(x_val - 1, new PointValue(x_val, duration / 60000));
                } else if (dp.getValue(Field.FIELD_ACTIVITY).asActivity() == "biking") {
                  biking.set(x_val - 1, new PointValue(x_val, duration / 60000));
                } else if (dp.getValue(Field.FIELD_ACTIVITY).asActivity() == "in_vehicle") {
                  in_vehicle.set(x_val - 1, new PointValue(x_val, duration / 60000));
                }
                Log.d(
                    TAG,
                    "timestamp: "
                        + timestamp
                        + ", activity: "
                        + activity
                        + ", duration: "
                        + duration / 60000);
              }
            }
            Line line = new Line(walking);
            line.setColor(ChartUtils.COLOR_GREEN);
            line.setHasLines(true);
            line.setHasPoints(false);
            line.setCubic(false);
            lines.add(line);

            line = new Line(biking);
            line.setColor(ChartUtils.COLOR_BLUE);
            line.setHasLines(true);
            line.setHasPoints(false);
            line.setCubic(false);
            lines.add(line);

            line = new Line(in_vehicle);
            line.setColor(ChartUtils.COLOR_VIOLET);
            line.setHasLines(true);
            line.setHasPoints(false);
            line.setCubic(false);
            lines.add(line);

            fitnessFragment.setActivityData(xAxisValues, lines);
          }
        }