How to plot real time line graph

2019-08-03 23:29发布

I am creating sound meter app in android I want to show values in line graph on real time anyone please tell me how I can do it?

3条回答
Juvenile、少年°
2楼-- · 2019-08-03 23:37

in onCreate(...):

GraphView graphView = new LineGraphView(this, "Amplitude");
graphView.setScrollable(true);
graphView.setViewPort(0, 30); // the x range you want to show without scrolling
GraphViewSeries graphSeries = new GraphViewSeries(new GraphViewDataInterface[0]);
graphView.addSeries(graphSeries);
setContentView(graphView);
//or add your graphView to the Activity's view as appropriate

then, as you get new values:

graphSeries.appendData(new GraphView.GraphViewData(xValue, yValue), true, 300); 
//where 300 is the maximum number of values to keep

New values will come in on the right, and the GraphView will automatically scroll to show the most recent data on the chart.

查看更多
在下西门庆
3楼-- · 2019-08-03 23:44

When using AChartEngine, you just need to add the new values to the series and call mChartView.repaint() in order to have the chart refresh with the new updates.

查看更多
仙女界的扛把子
4楼-- · 2019-08-03 23:57

For time operations please see android.text.format.Time http://developer.android.com/reference/android/text/format/Time.html

This is official sample code from there:

Time time = new Time();
time.set(4, 10, 2007);  // set the date to Nov 4, 2007, 12am
time.normalize(false);       // this sets isDst = 1
time.monthDay += 1;     // changes the date to Nov 5, 2007, 12am
millis = time.toMillis(false);   // millis is Nov 4, 2007, 11pm
millis = time.toMillis(true);    // millis is Nov 5, 2007, 12am

Now for showing the graph there is no ready class, so you will have to draw it yourself by overriding the Draw() function of View class, or to search a library to do this, for example: http://android-graphview.org/

查看更多
登录 后发表回答