16224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala/*
26224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala * Copyright 2012 AndroidPlot.com
36224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *
46224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *    Licensed under the Apache License, Version 2.0 (the "License");
56224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *    you may not use this file except in compliance with the License.
66224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *    You may obtain a copy of the License at
76224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *
86224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *        http://www.apache.org/licenses/LICENSE-2.0
96224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *
106224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *    Unless required by applicable law or agreed to in writing, software
116224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *    distributed under the License is distributed on an "AS IS" BASIS,
126224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *    See the License for the specific language governing permissions and
146224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala *    limitations under the License.
156224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala */
166224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
176224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalapackage com.androidplot.demos.widget;
186224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
196224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport android.appwidget.AppWidgetManager;
206224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport android.appwidget.AppWidgetProvider;
216224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport android.content.Context;
226224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport android.graphics.Bitmap;
236224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport android.graphics.Color;
246224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport android.widget.RemoteViews;
256224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport com.androidplot.demos.R;
266224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport com.androidplot.xy.XYSeries;
276224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport com.androidplot.xy.LineAndPointFormatter;
286224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport com.androidplot.xy.SimpleXYSeries;
296224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport com.androidplot.xy.XYPlot;
306224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
316224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalaimport java.util.Arrays;
326224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
336224eda509d436a575f801942337da92a6c18767Eino-Ville Talvalapublic class DemoAppWidgetProvider extends AppWidgetProvider {
346224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
356224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala    @Override
366224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
376224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala        for (int widgetId : appWidgetIds) {
386224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            XYPlot plot = new XYPlot(context, "Widget Example");
396224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            //plot.getLayoutParams().height = 100;
406224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            //plot.getLayoutParams().width = 100;
416224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            plot.measure(150,150);
426224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            plot.layout(0,0,150,150);
436224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            plot.setDrawingCacheEnabled(true);
446224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
456224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            // Create a couple arrays of y-values to plot:
466224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            Number[] series1Numbers = {1, 8, 5, 2, 7, 4};
476224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            Number[] series2Numbers = {4, 6, 3, 8, 2, 10};
486224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
496224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            // Turn the above arrays into XYSeries':
506224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            XYSeries series1 = new SimpleXYSeries(
516224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala                    Arrays.asList(series1Numbers),          // SimpleXYSeries takes a List so turn our array into a List
526224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala                    SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
536224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala                    "Series1");                             // Set the display title of the series
546224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
556224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            // same as above
566224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");
576224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
586224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            // Create a formatter to use for drawing a series using LineAndPointRenderer:
596224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            LineAndPointFormatter series1Format = new LineAndPointFormatter(
606224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala                    Color.rgb(0, 200, 0),                   // line color
616224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala                    Color.rgb(0, 100, 0),                   // point color
626224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala                    null, null);                                  // fill color (none)
636224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
646224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            // add a new series' to the xyplot:
656224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            plot.addSeries(series1, series1Format);
666224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
676224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            // same as above:
686224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            plot.addSeries(series2,
696224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala                    new LineAndPointFormatter(
706224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala                            Color.rgb(0, 0, 200), Color.rgb(0, 0, 100), null, null));
716224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
726224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
736224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            // reduce the number of range labels
746224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            plot.setTicksPerRangeLabel(3);
756224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
766224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            // by default, AndroidPlot displays developer guides to aid in laying out your plot.
776224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            // To get rid of them call disableAllMarkup():
786224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            //plot.disableAllMarkup();
796224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
806224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            Bitmap bmp = plot.getDrawingCache();
816224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala
826224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.demo_app_widget);
836224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            rv.setBitmap(R.id.imgView, "setImageBitmap", bmp);
846224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala            appWidgetManager.updateAppWidget(widgetId, rv);
856224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala        }
866224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala    }
876224eda509d436a575f801942337da92a6c18767Eino-Ville Talvala}
88