I am working with achartengine in my app. They have added a feature to handle null values but in order to handle them properly you need MathHelper.NULL_VALUE to be in the place of a null value. The graph needs a List Double[] (list of double arrays) in order to properly display the data. My code loops through a cursor, populates a list of doubles, converts the list of doubles into a Double[] then finally adds the Double[] to my List Double[].
I tried putting an if/else statement in the loop saying if column_TEMP is null then cvalue=MathHelper.NULL_VALUE but it does the calculations before it is needed and the results were quite catastrophic (it failed). The graph did not populate properly having a single small line on the very bottom of the screen and only showed 2 points instead of 10. It cannot do the calculations for MathHelper.NULL_VALUE until after it is in the List Double[]. I am trying to figure out a way to change null values when it goes from Double[] to List Double[]. Here is the code I am currently using. As-is it makes null values show as 0 on the graph. Graphs using larger numbers can be quite messed up if one null value is there. You could go from several hundred all the way across to 0 out of nowhere then back to several hundred again. Any suggestions are very appreciated.
private List<Double[]> values;
private List<Double> cValue;
private Double[] Dvalue;
while (cursor.moveToNext()) {
try {
String cdate=null;
Double cvalue=null;
column_date = cursor.getColumnIndex(Provider.DATE);
column_value = cursor.getColumnIndex(Provider.TEMP);
cdate = cursor.getString(column_date);
cvalue = cursor.getDouble(column_value);
};
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy/MM/dd");
Date dateObj = curFormater.parse(cdate);
if (dateObj.getTime() >= startDate.getTime() &&
dateObj.getTime() <= endDate.getTime()) {
SimpleDateFormat postFormater = new SimpleDateFormat("yyyy,MM,dd");
String dateObj1 = postFormater.format(dateObj);
SimpleDateFormat finalFormater = new SimpleDateFormat("yyyy,MM,dd");
Date dateObj2 = finalFormater.parse(dateObj1);
cDates.add(dateObj2);
cValue.add(cvalue);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
dateValues = cDates.toArray(new Date[0]);
dates.add(dateValues);
Dvalue = (Double[]) cValue.toArray(new Double[0]);
values.add(Dvalue);
In theory this is what the array should look like but I have no clue how to input the MathHelper.NULL_VALUE into the existing array.
values.add(new double[] { 21.2, 21.5, 21.7, 21.5, 21.4, 21.4, 21.3, 21.1, 20.6, 20.3, 20.2,
19.9, 19.7, 19.6, 19.9, 20.3, 20.6, 20.9, 21.2, 21.6, 21.9, 22.1, 21.7, 21.5 });
values.add(new double[] { 1.9, 1.2, 0.9, 0.5, 0.1, -0.5, -0.6, MathHelper.NULL_VALUE,
MathHelper.NULL_VALUE, -1.8, -0.3, 1.4, 3.4, 4.9, 7.0, 6.4, 3.4, 2.0, 1.5, 0.9, -0.5,
MathHelper.NULL_VALUE, -1.9, -2.5, -4.3 });
Edit:
I figured out one issue. In my while loop I put this code
Double d=MathHelper.NULL_VALUE;
String s=Double.toString(d);
Log.i(getClass().getSimpleName(), s);
and it logs 0.0 but lower down by my chartfactory settings (outside the loop) I put the
same code and it returned 1.7976931348623157E308 as expected. Why
would the while loop cause it to return 0.0?
Ok the value showing as 0.0 was an ID10T error. stupid me had string = instead of string.equals. The main problem though was it didn’t like passing the MathHelper.NULL_VALUE in the original list as a Double. What I had to do was pass everything as a String to the list of strings then add that to the string array and finally I passed that to the list of string arrays. Once I had everything in the list of string arrays I had to come up with new code to convert the list of string arrays into a list of Double arrays and it worked perfect.
Thanks for the help Laurence 🙂