1 /*
2  * Copyright 2012 AndroidPlot.com
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 
17 package com.androidplot.demos.widget;
18 
19 import android.appwidget.AppWidgetManager;
20 import android.appwidget.AppWidgetProvider;
21 import android.content.Context;
22 import android.graphics.Bitmap;
23 import android.graphics.Color;
24 import android.widget.RemoteViews;
25 import com.androidplot.demos.R;
26 import com.androidplot.xy.XYSeries;
27 import com.androidplot.xy.LineAndPointFormatter;
28 import com.androidplot.xy.SimpleXYSeries;
29 import com.androidplot.xy.XYPlot;
30 
31 import java.util.Arrays;
32 
33 public class DemoAppWidgetProvider extends AppWidgetProvider {
34 
35     @Override
onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)36     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
37         for (int widgetId : appWidgetIds) {
38             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);
44 
45             // 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};
48 
49             // Turn the above arrays into XYSeries':
50             XYSeries series1 = new SimpleXYSeries(
51                     Arrays.asList(series1Numbers),          // SimpleXYSeries takes a List so turn our array into a List
52                     SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
53                     "Series1");                             // Set the display title of the series
54 
55             // same as above
56             XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");
57 
58             // Create a formatter to use for drawing a series using LineAndPointRenderer:
59             LineAndPointFormatter series1Format = new LineAndPointFormatter(
60                     Color.rgb(0, 200, 0),                   // line color
61                     Color.rgb(0, 100, 0),                   // point color
62                     null, null);                                  // fill color (none)
63 
64             // add a new series' to the xyplot:
65             plot.addSeries(series1, series1Format);
66 
67             // same as above:
68             plot.addSeries(series2,
69                     new LineAndPointFormatter(
70                             Color.rgb(0, 0, 200), Color.rgb(0, 0, 100), null, null));
71 
72 
73             // reduce the number of range labels
74             plot.setTicksPerRangeLabel(3);
75 
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();
81 
82             RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.demo_app_widget);
83             rv.setBitmap(R.id.imgView, "setImageBitmap", bmp);
84             appWidgetManager.updateAppWidget(widgetId, rv);
85         }
86     }
87 }
88