0

我想从表中获取一些值并将它们存储在多折线图中。当我只使用一列(胖)时,它会正确显示图表,但是当我添加另一列或两列时,它会给我一个错误提示:数据集和渲染器不应为空,并且不应具有相同数量的系列。你能帮我解决错误吗?

  public XYMultipleSeriesDataset getFatDataset() {

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    Cursor c = database.rawQuery("select * from dailystats;", null);
    float i = 1.0f;

    c.moveToFirst();
    XYSeries seriesFat = new XYSeries("Fat");
    while(!c.isAfterLast()) {
        seriesFat.add(i++, c.getDouble(2));

        c.moveToNext();
    }
    dataset.addSeries(seriesFat);

   i = 1.0f;
    c.moveToFirst();
    XYSeries seriesProtein = new XYSeries("Protein");
    while(!c.isAfterLast()) {

        seriesProtein.add(i++, c.getDouble(3));

        c.moveToNext();
    }
    dataset.addSeries(seriesProtein);

    c.moveToFirst();
    i = 1.0f;
    XYSeries seriesCarbs = new XYSeries("Carbs");
    while(!c.isAfterLast()) {

        seriesCarbs.add(i++, c.getDouble(4));
        c.moveToNext();
    }
    dataset.addSeries(seriesCarbs);

    return dataset;
}

这是生成图表的代码:

public class ChartActivity {

public Intent getIntent(Context context)

{

XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();


XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
mrenderer.addSeriesRenderer(renderer);

DatabaseAdapter dba = new DatabaseAdapter(context);
dba.open();


Intent intent = ChartFactory.getLineChartIntent(context, dba.getFatDataset(), mrenderer,"line graph");
dba.close();

return intent;

}

}

4

1 回答 1

0

报告的错误非常清楚:Dataset and renderer should not be null and should not have the same number of series意味着您使用了不同数量的系列和系列渲染器。事实上,您正在添加两个数据集:

//...
dataset.addSeries(seriesFat);
//....
dataset.addSeries(seriesCarbs);
//....

但你只有一个 XYSeriesRenderer()

mrenderer.addSeriesRenderer(renderer);

在我看来,您的代码应该开始工作,只需将另一个渲染器添加到XYMultipleSeriesRenderer(),请尝试以下两行:

XYSeriesRenderer renderer2 = new XYSeriesRenderer();
mrenderer.addSeriesRenderer(renderer2);
于 2012-05-04T11:00:02.210 回答