1

I'm trying to build a realtime graph from data that updates every two seconds. If I use doubles as x-axis label there is no problem. However, using a custom DateFormatter to label the graph thausands of labels are shown, even if I set the number of horizontal labels to 10 or so.

Initializing of the graph:

graph0 = (GraphView) findViewById(R.id.realtimeGraph0);
// DateFormatter
df = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.GERMAN);

// legend
graph0.getLegendRenderer().setVisible(true);
graph0.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
graph0.setTitle("Realtime-Graph");
// set individual bounds
graph0.getViewport().setYAxisBoundsManual(true);
graph0.getViewport().setMinY(-15);
graph0.getViewport().setMaxY(15);
graph0.getViewport().setXAxisBoundsManual(true);
graph0.getViewport().setScalable(true);
graph0.getViewport().setScalableY(false);
graph0.getViewport().setScrollable(true);
graph0.getViewport().setScrollableY(false);
graph0.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(BluetoothScreen.this, df));
graph0.getGridLabelRenderer().setHumanRounding(false);
graph0.getGridLabelRenderer().setNumVerticalLabels(7);
graph0.getGridLabelRenderer().setNumHorizontalLabels(10);
graph0.getGridLabelRenderer().setHorizontalLabelsAngle(90);

// Graph starts with a line
graph0.addSeries(series_U);
graph0.addSeries(series_I);
graph0.addSeries(series_P); 
// set different colors of the series
series_U.setColor(Color.BLUE);
series_I.setColor(Color.GREEN);
series_P.setColor(Color.MAGENTA);
series_U.setTitle("Voltage");
series_I.setTitle("Current");
series_P.setTitle("Power");
series_U.setDrawDataPoints(true);
series_I.setDrawDataPoints(true);
series_P.setDrawDataPoints(true);

I get data from a bluetooth device every two seconds and add them to the graph:

mCalendar = Calendar.getInstance();
mDate = mCalendar.getTime();
series_U.appendData(new DataPoint(mDate, value0), true, 100);
series_I.appendData(new DataPoint(mDate, value1), true, 100);
series_P.appendData(new DataPoint(mDate, value2), true, 100);

I want to show all data so I set the Viewport to full range:

graph0.getViewport().setMinX(graph.getViewport().getMinX(true));
graph0.getViewport().setMaxX(graph.getViewport().getMaxX(true));

The graph looks like this: screenshot

As you can see there are so many labels that you can't even read them.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
lukasg
  • 11
  • 4

1 Answers1

0

While I'm not sure why the setNumHorizontalLabels(10) isn't working, I faced the same issue. If you are OK with removing the vertical grid lines:

your_graph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.HORIZONTAL);

which will also stack up to make the background black, then you can use a custom label formatter to buffer the axis labels. Try something like this:

graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
   int i = 0;
   @Override
   public String formatLabel(double value, boolean isValueX) {
       if (isValueX && int i == 100) {
           // show normal x values
           int i = 0;
           //format value here if  desired
           return value;
       } else if (isValueX){
           i++;
           return "";
       } else {
           // y axis labels
           return "" + (int) value;
       }
   }
});
Stoic_Observer
  • 159
  • 1
  • 14