【问题标题】:Why several Pie Charts are being merged into one? MPAndroidChart为什么将几个饼图合并为一个? MPAndroidChart
【发布时间】:2022-01-22 04:11:42
【问题描述】:

我想在同一布局上显示 3 个饼图。我将不同的数据集插入到不同的图表中,但最后所有这些数据都显示在 1 个图表上,另外 2 个图表没有数据。 我不能在一个布局上创建多个图表会不会有问题?

private PieChart firstCh, secondCh, thirdCh;
private void openDialog() {

    //code with dialog

        firstCh = dialogStat.findViewById(R.id.firstCharacter);
        secondCh = dialogStat.findViewById(R.id.secondCharacter);
        thirdCh = dialogStat.findViewById(R.id.thirdCharacter);

        ArrayList<PieEntry> statForFirst = new ArrayList<>();
        statForFirst.add(new PieEntry(myNumber, "Kiss"));
        statForFirst.add(new PieEntry(myNumber, "Marry"));
        statForFirst.add(new PieEntry(myNumber, "Kill"));

        ArrayList<PieEntry> statForSecond = new ArrayList<>();
        statForFirst.add(new PieEntry(myNumber, "Kiss"));
        statForFirst.add(new PieEntry(myNumber, "Marry"));
        statForFirst.add(new PieEntry(myNumber, "Kill"));

        ArrayList<PieEntry> statForThird = new ArrayList<>();
        statForFirst.add(new PieEntry(myNumber, "Kiss"));
        statForFirst.add(new PieEntry(myNumber, "Marry"));
        statForFirst.add(new PieEntry(myNumber, "Kill"));

        setNewChart(firstCh, statForFirst);
        setNewChart(secondCh, statForSecond);
        setNewChart(thirdCh, statForThird);

        //code with dialog
    }

private void setNewChart(PieChart chart, ArrayList<PieEntry> entries) {
        chart.getDescription().setEnabled(false);
        chart.getLegend().setEnabled(false);

        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setColors(getResources().getColor(R.color.pinkySarah), getResources().getColor(R.color.lightViola), getResources().getColor(R.color.eyeKiller));

        PieData data = new PieData(dataSet);
        data.setDrawValues(true);
        data.setValueFormatter(new PercentFormatter(chart));
        data.setValueTextSize(10f);
        data.setValueTextColor(R.color.darkViola);

        chart.setData(data);
    chart.invalidate();
    }

This is how it looks in my app 作为参考,我正在使用 LinearLayout

【问题讨论】:

    标签: java android android-studio pie-chart mpandroidchart


    【解决方案1】:

    只是一个拼写错误。

    您已将所有数据添加到 statForFirst arraylist 而不是其他两个列表。解决这个问题,你就可以开始了

    【讨论】:

      猜你喜欢
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 2019-01-18
      • 2017-10-03
      • 2019-11-24
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多