/
Dates.java
69 lines (57 loc) · 2.2 KB
/
Dates.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.jjoe64.graphview_demos.examples;
import android.graphics.Color;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.LegendRenderer;
import com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter;
import com.jjoe64.graphview.series.BarGraphSeries;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview_demos.FullscreenActivity;
import com.jjoe64.graphview_demos.R;
import java.util.Calendar;
import java.util.Date;
/**
* Created by jonas on 10.09.16.
*/
public class Dates extends BaseExample {
private int mNumLabels;
public Dates(int numLabels) {
mNumLabels = numLabels;
}
public Dates() {
mNumLabels = 4;
}
@Override
public void onCreate(FullscreenActivity activity) {
GraphView graph = (GraphView) activity.findViewById(R.id.graph);
initGraph(graph);
}
@Override
public void initGraph(GraphView graph) {
// generate Dates
Calendar calendar = Calendar.getInstance();
Date d1 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d2 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d3 = calendar.getTime();
// you can directly pass Date objects to DataPoint-Constructor
// this will convert the Date to double via Date#getTime()
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(d1, 1),
new DataPoint(d2, 5),
new DataPoint(d3, 3)
});
graph.addSeries(series);
// set date label formatter
graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(graph.getContext()));
graph.getGridLabelRenderer().setNumHorizontalLabels(mNumLabels);
// set manual x bounds to have nice steps
graph.getViewport().setMinX(d1.getTime());
graph.getViewport().setMaxX(d3.getTime());
graph.getViewport().setXAxisBoundsManual(true);
// as we use dates as labels, the human rounding to nice readable numbers
// is not nessecary
graph.getGridLabelRenderer().setHumanRounding(false);
}
}