【发布时间】:2017-11-17 14:11:20
【问题描述】:
我正在开发一个应用程序,该应用程序通过 XML 文件进行解析,收集日期和值,并使用 GraphView 使用循环遍历数据点数组列表的 for 循环绘制这些数据。我可以让图表生成带有日期和值范围的 x 和 y 轴,但没有绘制点。
我可以在 Android 监视器中看到正在生成数据点,它们只是没有在绘图。我的代码一定有问题,但我无法弄清楚。有什么想法吗?
更新:我应该补充一点,我的 XML 文件在同一日期有多个值(每 15 分钟收集一次数据)。我的猜测是我需要对数组中的这些值进行平均,以便每个日期只有一个值。这可能是影响 GraphView 的原因。
公共类 GraphActivity 扩展 AppCompatActivity {
GraphView graphView;
LineGraphSeries<DataPoint> series;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
graphView = (GraphView) findViewById(R.id.graph);
try {
InputStream is = GraphActivity.this.getResources()
.getAssets().open("smallmonth.xml");
ArrayList<GraphDateEntry> dateOnlyEntrySeries = new GraphDateParsing2().parse(is);
InputStream is1 = GraphActivity.this.getResources()
.getAssets().open("smallmonth.xml");
ArrayList<GraphValueEntry> valueOnlySeries = new GraphValueParsing().parse(is1);
for (int i = 0; i < dateOnlyEntrySeries.size(); i++) {
Double value = Double.parseDouble(valueOnlySeries.get(i).value);
String stringDate = dateOnlyEntrySeries.get(i).date;
DataPoint dateAndValue = new DataPoint(new SimpleDateFormat("yyyy-MM-dd").parse(stringDate), value);
series = new LineGraphSeries<>();
series.appendData(dateAndValue, true, dateOnlyEntrySeries.size());
}
graphView.addSeries(series);
series.setColor(Color.BLACK);
series.setThickness(8);
graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
@Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX) {
Log.d("formatLabel: ", sdf.format(new Date((long) value)));
return sdf.format(new Date((long) value));
} else {
return super.formatLabel(value, isValueX);
}
}
});
} catch (IOException ie) {
ie.printStackTrace();
} catch (ParseException pe) {
pe.printStackTrace();
}
}
【问题讨论】:
标签: android for-loop android-graphview linegraph