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