问题
I am using GraphView Android Library to plot the data of Light Sensor of my Smartphone. The Y-values of the resulting Graph are correct but the Graph cannot plot the floating point values of the X-axis which is the time in milliseconds at which it gets the reading. It truncates the fractional part of the valueand plots only agains the values 1,2,3... which doesn't give the realtime look to the graph.
Here's my code to get data from the Sensor:
@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;
}
}
and in my handler which updates the series data for GraphView, I am doing this:
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);
Also as extra detail, here are my settings for the 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;
The Graph I get on my App looks like the image attached below. Notice that the graph is only plotted for Integer values missing the fractional values in between any two intervals. Please let me know if I am doing any mistake in providing the correct data for GraphView series or is there any mistake in the setting up of the GraphView.
来源:https://stackoverflow.com/questions/39687208/graphview-realtime-plot-of-sensor-data