我正在使用MPAndroidChart 我需要实时显示折线图。但问题是,它在整个完成后显示。实际上它应该像实时一样。我试过的是,
private void updateUI(final ArrayList<TimeModel> pJsonObject) {
for(int i=0;i<pJsonObject.size();i++){
int speedRates = pJsonObject.get(i).getSpeedRate();
String time = String.valueOf(pJsonObject.get(i).getTotalTime());
xAxes.add(time);
//yAxes.add(new Entry(10, 0));
yAxes.add(new Entry(speedRates,i));
}
String[] xaxes = new String[xAxes.size()];
for (int i = 0; i < xAxes.size(); i++) {
xaxes[i] = xAxes.get(i).toString();
}
LineDataSet lineDataSet = new LineDataSet(yAxes, "Time");
lineDataSet.setDrawCircles(true);
lineDataSet.setColor(Color.GREEN);
lineDataSets.add(lineDataSet);
linechart.setData(new LineData(xaxes, lineDataSets));
linechart.setVisibleXRangeMaximum(65f);
// linechart.setTouchEnabled(true);
// linechart.setDragEnabled(true);
linechart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
linechart.getAxisLeft().setDrawGridLines(false);
linechart.getXAxis().setDrawGridLines(false);
YAxis yAxisRight = linechart.getAxisRight();
yAxisRight.setEnabled(false);
linechart.animateXY(3000, 3000);
}
但这是一个完整的阴谋。我需要在mpandroid图表中绘制实时移动图。
答案 0 :(得分:1)
这是代码。它启动线程,然后在循环中向条件添加条目(在这种情况下为500点),每次添加后,线程休眠1000毫秒(1秒),这是你的间隔:
var userLocation: CLLocation!
// Then, when your view loads or elswhere:
guard userLocation != nil else { return }
// Geocode the location for setting the user placemark.
let geocoder: CLGeocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(userLocation,
completionHandler: { (placemarks, error) -> Void in
DispatchQueue.main.async {
guard let placemarks = placemarks,
placemarks.count > 0 else { return }
self.setUserPlacemark(placemarks)
}
})
}
private func setUserPlacemark(_ placemarks: [CLPlacemark]) {
guard (placemarks.count > 0) else { return }
let placemark: CLPlacemark = placemarks[0]
if placemark.administrativeArea != nil
{
// Do something with placemark.administrativeArea
}
}
对于每个点的间隔为1秒的动画,请使用此
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 500; i++) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addEntry();
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}).start();
例如,在您的情况下:
mChart.animateX(1000 * pointCount);