Ejemplo n.º 1
0
  private void generateInitialLineData() {
    int numValues = 7;
    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_GREEN).setCubic(true);
    List<Line> lines = new ArrayList<Line>();
    lines.add(line);
    lineData = new LineChartData(lines);
    lineData.setAxisXBottom(new Axis(axisValues).setHasLines(true));
    lineData.setAxisYLeft(new Axis().setHasLines(true).setMaxLabelChars(3));
    chartTop.setLineChartData(lineData);
    // For build-up animation you have to disable viewport recalculation.
    chartTop.setViewportCalculationEnabled(false);

    // And set initial max viewport and current viewport- remember to set viewports after data.
    Viewport v = new Viewport(0, 110, 6, 0);
    chartTop.setMaximumViewport(v);
    chartTop.setCurrentViewport(v);

    chartTop.setZoomType(ZoomType.HORIZONTAL);
    generateLineData(ChartUtils.COLOR_GREEN, 100);
  }
 public LineChartData previewLineData() {
   LineChartData previewLineData = new LineChartData(lineData());
   previewLineData.setAxisYLeft(yAxis());
   previewLineData.setAxisXBottom(previewXAxis());
   previewLineData.getLines().get(4).setPointRadius(2);
   previewLineData.getLines().get(5).setPointRadius(2);
   previewLineData.getLines().get(6).setPointRadius(2);
   return previewLineData;
 }
    // 产生数据 记录
    //////////////////////////////////////////////////////////////////////////////////////////////////////
    private void generateDefaultData(int[] recordMsgBuffer) {
      int max = recordMsgBuffer[0];
      int min = recordMsgBuffer[0];
      List<PointValue> values = new ArrayList<PointValue>();

      for (int j = 0; j < recordMsgBuffer.length; j++) {
        values.add(new PointValue(j, (float) recordMsgBuffer[j] / 10f));

        if (recordMsgBuffer[j] > max) {
          max = recordMsgBuffer[j];
        }

        if (recordMsgBuffer[j] < min) {
          min = recordMsgBuffer[j];
        }
      }

      if (max - min < 50) {
        max = max + 25;
        min = min - 25;
      }

      Line line = new Line(values);
      line.setColor(ChartUtils.COLOR_GREEN);
      line.setHasPoints(false); // too many values so don't draw points.
      //            line.setFilled(true);

      List<PointValue> values1 = new ArrayList<PointValue>();
      values1.add(new PointValue(0, (float) max / 10f + 0.2f));
      Line line1 = new Line(values1);
      line1.setHasPoints(false); // too many values so don't draw points.
      line1.setPointRadius(0);

      List<PointValue> values2 = new ArrayList<PointValue>();
      values2.add(new PointValue(0, (float) min / 10f - 0.2f));
      Line line2 = new Line(values2);
      line2.setHasPoints(false); // too many values so don't draw points.
      line2.setPointRadius(0);

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

      data = new LineChartData(lines);
      data.setAxisXBottom(new Axis().setHasLines(true).setTextColor(ChartUtils.COLOR_GREEN));
      data.setAxisYLeft(
          new Axis().setHasLines(true).setMaxLabelChars(4).setTextColor(ChartUtils.COLOR_GREEN));

      // prepare preview data, is better to use separate deep copy for preview recordChart.
      // Set color to grey to make preview area more visible.
      previewData = new LineChartData(data);
      previewData.getLines().get(0).setColor(ChartUtils.DEFAULT_DARKEN_COLOR);
    }
    // 产生数据 实时
    /////////////////////////////////////////////////////////////////////////////////////////
    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();
    }
