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