2020import android .graphics .*;
2121import android .os .Bundle ;
2222
23+ import com .androidplot .util .PixelUtils ;
2324import com .androidplot .xy .SimpleXYSeries ;
2425import com .androidplot .xy .XYSeries ;
2526import com .androidplot .xy .*;
2627
2728import java .text .*;
2829import java .util .Arrays ;
30+ import java .util .Calendar ;
2931import java .util .Date ;
32+ import java .util .GregorianCalendar ;
3033
3134public class TimeSeriesActivity extends Activity {
3235
36+ private static final String SERIES_TITLE = "Signthings in USA" ;
37+
3338 private XYPlot plot1 ;
39+ private SimpleXYSeries series ;
3440
3541 @ Override
3642 public void onCreate (Bundle savedInstanceState ) {
3743 super .onCreate (savedInstanceState );
3844 setContentView (R .layout .time_series_example );
3945
4046 plot1 = (XYPlot ) findViewById (R .id .plot1 );
41- Number [] numSightings = {5 , 8 , 6 , 9 , 3 , 8 , 5 };
42-
43- // an array of years in milliseconds:
44- Number [] years = {
45- 978307200 , // 2001
46- 998309300 ,
47- 1009843200 , // 2002
48- 1041379200 , // 2003
49- 1052012100 ,
50- 1072915200 , // 2004
51- 1104537600 // 2005
47+
48+ // these will be our domain index labels:
49+ final Date [] years = {
50+ new GregorianCalendar (2001 , Calendar .JANUARY , 1 ).getTime (),
51+ new GregorianCalendar (2001 , Calendar .JULY , 1 ).getTime (),
52+ new GregorianCalendar (2002 , Calendar .JANUARY , 1 ).getTime (),
53+ new GregorianCalendar (2002 , Calendar .JULY , 1 ).getTime (),
54+ new GregorianCalendar (2003 , Calendar .JANUARY , 1 ).getTime (),
55+ new GregorianCalendar (2003 , Calendar .JULY , 1 ).getTime (),
56+ new GregorianCalendar (2004 , Calendar .JANUARY , 1 ).getTime (),
57+ new GregorianCalendar (2004 , Calendar .JULY , 1 ).getTime (),
58+ new GregorianCalendar (2005 , Calendar .JANUARY , 1 ).getTime (),
59+ new GregorianCalendar (2005 , Calendar .JULY , 1 ).getTime ()
5260 };
53- // create our series from our array of nums:
54- XYSeries series2 = new SimpleXYSeries (
55- Arrays .asList (years ),
56- Arrays .asList (numSightings ),
57- "Sightings in USA" );
61+
62+ addSeries (savedInstanceState );
63+
64+ plot1 .setRangeBoundaries (0 , 10 , BoundaryMode .FIXED );
5865
5966 plot1 .getGraph ().getGridBackgroundPaint ().setColor (Color .WHITE );
6067 plot1 .getGraph ().getDomainGridLinePaint ().setColor (Color .BLACK );
6168 plot1 .getGraph ().getDomainGridLinePaint ().
62- setPathEffect (new DashPathEffect (new float [] {1 , 1 }, 1 ));
69+ setPathEffect (new DashPathEffect (new float []{1 , 1 }, 1 ));
6370 plot1 .getGraph ().getRangeGridLinePaint ().setColor (Color .BLACK );
6471 plot1 .getGraph ().getRangeGridLinePaint ().
65- setPathEffect (new DashPathEffect (new float [] {1 , 1 }, 1 ));
72+ setPathEffect (new DashPathEffect (new float []{1 , 1 }, 1 ));
6673 plot1 .getGraph ().getDomainOriginLinePaint ().setColor (Color .BLACK );
6774 plot1 .getGraph ().getRangeOriginLinePaint ().setColor (Color .BLACK );
6875
69- // setup our line fill paint to be a slightly transparent gradient:
70- Paint lineFill = new Paint ();
71- lineFill .setAlpha (200 );
72-
73- LineAndPointFormatter formatter =
74- new LineAndPointFormatter (Color .rgb (0 , 0 , 0 ), Color .BLUE , Color .RED , null );
75- formatter .setFillPaint (lineFill );
7676 plot1 .getGraph ().setPaddingRight (2 );
77- plot1 .addSeries (series2 , formatter );
7877
7978 // draw a domain tick for each year:
8079 plot1 .setDomainStep (StepMode .SUBDIVIDE , years .length );
@@ -93,17 +92,15 @@ public void onCreate(Bundle savedInstanceState) {
9392 // create a simple date format that draws on the year portion of our timestamp.
9493 // see http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
9594 // for a full description of SimpleDateFormat.
96- private SimpleDateFormat dateFormat = new SimpleDateFormat ("MM- yyyy" );
95+ private final SimpleDateFormat dateFormat = new SimpleDateFormat ("MMM yyyy" );
9796
9897 @ Override
99- public StringBuffer format (Object obj , StringBuffer toAppendTo ,
100- FieldPosition pos ) {
101-
102- // because our timestamps are in seconds and SimpleDateFormat expects milliseconds
103- // we multiply our timestamp by 1000:
104- long timestamp = ((Number ) obj ).longValue () * 1000 ;
105- Date date = new Date (timestamp );
106- return dateFormat .format (date , toAppendTo , pos );
98+ public StringBuffer format (Object obj , StringBuffer toAppendTo , FieldPosition pos ) {
99+
100+ // this rounding is necessary to avoid precision loss when converting from
101+ // double back to int:
102+ int yearIndex = (int ) Math .round (((Number ) obj ).doubleValue ());
103+ return dateFormat .format (years [yearIndex ], toAppendTo , pos );
107104 }
108105
109106 @ Override
@@ -113,4 +110,44 @@ public Object parseObject(String source, ParsePosition pos) {
113110 }
114111 });
115112 }
113+
114+ /**
115+ * Instantiates our XYSeries, checking the current savedInstanceState for existing series data
116+ * to avoid having to regenerate on each resume. If your series data is small and easy to
117+ * regenerate (as it is here) then you can skip saving/restoring your series data to
118+ * savedInstanceState.
119+ * @param savedInstanceState Current saved instance state, if any; may be null.
120+ */
121+ private void addSeries (Bundle savedInstanceState ) {
122+ Number [] yVals ;
123+
124+ if (savedInstanceState != null ) {
125+ yVals = (Number []) savedInstanceState .getSerializable (SERIES_TITLE );
126+ } else {
127+ yVals = new Number []{5 , 8 , 6 , 9 , 3 , 8 , 5 , 4 , 7 , 4 };
128+ }
129+
130+ // create our series from our array of nums:
131+ series = new SimpleXYSeries (Arrays .asList (yVals ),
132+ SimpleXYSeries .ArrayFormat .Y_VALS_ONLY , SERIES_TITLE );
133+
134+ LineAndPointFormatter formatter =
135+ new LineAndPointFormatter (Color .rgb (0 , 0 , 0 ), Color .RED , Color .RED , null );
136+ formatter .getVertexPaint ().setStrokeWidth (PixelUtils .dpToPix (10 ));
137+ formatter .getLinePaint ().setStrokeWidth (PixelUtils .dpToPix (5 ));
138+
139+ // setup our line fill paint to be a slightly transparent gradient:
140+ Paint lineFill = new Paint ();
141+ lineFill .setAlpha (200 );
142+
143+ formatter .setFillPaint (lineFill );
144+
145+ plot1 .addSeries (series , formatter );
146+ }
147+
148+ @ Override
149+ public void onSaveInstanceState (Bundle bundle ) {
150+ // persist our series data so we don't have to regenerate each time:
151+ bundle .putSerializable (SERIES_TITLE , series .getyVals ().toArray (new Number []{}));
152+ }
116153}
0 commit comments