I want to display a pie chart using the data from the sqlite. I out the data into the list to have the entries for the pie chart. But it seems something is wrong with the code and I don;t know what is wrong with it.Below is the code.
public class chart extends Activity {
SQLiteDatabase SQ;
@Override
protected void onCreate (Bundle savedInstanceState){
…
    //add data
    addData();
    //customize legends
    Legend l=mChart.getLegend();
    l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
    l.setXEntrySpace(7);
    l.setYEntrySpace(5);
}
public ArrayList<String> queryXData(){
    ArrayList<String> xNewData = new ArrayList<String>();
    String query="SELECT "+ TableInfo.APPLIANCE +" FROM "+TableInfo.TABLE_NAME;
    Cursor cursor = SQ.rawQuery(query,null);
    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
        xNewData.add(cursor.getString(0));
    }
    cursor.close();
    return xNewData;
}
public ArrayList<Integer> queryYData(){
    ArrayList<Integer> yNewData= new ArrayList<Integer>();
    String query="SELECT "+ TableInfo.POWER_USED +" FROM "+TableInfo.TABLE_NAME;
    Cursor cursor=SQ.rawQuery(query,null);
    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
        yNewData.add(cursor.getInt(5));
    }
    cursor.close();
    return yNewData;
}
private void addData() {
    ArrayList<Entry> yVals = new ArrayList<Entry>();
    for (int i = 0; i < queryYData().size(); i++)
        yVals.add(new Entry(queryYData().get(i), i));
    ArrayList<String> xVals = new ArrayList<String>();
    for (int i = 0; i < queryXData().size(); i++)
        xVals.add(queryXData().get(i));
    //create pie data set
    PieDataSet dataSet=new PieDataSet(yVals,"Appliances");
    dataSet.setSliceSpace(3); // space between each arc slice
    dataSet.setSelectionShift(5);
    //add many colors
    ArrayList<Integer> colors=new ArrayList<Integer>();
    for(int c: ColorTemplate.VORDIPLOM_COLORS)
        colors.add(c);
    for(int c: ColorTemplate.JOYFUL_COLORS)
        colors.add(c);
    for(int c: ColorTemplate.COLORFUL_COLORS)
        colors.add(c);
    for(int c: ColorTemplate.LIBERTY_COLORS)
        colors.add(c);
    for(int c: ColorTemplate.PASTEL_COLORS)
        colors.add(c);
    colors.add(ColorTemplate.getHoloBlue());
    dataSet.setColors(colors);
    //instantiate pie data object now
    PieData data=new PieData(xVals, dataSet);
    data.setValueFormatter(new PercentFormatter());
    data.setValueTextSize(11f);
    data.setValueTextColor(Color.GRAY);
    mChart.setData(data);
    //undo all highlights
    mChart.highlightValues(null);
    //update pie chart
    mChart.invalidate();
    //demo start
}
}
Aboce is the code I tried , but there are errors, mainly
chart.queryYData(chart.java:100)
Cursor cursor=SQ.rawQuery(query,null);chart.addData(chart.java:111)for (int i = 0; i < queryYData().size(); i++)chart.onCreate(chart.java:76)addData();
These error made the app “Unfortunaely, app has stopped.”. Can someone help me with this? Thank you.
 
Aucun commentaire:
Enregistrer un commentaire