GraphView传感器数据的实时图

时间:2016-09-25 13:11:36

标签: android plot android-graphview

我正在使用GraphView Android Library绘制智能手机光传感器的数据。得到的图的Y值是正确的,但Graph不能绘制X轴的浮点值,X轴是获得读数的时间(以毫秒为单位)。它截断了值的小数部分,仅重新绘制了值1,2,3 ......它没有给出图形的实时外观。

这是我从传感器获取数据的代码:

 @Override
 public void onSensorChanged(SensorEvent event) {

 switch (event.sensor.getType())
 {
     case Sensor.TYPE_AMBIENT_TEMPERATURE:
         tempTv.setText((int)event.values[0]+"");
         break;

     case Sensor.TYPE_RELATIVE_HUMIDITY:
         humTv.setText((int)event.values[0]+"");
         break;

     case Sensor.TYPE_PRESSURE:
         presTv.setText((int)event.values[0]+"");
         break;

     case Sensor.TYPE_LIGHT: {
         lumTv.setText((int) event.values[0] + "");
         xPoint = (System.currentTimeMillis() - startTime)/1000L;
         lumValue = event.values[0];

         break;
     }
}

并在我的处理程序中更新GraphView的系列数据,我这样做:

mSeriesUpdater = new Runnable() {
         @Override
         public void run() {
             Log.i(TAG, "Time: " + Long.toString(xPoint) + " Sensor Value: " + lumValue);
             series.appendData(new DataPoint(xPoint, lumValue), true, 1000);
             mHandler.post(this);
         }
     };

    mHandler.postDelayed(mSeriesUpdater, 200);

另外作为额外的细节,这里是我对GraphView的设置:

   //Setting the GraphView main Layout
  mGraphView = (GraphView) v.findViewById(R.id.graphView);

    mGraphView.getGridLabelRenderer()
            .setHighlightZeroLines(false); //Otherwise a black ugly line appears for 0-point plot
    mGraphView.getGridLabelRenderer()
            .setGridStyle(GridLabelRenderer.GridStyle.HORIZONTAL);
  mGraphView.getGridLabelRenderer()
          .setNumVerticalLabels(15);
    mGraphView.getGridLabelRenderer()
            .setPadding(50);
    mGraphView.getGridLabelRenderer()
            .setNumHorizontalLabels(6);
    mGraphView.getGridLabelRenderer()
            .setPadding(100);

   //Formatting the Label

    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
    nf.setMinimumIntegerDigits(1);

    mGraphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(nf, nf));

  //Setting GraphView Viewport layouts
  Viewport mViewport =  mGraphView.getViewport();
    mViewport.setXAxisBoundsManual(true);
    mViewport.setMinX(1);
    mViewport.setMaxX(10);
    mViewport.setScrollable(true);
    mViewport.setMinY(0);



    //Setting up the series for the GraphView to display
    series = new LineGraphSeries<DataPoint>();
    series.setDrawBackground(true);
    series.setColor(Color.WHITE);
    series.setThickness(4);
    series.setBackgroundColor(0x40FFFFFF);
    mGraphView.addSeries(series);

    return v;

我在我的应用程序上获得的图表看起来像下面附带的图像。请注意,图表仅针对整数值绘制,缺少任何两个间隔之间的小数值。 enter image description here如果我在为GraphView系列提供正确的数据时遇到任何错误,或者在设置GraphView时出现任何错误,请告诉我。

0 个答案:

没有答案