Reputation: 49
I'm trying to display the result of a query in a bar graph using the library aChartengine. To view all the data taken before I put them in an ArrayList, but then as I enter the double array in the for loop? This is what I have so far.
private void grafico(String years){
SQLiteDatabase db = new ReHelper(getActivity()).getReadableDatabase();
ArrayList <Double> dettagli = new ArrayList<Double>();
String sql ="SELECT number FROM Table GROUP BY motion";
Cursor c = db.rawQuery(sql, null);
while (c.moveToNext()){
double number= c.getDouble(0);
dettagli.add(number);
XYSeries eeSeries = new XYSeries("series");
for(int i=0;i<...;i++){
eeSeries.add(i);
}
}
}
Upvotes: 1
Views: 189
Reputation: 2445
If I understand your question, you should do something like this:
Fetch your data:
XYSeries eeSeries = new XYSeries("series");
int i = 0;
Cursor c = db.rawQuery(sql, null);
while (c.moveToNext()){
double number= c.getDouble(0);
eeSeries.add(i++, number);
}
c.close();
Prepare your Graph/renderer:
// Create a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Add the series to the dataset
dataset.addSeries(eeseries);
// Creating XYSeriesRenderer
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setFillPoints(true);
renderer.setLineWidth(2);
renderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("Title");
multiRenderer.addSeriesRenderer(renderer);
The simplest way to show your graph is to use ChartFactory:
Intent intent = ChartFactory.getBarChartIntent(
getBaseContext(), dataset, multiRenderer, Type.DEFAULT);
startActivity(intent);
Upvotes: 1