在PieData数据处获取错误=新的PieData(标签,数据集);添加数据

时间:2016-07-13 06:14:52

标签: android graph charts mpandroidchart

在我的应用程序中需要使用饼图,我正在使用MPAndroidChart但是,在Piedata构造函数中添加数据时出错,请帮忙。 使用dependancy compile'c​​om.github.PhilJay:MPAndroidChart:v3.0.0 beta'

 imageView=(PieChart) v.findViewById(R.id.result_text);
    ArrayList<Entry> entries = new ArrayList<>();
    entries.add(new Entry(4f, 0));
    entries.add(new Entry(8f, 1));
    entries.add(new Entry(6f, 2));
    entries.add(new Entry(12f, 3));
    entries.add(new Entry(18f, 4));
    entries.add(new Entry(9f, 5));
    PieDataSet dataset = new PieDataSet(entries,"uyggh");
    ArrayList<Object> labels = new ArrayList<Object>();
    labels.add("January");
    labels.add("February");
    labels.add("March");
    labels.add("April");
    labels.add("May");
    labels.add("June");
    PieData data = new PieData(labels, dataset);//getting error here
    imageView.setData(data);
    imageView.setDescription("Description");

5 个答案:

答案 0 :(得分:4)

根据与com.github.PhilJay:MPAndroidChart:v3.0.3对应的最新文档,条目(饼图条目)现在将x和y值一起使用。

List<PieEntry> entries = new ArrayList<>();

entries.add(new PieEntry(18.5f, "Green"));
entries.add(new PieEntry(26.7f, "Yellow"));
entries.add(new PieEntry(24.0f, "Red"));
entries.add(new PieEntry(30.8f, "Blue")); 

PieDataSet set = new PieDataSet(entries, "Election Results");
PieData data = new PieData(set);
pieChart.setData(data);
pieChart.invalidate(); // refresh

因此,不要降低脱离程度,而是使用更新后的代码。

答案 1 :(得分:2)

PieData只接受一个参数,即PieDataSet。所以替换

PieData data = new PieData(labels, dataset);

PieData data = new PieData(dataset);

有关详情,请查看this官方示例。

希望,这个答案对您有所帮助。

答案 2 :(得分:1)

当我使用dependancy com.github.PhilJay:MPAndroidChart:v2.1.6&#39;然后 PieData接受两个参数,即String和PieDataSet。所以,我认为它的依赖更新问题。 使用较低的依赖性可以解决。

答案 3 :(得分:0)

您应该像这样在PieEntry方法中传递标签。 它对我有用

private  float[] yData = {25.3f,10.3f,66.3f,44.3f,16.3f,46.3f,23.3f};
private  String[] xData = {"med","hamza","mohammed","","","",""};

    for(int i = 0 ; i <yData.length;i++){
        yEntrys.add(new PieEntry(yData[i],xData[i % xData.length]));

}

这是我的代码,它可以100%使用 compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'但更高版本的gradle使用implementation而不是compile

public class Main2Activity extends AppCompatActivity {
   private  static  String TAG = "MainActivit";
private  float[] yData = {25.3f,10.3f,66.3f,44.3f,16.3f,46.3f,23.3f};
    private  String[] xData = {"med","hamza","mohammed","","","",""};

PieChart pieChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Log.d(TAG, "Beach chart");


        pieChart = (PieChart) findViewById(R.id.idPieChart);
        //pieChart.setDescription(" Sales by employee In thousands $");
        pieChart.setRotationEnabled(true);
        pieChart.setHoleRadius(25f);
        pieChart.setTransparentCircleAlpha(0);
        pieChart.setCenterText("super cool chart");
        pieChart.setCenterTextSize(10);
        pieChart.setDrawEntryLabels(true);

        addDataSet();


        pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                Log.d(TAG, "onValueSelected: value selected from chart");
                Log.d(TAG, "onValueSelected: "+ e.toString());
                Log.d(TAG, "onValueSelected: "+ h.toString());
            }

            @Override
            public void onNothingSelected() {

            }
        });
    }

    private void addDataSet() {
        Log.d(TAG, "addDataSet: started");

        ArrayList<PieEntry> yEntrys = new ArrayList<>();
        ArrayList<String> xEntrys = new ArrayList<>();

        for(int i = 0 ; i <yData.length;i++){
            yEntrys.add(new PieEntry(yData[i],xData[i % xData.length]));

 }

        for(int i = 0 ; i <xData.length;i++){
            xEntrys.add(xData[i]);

        }


        //create the DATA

        PieDataSet pieDataSet = new PieDataSet(yEntrys,"Beachs");
        pieDataSet.setSliceSpace(2);
        pieDataSet.setValueTextSize(12);



        //add colors to dataSet

        ArrayList<Integer> colors = new ArrayList<>();
        colors.add(Color.GRAY);
        colors.add(Color.BLUE);
        colors.add(Color.RED);
        colors.add(Color.GREEN);
        colors.add(Color.CYAN);
        colors.add(Color.YELLOW);
        colors.add(Color.MAGENTA);

        pieDataSet.setColors(colors);

pieDataSet.setSliceSpace(25);



        //add legend to chart

        Legend legend = pieChart.getLegend();
        legend.setForm(Legend.LegendForm.CIRCLE);
        legend.setPosition(Legend.LegendPosition.LEFT_OF_CHART);


        //create a pieData object

        PieData pieData = new PieData(pieDataSet);



        pieChart.setData(pieData);

pieChart.invalidate();

    }
}

答案 4 :(得分:0)

您的ArryayList&lt;&gt;应该使用String,而不是使用Object:

ArrayList<String> labels = new ArrayList<String>();
....
PieData data = new PieData(labels, dataset);//getting error here

因为新的PieData需要ArrayList&lt;&lt;字符串&gt;&gt;