Search code examples
firebasepie-chartmpandroidchart

MPAndroidChart Pie Chart Custom Legends are not displaying correctly


I have create piechart using MpAndroidChart and data is retrieved from firebase.I need to display custom legends but only one item is displaying but pieChart has more than 5items.Since getColor() and getLabels() are deprecated and i have used this Code to get color and label of piechart

listdataref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()) {
                Log.d("TAG", "IF else");
                /* new homePieTask().execute(dataSnapshot);*/

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

                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    int total = ds.child("total").getValue(Integer.class);
                    String name = ds.child("expensesName").getValue(String.class);
                    entries.add(new PieEntry(total, name));


                }

                pieDataSet = new PieDataSet(entries,"Expense By Dates");
                pieData= new PieData(pieDataSet);
                homepiechart.setData(pieData);
                pieDataSet.setColors(color);
                Legend legend = homepiechart.getLegend();
                legend.setEnabled(false);
                int colorCodes[] = getColors(legend);
                String labels[] = getLabels(legend);
                for (int i = 0; i < getColors(legend).length -1; i++) {
                    LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams(
                            20, 20);
                    parms_legen_layout.setMargins(0, 0, 20, 0);
                    LinearLayout legend_layout = new LinearLayout(context);
                    legend_layout.setLayoutParams(parms_legen_layout);
                    legend_layout.setOrientation(LinearLayout.HORIZONTAL);
                    legend_layout.setBackgroundColor(colorCodes[i]);
                    relativeLayout.addView(legend_layout);

                    TextView txt_unit = new TextView(context);
                    txt_unit.setText(labels[i]);
                    relativeLayout.addView(txt_unit);
                }

Solution

  • Finally i done this by Following Code and i dnt know this is best method or not but still it works good

     listdataref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    Log.d("Grid","On");
                    List<String> labelsList=new ArrayList<>();
                    if (getActivity() != null) {
    
                            Log.d("Grid", "OnOn");
                            for(int i=0;i< dataSnapshot.getChildrenCount();i++) {
                                for(DataSnapshot ds:dataSnapshot.getChildren()) {
    
                                    String label = ds.child("expensesName").getValue(String.class);
    
                                labelsList.add(label);
                                    List<Integer> intList = new ArrayList<Integer>();
                                    for (int j : color)
                                    {
                                        intList.add(j);
                                    }
    
    
                                    aClass = new LegendsClass(intList.get(i), labelsList.get(i));
    
                            }
    
                                legendsClasses.add(aClass);
                                gridViewAdapter = new GridViewAdapter(getActivity(), legendsClasses);
                                gridView.setAdapter(gridViewAdapter);
                                gridViewAdapter.notifyDataSetChanged();
    
    
    
                            Log.d("Grid", "After");
                        }
    
    
                    }
                    }else
                {
                    Log.d("Grid","else");
                    gridView.setVisibility(View.INVISIBLE);
                }
    
            }
    
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
    
            }
        });