我正在使用AChartEngine来实时绘制从Ubidots获取的值。该图表是实时检索值,但是当在Ubidots上没有发送任何值时,Ubidots上发送的最后一个检索值将再次被绘制,如屏幕截图所示。
以下是我的代码:
LineGraph.java:
public class LineGraph {
private GraphicalView view;
private TimeSeries dataset = new TimeSeries("LDR Values");
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used to customize line 1
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph
public LineGraph()
{
// Add single dataset to multiple dataset
mDataset.addSeries(dataset);
// Customization time for line 1!
renderer.setColor(Color.BLUE);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
renderer.setDisplayChartValues(true);
renderer.setChartValuesSpacing(10);
// mRenderer: renderer that controls the full charts and add the single renderer for each series:
// Enable Zoom
//mRenderer.setZoomButtonsVisible(true);
mRenderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
mRenderer.setXTitle("Day #");
mRenderer.setYTitle("LDR Values");
mRenderer.setYAxisMax(30000);
mRenderer.setYAxisMin(10000);
mRenderer.setShowGrid(true);
// Add single renderer to multiple renderer
mRenderer.addSeriesRenderer(renderer);
}
public GraphicalView getView(Context context)
{
view = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view;
}
public void addNewPoints(Point p)
{
dataset.add(p.getX(), p.getY());
}
}
Point.java:
public class Point {
private int x;
private int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private static GraphicalView view;
private LineGraph line = new LineGraph();
private static Thread thread;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
thread = new Thread() {
public void run()
{
for (int i = 0; i <1000; i++)
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point p = UbidotsData.getDataFromReceiver(i); // We got new data!
line.addNewPoints(p); // Add it to our graph
view.repaint();
}
}
};
thread.start();
}
@Override
protected void onStart() {
super.onStart();
view = line.getView(this);
setContentView(view);
}
}
UbidotsData.java
public class UbidotsData {
//private static Context context;
public static Point getDataFromReceiver(int x)
{
ApiClient api = new ApiClient("XXXXXXXXXXXXXXXXXXX");
Variable ldr = api.getVariable("XXXXXXXXXXXXXXXXXXX");
Value[] values = ldr.getValues();
int newValue = (int) values[0].getValue();
if(newValue>22000){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(Context.getContext(), notification);
mp.start();
}
return new Point(x, newValue);
}
}
此外,当我关闭应用程序并再次打开它时,它开始绘制先前从Ubidots检索的相同数据(当我在实时获取数据时运行程序时)。如何在图形中使数据永久化而不是在打开图形时将图形再次绘制?在这种情况下,我应该使用AsyncTask
而不是Thread
吗?
为了更清楚,这条线取出了值:
int newValue = (int) values[0].getValue();