如何将数据放入MPAndroidChart的Piechart

时间:2018-08-21 07:56:37

标签: android retrofit2 mpandroidchart android-studio-2.3

昨天我尝试将数据(十进制数,字符串)放入MPAndroidchart的Piechart中,但遇到了问题,如果我在“ yEntrys.add(新PieEntry)中添加了字段库存(十进制数)和item_name(字符串) (...)”,然后两者都不显示。如果仅在“ yEntrys.add(new PieEntry(...)”中仅添加一个字段item_name(String),它将再次显示而没有库存,这让我感到困惑。 (为了放置数据,我使用的是Retrofit2的“ GET”方法。)

一些来自json格式的数据来自数据库,并且它是动态的:

{"success":true,
    "result":
        [{"id":"1","item_name":"Salt","stock":"6.5"},
         {"id":"2","item_name":"Sugar","stock":"50.8"},
         {"id":"3","item_name":"Coffe ","stock":"40"},
         {"id":"4","item_name":"Rice","stock":"30.8"
         }]
}

这是我在ListActivity中的代码:

ItemService itemService = APIClient.getClient().create(ItemService .class);
Call<responseItem > call = itemService.getFindAll();
ResponseItem responseItem = response.body();
    if (responseItem.getSuccess().equals("true")) {
        itemList = response.body().getResult();
        List<Item> itemList = new ArrayList<>();
        ArrayList<PieEntry> yEntrys = new ArrayList<PieEntry>();

        for (int i = 0; i < itemList.size(); i++) {
        //this is place i added some field
        yEntrys.add(new PieEntry(i, itemList .get(i).getStock(), itemList.get(i).getItem_name()));

        }

        PieDataSet dataset = new PieDataSet(yEntrys,null);
        dataset.setSliceSpace(3);
        dataset.setValueTextSize(5);

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

        Legend legend = pieChart.getLegend();
        legend.setForm(Legend.LegendForm.DEFAULT);
        legend.setPosition(Legend.LegendPosition.LEFT_OF_CHART);
        legend.setTextSize(8);

        Description description = new Description();
        description.setText("Stocks Description of Items");

        pieChart.setDescription(description);  
        description.setTextColor(Color.BLUE);
        pieChart.setRotationEnabled(true);

        pieChart.setHoleRadius(25f);
        pieChart.setTransparentCircleAlpha(0);
        pieChart.setCenterText("Chart");
        pieChart.setCenterTextSize(10);
        pieChart.setDrawEntryLabels(true);
        pieChart.setEntryLabelTextSize(5);
        pieChart.setTransparentCircleColor(5);

        pieData = new PieData(dataset);
        pieData.setValueTextColor(Color.BLUE);
        pieChart.setData(pieData);
        pieChart.animateY(2000);
        pieChart.highlightValues(null);
        pieChart.invalidate();

        }else{
    Toast.makeText(ListActivity.this, responseItem.getSuccess(), Toast.LENGTH_SHORT).show();
    }

我的答案是如何解决此问题。谢谢大家。

0 个答案:

没有答案