1

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

4

2 に答える 2

0

@appsthatmatter (jjoe64) と @LDSDev のおかげで"\n"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 に答える