我想从表中获取一些值并将它们存储在多线图中。 当我只使用一列(胖)时,它会正确显示图形,但是当我添加另一列或两列时,它会给我一个错误说: 数据集和渲染器不应为null,并且不应具有相同的系列数。 你能帮我修一下这个错误吗?
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;
}
}
答案 0 :(得分: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);