2020import android .appwidget .AppWidgetProvider ;
2121import android .content .Context ;
2222import android .graphics .Bitmap ;
23+ import android .graphics .Canvas ;
2324import android .graphics .Color ;
2425import android .widget .RemoteViews ;
2526import com .androidplot .demos .R ;
27+ import com .androidplot .ui .*;
28+ import com .androidplot .util .PixelUtils ;
2629import com .androidplot .xy .XYSeries ;
2730import com .androidplot .xy .LineAndPointFormatter ;
2831import com .androidplot .xy .SimpleXYSeries ;
@@ -36,15 +39,40 @@ public class DemoAppWidgetProvider extends AppWidgetProvider {
3639 public void onUpdate (Context context , AppWidgetManager appWidgetManager , int [] appWidgetIds ) {
3740 for (int widgetId : appWidgetIds ) {
3841 XYPlot plot = new XYPlot (context , "Widget Example" );
39- //plot.getLayoutParams().height = 100;
40- //plot.getLayoutParams().width = 100;
41- plot .measure (150 ,150 );
42- plot .layout (0 ,0 ,150 ,150 );
43- plot .setDrawingCacheEnabled (true );
42+ final int h = (int ) context .getResources ().getDimension (R .dimen .sample_widget_height );
43+ final int w = (int ) context .getResources ().getDimension (R .dimen .sample_widget_width );
4444
45+ plot .getGraphWidget ().setMargins (0 , 0 , 0 , 0 );
46+ plot .getGraphWidget ().setPadding (0 , 0 , 0 , 0 );
47+ plot .getGraphWidget ().getGridBox ().setPadding (20 , 25 , 0 , 20 );
48+ plot .getGraphWidget ().getDomainTickLabelPaint ().setTextSize (PixelUtils .spToPix (10 ));
49+ plot .getGraphWidget ().getRangeTickLabelPaint ().setTextSize (PixelUtils .spToPix (10 ));
50+ plot .getGraphWidget ().getDomainOriginTickLabelPaint ().setTextSize (PixelUtils .spToPix (10 ));
51+ plot .getGraphWidget ().getRangeOriginTickLabelPaint ().setTextSize (PixelUtils .spToPix (10 ));
52+
53+ plot .getGraphWidget ().position (0 , XLayoutStyle .ABSOLUTE_FROM_LEFT , 0 ,
54+ YLayoutStyle .ABSOLUTE_FROM_TOP , AnchorPosition .LEFT_TOP );
55+
56+ plot .getGraphWidget ().setSize (Size .FILL );
57+
58+ plot .getLayoutManager ().moveToTop (plot .getTitleWidget ());
59+
60+ plot .measure (w , h );
61+ plot .layout (0 , 0 , w , h );
62+
63+ /*plot.getGraphWidget().setMarginBottom(PixelUtils.dpToPix(40));
64+ plot.getGraphWidget().setMarginLeft(PixelUtils.dpToPix(80));
65+ plot.getGraphWidget().setPaddingLeft(PixelUtils.dpToPix(80));
66+ plot.setPlotMargins(0, 0, 0, 0);
67+ plot.getGraphWidget().position(0, XLayoutStyle.ABSOLUTE_FROM_LEFT, 0,
68+ YLayoutStyle.ABSOLUTE_FROM_TOP, AnchorPosition.LEFT_TOP);
69+
70+ plot.getGraphWidget().setSize(Size.FILL);*/
4571 // Create a couple arrays of y-values to plot:
46- Number [] series1Numbers = {1 , 8 , 5 , 2 , 7 , 4 };
47- Number [] series2Numbers = {4 , 6 , 3 , 8 , 2 , 10 };
72+ //Number[] series1Numbers = {1, 8, 5, 2, 7, 4};
73+ //Number[] series2Numbers = {4, 6, 3, 8, 2, 10};
74+ Number [] series1Numbers = {1 , 4 , 2 , 8 , 4 , 16 , 8 , 32 , 16 , 64 };
75+ Number [] series2Numbers = {5 , 2 , 10 , 5 , 20 , 10 , 40 , 20 , 80 , 40 };
4876
4977 // Turn the above arrays into XYSeries':
5078 XYSeries series1 = new SimpleXYSeries (
@@ -53,13 +81,14 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
5381 "Series1" ); // Set the display title of the series
5482
5583 // same as above
56- XYSeries series2 = new SimpleXYSeries (Arrays .asList (series2Numbers ), SimpleXYSeries .ArrayFormat .Y_VALS_ONLY , "Series2" );
84+ XYSeries series2 = new SimpleXYSeries (Arrays .asList (series2Numbers ),
85+ SimpleXYSeries .ArrayFormat .Y_VALS_ONLY , "Series2" );
5786
5887 // Create a formatter to use for drawing a series using LineAndPointRenderer:
5988 LineAndPointFormatter series1Format = new LineAndPointFormatter (
6089 Color .rgb (0 , 200 , 0 ), // line color
6190 Color .rgb (0 , 100 , 0 ), // point color
62- null , null ); // fill color (none)
91+ null , null ); // fill color (none)
6392
6493 // add a new series' to the xyplot:
6594 plot .addSeries (series1 , series1Format );
@@ -72,15 +101,16 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
72101
73102 // reduce the number of range labels
74103 plot .setTicksPerRangeLabel (3 );
104+ plot .setTicksPerDomainLabel (2 );
75105
76- // by default, AndroidPlot displays developer guides to aid in laying out your plot.
77- // To get rid of them call disableAllMarkup():
78- //plot.disableAllMarkup();
79-
80- Bitmap bmp = plot .getDrawingCache ();
106+ // hide the legend:
107+ plot .getLegendWidget ().setVisible (false );
81108
82109 RemoteViews rv = new RemoteViews (context .getPackageName (), R .layout .demo_app_widget );
83- rv .setBitmap (R .id .imgView , "setImageBitmap" , bmp );
110+
111+ Bitmap bitmap = Bitmap .createBitmap (w , h , Bitmap .Config .ARGB_8888 );
112+ plot .draw (new Canvas (bitmap ));
113+ rv .setImageViewBitmap (R .id .imgView , bitmap );
84114 appWidgetManager .updateAppWidget (widgetId , rv );
85115 }
86116 }
0 commit comments