MPAndroidChart饼图图表自定义图例无法正确显示

时间:2018-11-01 15:21:04

标签: firebase pie-chart mpandroidchart

我已经使用MpAndroidChart创建了Piechart,并且从firebase中检索了数据。我需要显示自定义图例,但仅显示一项,但是pieChart包含5个以上的项目。 {3}}获取饼图的颜色和标签

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

1 个答案:

答案 0 :(得分:0)

最后,我通过以下代码做到了这一点,并且我不知道这是否是最好的方法,但仍然可以很好地工作

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

        }
    });