1

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();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

I figured it out. I started by looping through my arrays and creating new arrays without any duplicates. Then I used those arrays to create an array of DataPoints and added that to the graph:

            for (int j = 0; j < dateOnlyEntrySeries.size() - 1; j++) {
                String stringValue = valueOnlySeries.get(j).value;
                String stringDate = dateOnlyEntrySeries.get(j).date;
                String stringDate2 = dateOnlyEntrySeries.get(j + 1).date;

                finalDate = dateOnlyEntrySeries.get(dateOnlyEntrySeries.size() - 1).date;
                if (!(stringDate.equalsIgnoreCase(stringDate2))) {

                    array3.add(stringDate);
                    array4.add(stringValue);
                    n = n + 1;
                }
            }

            dp = new DataPoint[array3.size()];
            int arraySize = array3.size();

            for (int i = 0; i < array3.size(); i++) {
                Double newDoubleValue = Double.parseDouble(array4.get(i));
                String newStringDate = array3.get(i);

                dp[i] = new DataPoint(new SimpleDateFormat("yyyy-MM-dd").parse(newStringDate), newDoubleValue);
            }

            finalDoubleValue = Double.parseDouble(valueOnlySeries.get(valueOnlySeries.size() - 1).value);

            DataPoint finalDataPoint = new DataPoint(
                    new SimpleDateFormat("yyyy-MM-dd").parse(finalDate), finalDoubleValue);


            series = new LineGraphSeries<>(dp);

            series.appendData(finalDataPoint, true, 30);
            graphView.addSeries(series); 

It took some more tweaking with the GraphView settings to get the axis labeled correctly and the graph sized appropriately for the data but I got it working.