I'm working on an app that parses through an XML file, collects dates and values, and plots this data with GraphView using a for loop that loops through and array list of data points. I can get the graph to generate the x and y axis with the dates and value range but no points are plotted.
I can see in the Android Monitor that the data points are being generated, they just aren't plotting. There must be something wrong with my code but I can't figure it out. Any ideas?
Update: I should add that my XML file has multiple values for the same date (data is collected every 15 minutes). My guess is that I need to average these values in the array so that there is only one value per date. This may be what's affecting the GraphView.
public class GraphActivity extends 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();
}
}