I'm trying to display the data taken from the database and create a pie chart. there are no errors but I see only one data and not all of those stored. I hope someone tells me where I'm wrong.
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();
}
Because of your query
SELECT field1, SUM(field2), color FROM Table
which has an aggregate function SUM(field2)
that condenses all matching rows to one row. For the non-aggregate projection field1
the value of the last row is taken.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.