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データベースに問題がないことはわかっています。