PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] Why several Pie Charts are being merged into one? MPAndroidChart

 April 22, 2022     android, android-studio, java, mpandroidchart, pie-chart     No comments   

Issue

I want to show 3 several pie charts on the same layout. I insert different data sets to different charts, but by the end all of these data shows on 1 chart and another 2 charts have no data. Could there be a problem that I cannot create several charts on one layout?

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 For reference, i am using LinearLayout


Solution

Just a typo mistake.

you have added all the data to statForFirst arraylist instead of other two lists. Fix this and you are good to go



Answered By - DrHowdyDoo
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing