2

I attempt to show a tersely formatted date/time on the x-axis in a graphview chart. As per the API Code examples, I set HumanRounding to false when using using a date formatter on that axis. I'm also setting the NumHorizontalLabels to 3 in order to display reasonably OK in both orientations.

This results in e.g. the following, where the date labels show as a black shape, and the LineChart background is different. I'm speculating that the black shape is the result of all my date data points overwriting each other:

With HumanRounding set to false

With HumanRounding set to true (commented out), I get labels showing, but instead of the expected 3 evenly distributed labels, they are unpredictably spread out and/or not equal to 3, sometimes the labels over-write each other, sometimes they are bunched on the left...

with human rounding true

The number of date data-points on the x-axis can vary depending on how much history the user has selected. Note that this can vary from 60 to thousands of minutes.

Here's the code that receives data and charts it. Note that the unixdate retrieved from wxList elements has already been converted to a Java date (by multiplying by 1000) by the time they get used here (the time portion of the x-axis are in fact correct when they do show up in a reasonably distributed manner):

       protected void onPostExecute(List<WxData> wxList) {

            // We will display MM/dd HH:mm on the x-axes on all graphs...
            SimpleDateFormat shortDateTime = new SimpleDateFormat("MM/dd HH:mm");
            shortDateTime.setTimeZone(TimeZone.getTimeZone("America/Toronto"));
            DateAsXAxisLabelFormatter xAxisFormat = new DateAsXAxisLabelFormatter(parentContext, shortDateTime);

            if (wxList == null || wxList.isEmpty()) {
                makeText(parentContext,
                        "Could not retrieve data from server",
                        Toast.LENGTH_LONG).show();
            } else {

                // Temperature Celcius
                GraphView tempGraph = findViewById(R.id.temp_graph);
                tempGraph.removeAllSeries();
                tempGraph.setTitle(parentContext.getString(R.string.temp_graph_label));
                DataPoint[] tempCArray = new DataPoint[wxList.size()];
                for (int i = 0; i < wxList.size(); i++) {
                    tempCArray[i] = new DataPoint(wxList.get(i).getUnixtime(), wxList.get(i).getTempC().doubleValue());
                }
                LineGraphSeries<DataPoint> tempCSeries = new LineGraphSeries<>(tempCArray);
                tempGraph.addSeries(tempCSeries);
                tempGraph.getGridLabelRenderer().invalidate(false, false);
                tempGraph.getGridLabelRenderer().setLabelFormatter(xAxisFormat);
                tempGraph.getGridLabelRenderer().setNumHorizontalLabels(3);
                tempGraph.getViewport().setMinX(wxList.get(0).getUnixtime());
                tempGraph.getViewport().setMaxX(wxList.get(wxList.size() - 1).getUnixtime());
                tempGraph.getViewport().setXAxisBoundsManual(true);
                // Code below seems buggy - with humanRounding, X-axis turns black
//                tempGraph.getGridLabelRenderer().setHumanRounding(false);
...

I have tried many variations,but I cannot get the graph to consistently display 3 datetimes evenly spread out, for both orientations, for varyings sample sizes. Any help is appreciated.

RBH
  • 261
  • 1
  • 10

0 Answers0