Ejemplo n.º 5
0
  public void addListToChart(List<Day> days) {
    int currentValue;
    int size = days.size();
    maxValueOfLineChartData = 0;
    List<PointValue> valuePoints = new ArrayList<PointValue>();

    if (size == 1) {
      for (int i = 0; i < 24; i++) {
        currentValue = days.get(0).getHours().get(i).getMmm().getMean();
        if (currentValue > maxValueOfLineChartData) {
          maxValueOfLineChartData = currentValue;
        }
        valuePoints.add(new PointValue(i, currentValue));
      }
    } else {
      for (int i = 0; i < size; i++) {
        currentValue = days.get(i).getMmm().getMean();
        if (currentValue > maxValueOfLineChartData) {
          maxValueOfLineChartData = currentValue;
        }
        valuePoints.add(new PointValue(i, currentValue));
      }
    }

    dataLine = new Line(valuePoints);
    dataLine.setColor(ChartUtils.COLOR_ORANGE);
    dataLine.setHasPoints(false);

    List<Line> lines = new ArrayList<Line>();
    lines.add(dataLine);

    data = new LineChartData(lines);
    data.setAxisXBottom(new Axis());
    data.setAxisYLeft(new Axis().setHasLines(true));

    previewData = new LineChartData(data);
    previewData.getLines().get(0).setColor(ChartUtils.DEFAULT_DARKEN_COLOR);

    chart.setLineChartData(data);
    previewChart.setLineChartData(previewData);

    updateChartView();
  }
    private LineChartData generateLineChartData() {
      int numValues = 20;

      List<PointValue> values = new ArrayList<PointValue>();
      for (int i = 0; i < numValues; ++i) {
        values.add(new PointValue(i, (float) Math.random() * 100f));
      }

      Line line = new Line(values);
      line.setColor(ChartUtils.COLOR_GREEN);

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

      LineChartData data = new LineChartData(lines);
      data.setAxisXBottom(new Axis().setName("Axis X"));
      data.setAxisYLeft(new Axis().setName("Axis Y").setHasLines(true));
      return data;
    }
    private LineChartData generatePreviewLineChartData() {
      int numValues = 50;

      List<PointValue> values = new ArrayList<PointValue>();
      for (int i = 0; i < numValues; ++i) {
        values.add(new PointValue(i, (float) Math.random() * 100f));
      }

      Line line = new Line(values);
      line.setColor(ChartUtils.DEFAULT_DARKEN_COLOR);
      line.setHasPoints(false); // too many values so don't draw points.

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

      LineChartData data = new LineChartData(lines);
      data.setAxisXBottom(new Axis());
      data.setAxisYLeft(new Axis().setHasLines(true));

      return data;
    }
  /** ************************** DATA POINTS *************************** */
  private void getForecastData() {

    List<PointValue> values = new ArrayList<PointValue>();
    // List<PointValue> values2 = new ArrayList<PointValue>();
    for (int i = 0; i < numData; ++i) {
      Log.d("VALUES", "Time: " + Integer.toString(times[i]));
      Log.d("VALUES", "Temp: " + Double.toString(temperatures[i]));

      values.add(new PointValue(times[i], (float) temperatures[i]));
      /*
      if (i==0 || i==6 || i==12 || i==15 || i==17 || i==20) {
          values2.add(new PointValue(times[i], (float) temperatures[i]+(float) (Math.random() - 0.50f)));
      } else {
          values2.add(new PointValue(times[i], values2.get(i-1).getY() + (float) (Math.random()/3 - 0.25f)));
      }*/
    }

    Line line = new Line(values);
    line.setColor(ChartUtils.COLOR_GREEN);
    line.setHasPoints(false); // too many values so don't draw points.
    List<Line> lines = new ArrayList<Line>();
    lines.add(line);
    /*
            Line line2 = new Line(values2);
            line2.setColor(ChartUtils.COLOR_BLUE);
            line2.setHasPoints(false);
            lines.add(line2);
    */
    data = new LineChartData(lines);
    data.setAxisXBottom(new Axis());
    data.setAxisYLeft(new Axis().setHasLines(true));

    // prepare preview data, is better to use separate deep copy for preview chart.
    // Set color to grey to make preview area more visible.
    previewData = new LineChartData(data);
    previewData.getLines().get(0).setColor(ChartUtils.DEFAULT_DARKEN_COLOR);
    //        previewData.getLines().get(1).setColor(ChartUtils.DEFAULT_DARKEN_COLOR);

  }
  private void generateDefaultData() {
    int numValues = 50;

    List<PointValue> values = new ArrayList<PointValue>();
    for (int i = 0; i < numValues; ++i) {
      values.add(new PointValue(i, (float) Math.random() * 100f));
    }

    Line line = new Line(values);
    line.setColor(ChartUtils.COLOR_GREEN);
    line.setHasPoints(false); // too many values so don't draw points.

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

    data = new LineChartData(lines);
    data.setAxisXBottom(new Axis());
    data.setAxisYLeft(new Axis().setHasLines(true));

    // prepare preview data, is better to use separate deep copy for preview chart.
    // Set color to grey to make preview area more visible.
    previewData = new LineChartData(data);
    previewData.getLines().get(0).setColor(ChartUtils.DEFAULT_DARKEN_COLOR);
  }
  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();
  }
 public LineChartData lineData() {
   LineChartData lineData = new LineChartData(defaultLines());
   lineData.setAxisYLeft(yAxis());
   lineData.setAxisXBottom(xAxis());
   return lineData;
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.graph);
    // generate Dates
    Calendar calendar = Calendar.getInstance();
    Date d1 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);
    Date d2 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);
    Date d3 = calendar.getTime();

    Date d4 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);
    Date d5 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);
    Date d6 = calendar.getTime();

    Date d7 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);
    Date d8 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);
    Date d9 = calendar.getTime();

    GraphView graph = (GraphView) findViewById(R.id.graph);

    // you can directly pass Date objects to DataPoint-Constructor
    // this will convert the Date to double via Date#getTime()
    LineGraphSeries<DataPoint> series =
        new LineGraphSeries<DataPoint>(
            new DataPoint[] {
              new DataPoint(d1, 1),
              new DataPoint(d2, 5),
              new DataPoint(d3, 3),
              new DataPoint(d4, 4),
              new DataPoint(d5, 6),
              new DataPoint(d6, 5),
              new DataPoint(d7, 7),
              new DataPoint(d8, 4),
              new DataPoint(d9, 6),
            });
    series.setColor(getResources().getColor(android.R.color.holo_green_dark));
    LineGraphSeries<DataPoint> series2 =
        new LineGraphSeries<DataPoint>(
            new DataPoint[] {
              new DataPoint(d1, 2),
              new DataPoint(d2, 3),
              new DataPoint(d3, 3),
              new DataPoint(d4, 4),
              new DataPoint(d5, 5),
              new DataPoint(d6, 5),
              new DataPoint(d7, 7),
              new DataPoint(d8, 4),
              new DataPoint(d9, 6),
            });
    series.setColor(getResources().getColor(android.R.color.holo_red_dark));
    graph.addSeries(series);
    graph.addSeries(series2);

    // set date label formatter
    graph
        .getGridLabelRenderer()
        .setLabelFormatter(new DateAsXAxisLabelFormatter(NativeGraphActivity.this));
    graph.getGridLabelRenderer().setNumHorizontalLabels(8); // only 4 because of the space

    // set manual x bounds to have nice steps
    graph.getViewport().setMinX(d1.getTime());
    graph.getViewport().setMaxX(d9.getTime());
    graph.getViewport().setXAxisBoundsManual(true);

    lecho.lib.hellocharts.view.LineChartView chartView = (LineChartView) findViewById(R.id.chart);
    List<PointValue> values = new ArrayList<PointValue>();
    values.add(new PointValue(0, 2));
    values.add(new PointValue(1, 4));
    values.add(new PointValue(2, 3));
    values.add(new PointValue(3, 4));

    // In most cased you can call data model methods in builder-pattern-like manner.
    Line line =
        new Line(values)
            .setColor(getResources().getColor(android.R.color.holo_red_dark))
            .setCubic(false);

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

    LineChartData data = new LineChartData();
    data.setLines(lines);
    Axis axis = new Axis();
    axis.setHasTiltedLabels(true);

    data.setAxisXBottom(new Axis().setAutoGenerated(true));
    data.setAxisYLeft(new Axis().setAutoGenerated(true));
    data.setBaseValue(20);

    chartView.setLineChartData(data);
  }