无法解析构造函数&#39; PieData(java.util.ArrayList <java.lang.string>,com.hithub.mikephil.charting.data.PieDataSet)&#39;

时间:2017-11-01 09:02:36

标签: android android-studio mpandroidchart

我正在使用MPAndroidChart库的PieChart。

我试图生成带有图例的饼图。但是一个错误不断弹出无法解析构造函数&#39; PieData(java.util.ArrayList,com.hithub.mikephil.charting.data.PieDataSet)&#39;

public class Graph_all extends Fragment {

    private PieData data;
    private Map<String, Float> dataGraph = null;

    public Graph_all(){

    }

    PieChart mChart;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_piegraph, container, false);

        mChart = (PieChart) view.findViewById(R.id.pieChartGraph);
        mChart.getDescription().setEnabled(false);

        addData(dataGraph);

        Legend l = mChart.getLegend();
        l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
        l.setXEntrySpace(7);
        l.setYEntrySpace(8);
        l.setTextColor(Color.BLACK);
        l.setTextSize(12f);


        return view;
    }

    public ArrayList<String> queryXData(){
        ExpenseDbHelper db = new ExpenseDbHelper(getActivity());
        SQLiteDatabase sqlite = db.getReadableDatabase();
        ArrayList<String> xNewData = new ArrayList<String>();
        String query = "SELECT name, SUM(value) AS total  FROM expenses, categories WHERE expenses.category_id=categories._id GROUP BY category_id";
        Cursor cursor = sqlite.rawQuery(query, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            xNewData.add(cursor.getString(cursor.getColumnIndex("name")));
        }
        cursor.close();
        return xNewData;
    }

    public ArrayList<Float> queryYData(){
        ExpenseDbHelper db = new ExpenseDbHelper(getActivity());
        SQLiteDatabase sqliteY = db.getReadableDatabase();
        ArrayList<Float> yNewData = new ArrayList<Float>();
        String query = "SELECT category_id, SUM(value) AS total FROM expenses GROUP BY category_id";
        Cursor cursor = sqliteY.rawQuery(query, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            yNewData.add(cursor.getFloat(cursor.getColumnIndexOrThrow("total")));
        }
        cursor.close();
        return yNewData;
    }

    private void addData(Map<String, Float> dataGraph) {
        int i = 0;
        ArrayList<PieEntry> yVals = new ArrayList<PieEntry>();
        ArrayList<String> xVals = new ArrayList<String>();

        for (Map.Entry<String, Float> entry : dataGraph.entrySet()) {
            yVals.add(new PieEntry(entry.getValue(), i++));
            xVals.add(entry.getKey());
        }

        PieDataSet dataSet = new PieDataSet(yVals, null);
        dataSet.setSliceSpace(3f); // space between each arc slice
        dataSet.setSelectionShift(5f);

        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);

        PieData data = new PieData(xVals, dataSet);
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE);

        mChart.setData(data);
        mChart.highlightValues(null);
        mChart.invalidate();
        mChart.setDrawHoleEnabled(false);
    }
}

错误在 addData()

PieData data = new PieData(xVals, dataSet);

我真的不知道这个

有什么问题

1 个答案:

答案 0 :(得分:0)

确保public PieData(ArrayList<String> xVals,PieDataSet dataSet)课程中有PieData构造函数。

public class PieData{

ArrayList<String> xVals;
PieDataSet dataSet;

public PieData(ArrayList<String> xVals,PieDataSet dataSet){
    this.xVals = xVals;
    this.dataSet = dataSet;
}
...
}