0

我创建了一个图表,它将从我的数据库中获取多个数据集,但它没有显示在图表中,并且只显示没有可用的数据。谁能指出问题?我尝试阅读 mpandroidchart 教程,但我仍然无法指出问题所在。提前致谢

public ArrayList<String> queryXData(){
    ArrayList<String> xNewData = new ArrayList<String>();
    String query = "SELECT Subject FROM gradesinfo ORDER BY Subject";
    Cursor cursor = cn.rawQuery(query, null);
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        xNewData.add(cursor.getString(cursor.getColumnIndex("Subject")));
    }
    cursor.close();
    return xNewData;
}

public ArrayList<Float> queryYData(){
    ArrayList<Float> yNewData = new ArrayList<Float>();
    String query = "SELECT PG FROM gradesinfo ORDER BY Subject";
    Cursor cursor = cn.rawQuery(query, null);
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        yNewData.add(cursor.getFloat(cursor.getColumnIndex("PG")));
    }
    cursor.close();
    return yNewData;
}


public ArrayList<Float> queryYData2(){
    ArrayList<Float> yNewData2 = new ArrayList<Float>();
    String query = "SELECT MG FROM gradesinfo ORDER BY Subject";
    Cursor cursor = cn.rawQuery(query, null);
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        yNewData2.add(cursor.getFloat(cursor.getColumnIndex("MG")));
    }
    cursor.close();
    return yNewData2;
}


public ArrayList<Float> queryYData3(){
    ArrayList<Float> yNewData3 = new ArrayList<Float>();
    String query = "SELECT FG FROM gradesinfo ORDER BY Subject";
    Cursor cursor = cn.rawQuery(query, null);
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        yNewData3.add(cursor.getFloat(cursor.getColumnIndex("FG")));
    }
    cursor.close();
    return yNewData3;
}


private void addData(){
    Cursor cursor = cn.rawQuery("SELECT PG, MG, FG FROM gradesinfo ORDER BY Subject", null);

    if(cursor.getCount()>=2){

        ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
        for (int i = 0; i < queryYData().size(); i++)
            yVals.add(new BarEntry(queryYData().get(i), i));

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        for (int i = 0; i < queryYData2().size(); i++)
            yVals1.add(new BarEntry(queryYData2().get(i), i));

        ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
        for (int i = 0; i < queryYData3().size(); i++)
            yVals2.add(new BarEntry(queryYData3().get(i), i));

        ArrayList<String> xVals = new ArrayList<String>();
        for(int i = 0; i < queryXData().size(); i++)
            xVals.add(queryXData().get(i));

        BarDataSet dataSet = new BarDataSet(yVals, "Prelim");
        dataSet.setColors(ColorTemplate.COLORFUL_COLORS);

        BarDataSet dataSet2 = new BarDataSet(yVals1, "Midterm");
        dataSet2.setColors(ColorTemplate.COLORFUL_COLORS);

        BarDataSet dataSet3 = new BarDataSet(yVals2, "Finals");
        dataSet3.setColors(ColorTemplate.COLORFUL_COLORS);


        ArrayList<BarDataSet> dataset = new ArrayList<BarDataSet>();
        dataset.add(dataSet);
        dataset.add(dataSet2);
        dataset.add(dataSet3);

        BarData data = new BarData(xVals, dataset);


        LimitLine line = new LimitLine(75f, "Passing Grade");
        line.setTextSize(8f);
        line.setLineWidth(4f);
        YAxis leftAxis = barChart.getAxisLeft();
        leftAxis.addLimitLine(line);

        barChart.setData(data);
        barChart.setDescription("Grades Chart");
        barChart.animateY(2000);
    }
4

1 回答 1

1

我怀疑您的图表不知道收到的数据。告诉图表做:

barChart.notifyDataSetChanged();
barChart.invalidate();
于 2016-08-11T17:57:02.097 回答