Skip to content

Commit d9a2f40

Browse files
authored
Updates examples / docs for saving and restoring series data to the instance state. (halfhp#35)
* uprevd to latest, gradle, build tools etc. * Refactored examples and documentation to no longer use the SeriesRegistry to save/restore instance state. Added a simple example of saving/restoring just the series array data into TimeSeriesActivty. * fixed unit test location paths
1 parent 2ff46c3 commit d9a2f40

15 files changed

Lines changed: 110 additions & 110 deletions

File tree

androidplot-core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ android {
3838
lintOptions {
3939
abortOnError false
4040
}
41+
buildToolsVersion '25.0.0'
4142
}
4243

4344
group = 'com.androidplot'

androidplot-core/src/main/java/com/androidplot/xy/XYGraphWidget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public static class LineLabelRenderer {
135135
public void drawLabel(Canvas canvas, LineLabelStyle style, Number val, float x, float y, boolean isOrigin) {
136136
final int canvasState = canvas.save();
137137
try {
138-
final String txt = style.format.format(val.doubleValue());
138+
final String txt = style.format.format(val);
139139
canvas.rotate(style.getRotation(), x, y);
140140
drawLabel(canvas, txt, style.getPaint(), x, y, isOrigin);
141141
} finally {

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ buildscript {
3838
}
3939

4040
dependencies {
41-
classpath 'com.android.tools.build:gradle:2.2.0'
41+
classpath 'com.android.tools.build:gradle:2.3.0'
4242
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
4343
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
4444
classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.5.0'

circle.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ test:
3232

3333
# junit xml report:
3434
- mkdir -p $CIRCLE_TEST_REPORTS/junit-xml/
35-
- find . -type f -regex ".*/build/test-results/.*xml" -exec cp {} $CIRCLE_TEST_REPORTS/junit-xml/ \;
35+
- find . -type f -regex ".*/build/test-results/testReleaseUnitTest/.*xml" -exec cp {} $CIRCLE_TEST_REPORTS/junit-xml/ \;
3636

3737
# junit html report:
3838
# TODO: recursively copy subdirs etc
3939
- mkdir -p $CIRCLE_TEST_REPORTS/junit-html/
40-
- cp -r ${HOME}/${CIRCLE_PROJECT_REPONAME}/androidplot-core/build/reports/tests/release/* $CIRCLE_TEST_REPORTS/junit-html/
40+
- cp -r ${HOME}/${CIRCLE_PROJECT_REPONAME}/androidplot-core/build/reports/tests/testReleaseUnitTest/* $CIRCLE_TEST_REPORTS/junit-html/
4141

4242
# lint report:
4343
- mkdir -p $CIRCLE_TEST_REPORTS/lint/

demoapp-wearable/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ buildscript {
1919
jcenter()
2020
}
2121
dependencies {
22-
classpath 'com.android.tools.build:gradle:1.2.3'
22+
classpath 'com.android.tools.build:gradle:2.3.0'
2323
}
2424
}
2525
apply plugin: 'com.android.application'

demoapp/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ android {
7676
lintOptions {
7777
abortOnError false
7878
}
79+
buildToolsVersion '25.0.0'
7980
}
8081

8182
play {

demoapp/src/main/java/com/androidplot/demos/DemoApplication.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
import com.squareup.leakcanary.*;
66

7-
/**
8-
* Created by halfhp on 10/1/16.
9-
*/
107
public class DemoApplication extends Application {
118

129
@Override public void onCreate() {

demoapp/src/main/java/com/androidplot/demos/SimpleXYPlotActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void onCreate(Bundle savedInstanceState)
7272
PixelUtils.dpToPix(20),
7373
PixelUtils.dpToPix(15)}, 0));
7474

75-
// just for fun, add some smoothing to the lines:
75+
// (optional) add some smoothing to the lines:
7676
// see: http://androidplot.com/smooth-curves-and-androidplot/
7777
series1Format.setInterpolationParams(
7878
new CatmullRomInterpolator.Params(10, CatmullRomInterpolator.Type.Centripetal));

demoapp/src/main/java/com/androidplot/demos/TimeSeriesActivity.java

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,61 +20,60 @@
2020
import android.graphics.*;
2121
import android.os.Bundle;
2222

23+
import com.androidplot.util.PixelUtils;
2324
import com.androidplot.xy.SimpleXYSeries;
2425
import com.androidplot.xy.XYSeries;
2526
import com.androidplot.xy.*;
2627

2728
import java.text.*;
2829
import java.util.Arrays;
30+
import java.util.Calendar;
2931
import java.util.Date;
32+
import java.util.GregorianCalendar;
3033

3134
public 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
}

demoapp/src/main/java/com/androidplot/demos/TouchZoomExampleActivity.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,10 @@ public void onClick(View view) {
8585
// enable autoselect of sampling level based on visible boundaries:
8686
plot.getRegistry().setEstimator(new ZoomEstimator());
8787

88-
if(savedInstanceState != null && savedInstanceState.containsKey("seriesRegistry")) {
89-
XYSeriesRegistry registry = (XYSeriesRegistry) savedInstanceState.getSerializable("seriesRegistry");
90-
plot.setRegistry(registry);
91-
} else {
92-
generateSeriesData();
93-
}
88+
generateSeriesData();
9489
reset();
9590
}
9691

97-
@Override
98-
public void onSaveInstanceState(Bundle bundle) {
99-
bundle.putSerializable("seriesRegistry", plot.getRegistry());
100-
}
101-
10292
private void reset() {
10393
plot.setDomainBoundaries(0, 10000, BoundaryMode.FIXED);
10494
plot.setRangeBoundaries(0, 1000, BoundaryMode.FIXED);

0 commit comments

Comments
 (0)