2626import android .widget .ArrayAdapter ;
2727import android .widget .ListView ;
2828import com .androidplot .Plot ;
29- import com .androidplot .xy . XYSeries ;
30- import com .androidplot .xy . LineAndPointFormatter ;
31- import com .androidplot .xy . SimpleXYSeries ;
32- import com .androidplot .xy .XYPlot ;
29+ import com .androidplot .Series ;
30+ import com .androidplot .ui . SeriesAndFormatter ;
31+ import com .androidplot .util . PixelUtils ;
32+ import com .androidplot .xy .* ;
3333
3434import java .util .ArrayList ;
3535import java .util .List ;
@@ -41,21 +41,64 @@ public class ListViewActivity extends Activity {
4141 private static final int NUM_SERIES_PER_PLOT = 5 ;
4242 private ListView lv ;
4343
44+ private List <List <SeriesAndFormatter <XYSeries , LineAndPointFormatter >>> seriesData = new ArrayList <>(NUM_PLOTS );
45+
4446 public void onCreate (Bundle savedInstanceState ) {
4547 super .onCreate (savedInstanceState );
4648 setContentView (R .layout .listview_example );
49+ PixelUtils .init (this );
50+ generateData ();
4751 lv = (ListView ) findViewById (R .id .listView1 );
4852 lv .setAdapter (new MyViewAdapter (getApplicationContext (), R .layout .listview_example_item , null ));
4953 }
5054
55+ protected void generateData () {
56+ Random generator = new Random ();
57+ for (int i = 0 ; i < NUM_PLOTS ; i ++) {
58+ List <SeriesAndFormatter <XYSeries , LineAndPointFormatter >> seriesList
59+ = new ArrayList <>(NUM_SERIES_PER_PLOT );
60+
61+ for (int k = 0 ; k < NUM_SERIES_PER_PLOT ; k ++) {
62+ ArrayList <Number > nums = new ArrayList <>();
63+ for (int j = 0 ; j < NUM_POINTS_PER_SERIES ; j ++) {
64+ nums .add (generator .nextFloat ());
65+ }
66+
67+ double rl = Math .random ();
68+ double gl = Math .random ();
69+ double bl = Math .random ();
70+
71+ double rp = Math .random ();
72+ double gp = Math .random ();
73+ double bp = Math .random ();
74+
75+ LineAndPointFormatter lpf = new LineAndPointFormatter (
76+ Color .rgb (new Double (rl * 255 ).intValue (),
77+ new Double (gl * 255 ).intValue (), new Double (bl * 255 ).intValue ()),
78+ Color .rgb (new Double (rp * 255 ).intValue (),
79+ new Double (gp * 255 ).intValue (), new Double (bp * 255 ).intValue ()),
80+ null , null );
81+
82+ // for fun, configure interpolation on the formatter:
83+ lpf .setInterpolationParams (
84+ new CatmullRomInterpolator .Params (20 , CatmullRomInterpolator .Type .Centripetal ));
85+
86+ seriesList .add (new SeriesAndFormatter <XYSeries , LineAndPointFormatter >(
87+ new SimpleXYSeries (nums , SimpleXYSeries .ArrayFormat .Y_VALS_ONLY , "S" + k ),
88+ lpf ));
89+ }
90+ seriesData .add (seriesList );
91+ }
92+ }
93+
5194 class MyViewAdapter extends ArrayAdapter <View > {
5295 public MyViewAdapter (Context context , int resId , List <View > views ) {
5396 super (context , resId , views );
5497 }
5598
5699 @ Override
57100 public int getCount () {
58- return 5 ;
101+ return NUM_PLOTS ;
59102 }
60103
61104 @ Override
@@ -68,29 +111,12 @@ public View getView(int pos, View convertView, ViewGroup parent) {
68111 }
69112
70113 Plot p = (XYPlot ) v .findViewById (R .id .xyplot );
71- Random generator = new Random ();
72-
114+ p .clear ();
73115 p .setTitle ("plot" + pos );
74116
75- for (int k = 0 ; k < NUM_SERIES_PER_PLOT ; k ++) {
76- ArrayList <Number > nums = new ArrayList <Number >();
77- for (int j = 0 ; j < NUM_POINTS_PER_SERIES ; j ++) {
78- nums .add (generator .nextFloat ());
79- }
80-
81- double rl = Math .random ();
82- double gl = Math .random ();
83- double bl = Math .random ();
84-
85- double rp = Math .random ();
86- double gp = Math .random ();
87- double bp = Math .random ();
88-
89- XYSeries series = new SimpleXYSeries (nums , SimpleXYSeries .ArrayFormat .Y_VALS_ONLY , "S" + k );
90- p .addSeries (series , new LineAndPointFormatter (
91- Color .rgb (new Double (rl * 255 ).intValue (), new Double (gl * 255 ).intValue (), new Double (bl * 255 ).intValue ()),
92- Color .rgb (new Double (rp * 255 ).intValue (), new Double (gp * 255 ).intValue (), new Double (bp * 255 ).intValue ()),
93- null , null ));
117+ List <SeriesAndFormatter <XYSeries , LineAndPointFormatter >> thisSeriesList = seriesData .get (pos );
118+ for (SeriesAndFormatter <XYSeries , LineAndPointFormatter > sf : thisSeriesList ) {
119+ p .addSeries (sf .getSeries (), sf .getFormatter ());
94120 }
95121 p .redraw ();
96122 return v ;
0 commit comments