graphview太多标签

时间:2018-03-07 10:37:00

标签: android labels android-graphview

我尝试根据每两秒更新一次的数据构建实时图表。如果我使用双打作为x轴标签,没有问题。但是,即使我将水平标签的数量设置为10左右,也会显示使用自定义DateFormatter来标记图形的标签。

初始化图表:

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);

我每两秒钟从蓝牙设备获取数据并将它们添加到图表中:

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);

我想显示所有数据,因此我将视口设置为全范围:

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

图表如下所示: screenshot

正如您所看到的那样,有许多标签甚至无法读取它们。

1 个答案:

答案 0 :(得分:0)

虽然我不确定setNumHorizontalLabels(10)为何不起作用,但我遇到了同样的问题。如果您可以删除垂直网格线,则可以:

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

也会堆叠起来使背景变黑,然后可以使用自定义标签格式化程序来缓冲轴标签。尝试这样的事情:

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;
       }
   }
});