0

我一直在尝试使用 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;
}

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

谢谢!

4

0 回答 0