aChart主屏幕小部件

时间:2012-10-22 20:20:34

标签: android bitmap android-appwidget achartengine graphing

我一直在尝试使用aChartEngine作为图形库创建一个简单的主屏幕小部件,但我似乎无法让图表完全渲染。似乎没有任何错误消息,所以我不确定我是否做错了。

我有机会看到这篇文章(https://groups.google.com/forum/?fromgroups=#!topic/achartengine/ovAU5v39ViE),但我似乎无法找到这样的saveToBitmap方法。在过去,我已经看到AndroidPlot中使用的getDrawingCache方法做了同样的事情,但它似乎没有用。

这是我用来获取图表图像的代码:

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds)
{
    for (int appWidgetId : appWidgetIds)
    {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);

        views.setImageViewBitmap(R.id.myChartView, getChartImage(context));

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

private Bitmap getChartImage(Context context)
{
mChartView.setDrawingCacheEnabled(true);

        String[] titles = new String[] { "2007", "2008" };
        List<double[]> values = new ArrayList<double[]>();
        values.add(new double[] { 5230, 7300, 9240, 10540, 7900, 9200, 12030, 11200, 9500, 10500,
            11600, 13500 });
        values.add(new double[] { 14230, 12300, 14240, 15244, 15900, 19200, 22030, 21200, 19500, 15500,
            12600, 14000 });
        int[] colors = new int[] { Color.CYAN, Color.BLUE };
        XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
        renderer.setOrientation(Orientation.VERTICAL);
        setChartSettings(renderer, "Monthly sales in the last 2 years", "Month", "Units sold", 0.5,
            12.5, 0, 24000, Color.GRAY, Color.LTGRAY);
        renderer.setXLabels(1);
        renderer.setYLabels(10);
        renderer.addXTextLabel(1, "Jan");
        renderer.addXTextLabel(3, "Mar");
        renderer.addXTextLabel(5, "May");
        renderer.addXTextLabel(7, "Jul");
        renderer.addXTextLabel(10, "Oct");
        renderer.addXTextLabel(12, "Dec");
        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
          SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
          seriesRenderer.setDisplayChartValues(true);
        }

        mChartView = ChartFactory.getBarChartView(context, buildBarDataset(titles, values), renderer,
                Type.DEFAULT);
        Bitmap bmp = mChartView.getDrawingCache();
        return bmp;
}

有人能告诉我哪里出错了吗?

谢谢!

0 个答案:

没有答案