1

我正在使用GraphView库在我正在构建的 Android 应用程序中绘制图形。

图表使用日期作为 x 轴。但是,使用DateFormat不允许我在带有换行符的水平标签上显示。这样做可以让我一次在屏幕上获得更多的点和标签。

这是图表当前的外观:

在此处输入图像描述

这就是我希望图表上的标签出现的方式:

在此处输入图像描述

我通过从SQLite数据库中获取日期来做到这一点。然后我将它们转换为字符串并将它们存储在一个数组中。然后将字符串格式化为SimpleDateFormat包含新行的 a。我已经调试了好几次,很明显数组中的每个字符串都包含这个新行。但是,当将水平标签设置为此字符串数组时,它会像在第一张图像中那样显示,而不是我喜欢的样子。

我的代码:

List<String> dates = new ArrayList<String>();
//getting the date Strings from the Sqlite Database
try {
    dates = db.getTimeDate();
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

//Creates the line break to add into string
String newline = System.getProperty("line.separator");

//Create date format that contains the line break
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy"+newline+"HH:mm:ss");

//Iterate through arraylist formatting the string to new date format that contains linebreak 
for (int i = 0; i > dates.size(); i++) {
    String datad = dateFormat.format(dates.get(i));
    dates.set(i, datad);
}

final String[] _date = dates.toArray(new String[dates.size()]);

// make graphdata
List<GraphViewData> graphdata = new ArrayList<GraphViewData>();
for (int i = 0; i < dates.size(); i++) {
    graphdata.add(new GraphViewData(i, _time[i]));
}

GraphViewData[] _graphdata = graphdata
        .toArray(new GraphViewData[graphdata.size()]);

GraphViewSeries exampleSeries = new GraphViewSeries("", new GraphViewSeriesStyle(
        getResources().getColor(R.color.mathleteRed), 2), _graphdata);

GraphView graphView = new LineGraphView(this, "");
graphView.setLayoutParams(new LayoutParams(320, 200));
graphView.addSeries(exampleSeries); // data
graphView.getGraphViewStyle().setTextSize(
        getResources().getDimension((R.dimen.graph_text_size)));
graphView.getGraphViewStyle().setNumHorizontalLabels(5);
graphView.setScrollable(true);
graphView.setShowHorizontalLabels(true);
((LineGraphView) graphView).setDrawDataPoints(true);
((LineGraphView) graphView).setDataPointsRadius(4);

graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
    public String formatLabel(double value, boolean isValueX) {
        if (isValueX) {
            // Assuming only 7 total values here
            return _date[(int) value];
        } else
            return String.format("%.2f", value);
    }
});

graphView.setViewPort(0, 4);
graphView.getGraphViewStyle().setHorizontalLabelsColor(
        getResources().getColor(R.color.white));

任何帮助都会很棒。我知道问题不在于SQLite数据库,因为我已经对其进行了多次调试和测试。

4

2 回答 2

0

在类GraphView.java中,方法drawHorizo​​ntalLabels是必须添加代码以换行的地方。我添加了另一种方法,以便仅在水平标签为日期格式时才换行,这样它就不会破坏任何不是日期的标签。

protected void drawHorizontalLabels(Canvas canvas, float border,
        float horstart, float height, String[] horlabels, float graphwidth) {

    // horizontal labels + lines
    int hors = horlabels.length - 1;
    for (int i = 0; i < horlabels.length; i++) {

        paint.setColor(graphViewStyle.getGridColor());
        float x = ((graphwidth / hors) * i) + horstart;
        if (graphViewStyle.getGridStyle() != GridStyle.VERTICAL) {
            canvas.drawLine(x, height - border, x, border, paint);
        }
        if (showHorizontalLabels) {
            paint.setTextAlign(Align.CENTER);
            if (i == horlabels.length - 1)
                paint.setTextAlign(Align.RIGHT);
            if (i == 0)
                paint.setTextAlign(Align.LEFT);
            paint.setColor(graphViewStyle.getHorizontalLabelsColor());


    //this is where I have added code.

            if (HorDates == true) {
                String[] Text = horlabels[i].split("\\s");

                canvas.drawText(Text[1], x, height - 25, paint);
                canvas.drawText(Text[0], x, height - 15, paint);
            } else {
                canvas.drawText(horlabels[i], x, height - 25, paint);
            }
        }
    }
}

但是,这仅在日期格式为日期和时间之间的空格时才有效,如下所示: 11/12/14 01:21:40

这样,代码会将标签文本在空格处拆分为两个字符串,然后将它们分别绘制在另一个之下。

还有一个简单的 set HorDates 方法:

public void isHorLabDates(boolean _HorDates) {
    this.HorDates = _HorDates;
}

水平标签现在打印在画布上,如下所示:

2014 年 11 月 12 日

01:21:40

于 2014-08-06T11:27:28.850 回答
0

现在有可能,感谢@appsthatmatter (jjoe64) 和@LDSDev,只需使用"\n"in DateFormat.

您甚至可以DateAsXAxisLabelFormatter使用日期作为标签示例中使用 as:

graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(
        getActivity(), new SimpleDateFormat("dd/MM/yy"+"\n"+"HH:mm:ss")));

GraphView 4.2.2测试。


或者,我建议您尝试下一个选项以显示具有特定倾斜角度的标签。在某些情况下,它允许以更紧凑和优雅的方式显示标签:

graph.getGridLabelRenderer().setHorizontalLabelsAngle(135)
于 2018-11-01T03:42:01.147 回答