【问题标题】:GraphView Horizontal Date Labels unable to Line BreakGraphView 水平日期标签无法换行
【发布时间】:2014-07-09 16:48:51
【问题描述】:

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

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

这是图表当前的样子:

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

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

我的代码:

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 数据库,因为我已经对其进行了多次调试和测试。

【问题讨论】:

  • 我为此创建了一个问题。 github.com/jjoe64/GraphView/issues/191
  • 嘿伙计,我实际上想出了自己的解决方案。生病把它放在下面的答案中,看看我是否可以将它上传到 GitHub,这样你就可以在你的代码中实现它,如果你愿意的话。我还添加了一些其他可能有用的东西。

标签: java android simpledateformat line-breaks android-graphview


【解决方案1】:

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

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

还有一个简单的设置 HorDates 方法:

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

水平标签现在像这样在画布中打印:

2014 年 11 月 12 日

01:21:40

【讨论】:

    【解决方案2】:

    感谢@appsthatmatter (jjoe64) 和@LDSDev,现在可以在DateFormat 中使用"\n"

    您甚至可以像Use dates as labels 示例中那样使用DateAsXAxisLabelFormatter

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

    使用 GraphView 4.2.2 测试。


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

    graph.getGridLabelRenderer().setHorizontalLabelsAngle(135)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 2012-05-24
      • 2012-11-13
      相关资源
      最近更新 更多