3

I'm trying to use AndroidPlot to draw a chart in a homescreen widget. I know that in a normal app, it uses a custom view and from what I've seen (Android: AppWidget with custom view not working), the workaround is to render this as a bitmap in an imageView.

Now I've taken the quickstart code for AndroidPlot and put it into the provider class but it doesn't seem to render anything when I drop it on the homescreen.

The difference between this code and the original quickstart code is that in the quickstart, it leverages the Activity.findViewById but obviously it can't be used here.

Can anyone see something here that I'm doing wrong that may be causing the empty rendering? Appreciate any help you could provide!

private Bitmap getChartImage(Context context)
{

    // initialize our XYPlot reference:
    mySimpleXYPlot = new XYPlot(context, "My Simple XYPlot");

    mySimpleXYPlot.setDrawingCacheEnabled(true);

    // add a new series
    mySimpleXYPlot.addSeries(new SimpleXYSeries(), LineAndPointRenderer.class, new LineAndPointFormatter(Color.rgb(0, 200, 0), Color.rgb(200, 0, 0)));

    // reduce the number of range labels
    mySimpleXYPlot.getGraphWidget().setRangeTicksPerLabel(4);

    // reposition the domain label to look a little cleaner:
    Widget domainLabelWidget = mySimpleXYPlot.getDomainLabelWidget();

    mySimpleXYPlot.position(domainLabelWidget,                     // the widget to position
                            45,                                    // x position value, in this case 45 pixels
                            XLayoutStyle.ABSOLUTE_FROM_LEFT,       // how the x position value is applied, in this case from the left
                            0,                                     // y position value
                            YLayoutStyle.ABSOLUTE_FROM_BOTTOM,     // how the y position is applied, in this case from the bottom
                            AnchorPosition.LEFT_BOTTOM);           // point to use as the origin of the widget being positioned

    // get rid of the visual aids for positioning:
    mySimpleXYPlot.disableAllMarkup();

    //mySimpleXYPlot.measure(150, 150);
    //mySimpleXYPlot.layout(0, 0, 150, 150);


    Bitmap bmp = mySimpleXYPlot.getDrawingCache();

    return bmp;
}
Community
  • 1
  • 1
daitienshi
  • 181
  • 1
  • 3
  • 11
  • I have same problem with another component that wanna make it as Bmp and update remote view. In my case layout will never draw cause it is not attached to any view so size will always 0 . Did you find any solution? – Mahdi Jan 08 '21 at 09:10

1 Answers1

1

Have you stepped through to see if the bitmap is actually empty? My guess is that the Bitmap is fine and the problem exists outside of this chunk of code. Take a look at this example usage of a widget with AndroidPlot - it's super minimal and it definitely works. Hopefully there's a solution in there for you :)

Nick

Nick
  • 8,181
  • 4
  • 38
  • 63