0
votes

I am using MP android Pie Chart API to generate a pie chart for my android app. I followed one youtube tutorial to add details statically to the Pie chart and It got successfully added The code:

public class EnergyBreakdown extends AppCompatActivity {
    PieChart pieChart;
    ArrayList<PieEntry> entries;
    ArrayList<String> PieEntryLabels;
    PieDataSet pieDataSet;
    PieData pieData;
    int LightDimmerLevel =2;
    int NumberofLights=5;
    int result;
    ArrayList<Integer> lightResult;

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

        pieChart=findViewById(R.id.piechart);
        entries=new ArrayList<>();
        PieEntryLabels = new ArrayList<String>();

        AddValuesToPIEENTRY();

        AddValuesToPieEntryLabels();

        pieDataSet = new PieDataSet(entries, "");

        pieData = new PieData(pieDataSet);

        pieDataSet.setColors(ColorTemplate.COLORFUL_COLORS);

        pieChart.setData(pieData);
        pieChart.setCenterText("Energy Breakdown");

        pieChart.animateY(3000);

    }
    public void AddValuesToPIEENTRY(){

        entries.add(new PieEntry(2f, 0));
        entries.add(new PieEntry(4f, 1));
        entries.add(new PieEntry(6f, 2));
        entries.add(new PieEntry(8f, 3));
        entries.add(new PieEntry(7f, 4));
        entries.add(new PieEntry(3f, 5));

    }

    public void AddValuesToPieEntryLabels(){

        PieEntryLabels.add("January");
        PieEntryLabels.add("February");
        PieEntryLabels.add("March");
        PieEntryLabels.add("April");
        PieEntryLabels.add("May");
        PieEntryLabels.add("June");

    }


}

I got result like this this result

But now I want to add it dynamically as in make some calculation and add data to pie chart. I made some changes in the code but it is not showing any data

public class EnergyBreakdown extends AppCompatActivity {
    PieChart pieChart;
    ArrayList<PieEntry> entries;
    ArrayList<String> PieEntryLabels;
    PieDataSet pieDataSet;
    PieData pieData;
    int LightDimmerLevel =2;
    int NumberofLights=2;
    int result;
    ArrayList<Integer> lightResult;

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

        pieChart=findViewById(R.id.piechart);
        entries=new ArrayList<>();
        PieEntryLabels = new ArrayList<String>();

        AddValuesToPIEENTRY();

        AddValuesToPieEntryLabels();

        pieDataSet = new PieDataSet(entries, "");

        pieData = new PieData(pieDataSet);

        pieDataSet.setColors(ColorTemplate.COLORFUL_COLORS);

        pieChart.setData(pieData);
        pieChart.setCenterText("Energy Breakdown");

        pieChart.animateY(3000);

    }
    public void AddValuesToPIEENTRY(){
        for (int i=0;i<NumberofLights+1;i++){
            result=((LightDimmerLevel)/(NumberofLights*5))*100;

            entries.add(new PieEntry(result));
        }
//       
    }

    public void AddValuesToPieEntryLabels(){

        PieEntryLabels.add("January");
        PieEntryLabels.add("February");
        PieEntryLabels.add("March");
        PieEntryLabels.add("April");
        PieEntryLabels.add("May");
        PieEntryLabels.add("June");

    }


}

Can anyone help where I am doing wrong and where should i make changes to add data dynamically to pie chart dynamically

1

1 Answers

0
votes

By looking your static data, it needs to pass float type data, change your int variable to float and the second param is index of loop

float LightDimmerLevel = 2;
float NumberofLights = 2;
float result;

...

public void AddValuesToPIEENTRY(){
     for (int i=0;i<NumberofLights+1;i++){
        result=((LightDimmerLevel)/(NumberofLights*5))*100;
        entries.add(new PieEntry(result, i));
     }     
}