我正在尝试显示从数据库中获取的数据并创建饼图。没有错误,但我只看到一个数据而不是所有存储的数据。 我希望有人告诉我我哪里错了。
private void pie(){
SQLiteDatabase db = new MydbHelper(getActivity()).getReadableDatabase();
String sql = "SELECT field1, SUM(field2), color FROM Table";
Cursor c = db.rawQuery(sql, null);
int count = c.getCount();
double[] values = new double[count];
String[] categoryNames = new String[count];
int[] colori = new int [count];
for(int i=0; i<count; i++) {
c.moveToNext();
categoryNames[i] = c.getString(0);
values[i] = c.getDouble(1);
colori[i] = c.getInt(2);
}
// Instantiating CategorySeries to plot Pie Chart
CategorySeries distributionSeries = new CategorySeries(" Android version distribution as on October 1, 2012");
for(int i=0 ;i < categoryNames.length;i++){
// Adding a slice with its values and name to the Pie Chart
distributionSeries.add(categoryNames[i], values[i]);
}
// Instantiating a renderer for the Pie Chart
DefaultRenderer defaultRenderer = new DefaultRenderer();
for(int i = 0 ;i<categoryNames.length;i++){
SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
defaultRenderer.addSeriesRenderer(seriesRenderer);
}
defaultRenderer.setChartTitle("Android version distribution as on October 1, 2012 ");
defaultRenderer.setChartTitleTextSize(20);
defaultRenderer.setZoomButtonsVisible(true);
Intent intent = ChartFactory.getPieChartIntent(getActivity(), distributionSeries , defaultRenderer, "AChartEnginePieChartDemo");
// Start Activity
startActivity(intent);
c.close();
db.close();
}
答案 0 :(得分:1)
由于您的查询
SELECT field1, SUM(field2), color FROM Table
具有聚合函数SUM(field2)
,它将所有匹配的行压缩为一行。对于非聚合投影field1
,将采用最后一行的值。