Changing label and color of pie chart entries - Android

Asked

Viewed 207 times

0

I have the following code:

public class Pontuacaouseractivity extends Appcompatactivity {

PieChart graficoUser;
int pontosCor, pontosNumero, pontosObjeto;

FirebaseUser user;
FirebaseAuth auth;
DatabaseReference databaseRef, userDB;

ArrayList<PieEntry> entries;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
    setContentView(R.layout.activity_pontuacao_user);

    auth = FirebaseAuth.getInstance();
    databaseRef = FirebaseDatabase.getInstance().getReference();
    user = auth.getCurrentUser();
    userDB = databaseRef.child(user.getUid());

    graficoUser = (PieChart) findViewById(R.id.graficoUser);

    userDB.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();

            Log.v("map", map+"");

            String username = map.get("username");
            String password = map.get("password");
            String email = map.get("email");
            String pontuacaoObjeto = map.get("pontuacaoObjeto");
            String pontuacaoCor = map.get("pontuacaoCor");
            String pontuacaoNumero = map.get("pontuacaoNumero");

            pontosObjeto = Integer.valueOf(pontuacaoObjeto);
            pontosNumero = Integer.valueOf(pontuacaoNumero);
            pontosCor = Integer.valueOf(pontuacaoCor);

            Log.v("nome: ",email+"");

            entries = new ArrayList<>();
            entries.add(new PieEntry(pontosCor, "cor"));
            entries.add(new PieEntry(pontosNumero, "numero"));
            entries.add(new PieEntry(pontosObjeto, "objeto"));

            PieDataSet dataset = new PieDataSet(entries, "pontuações");

            ArrayList<String> labels = new ArrayList<String>();
            labels.add("pontuação da cor");
            labels.add("pontuação do numero");
            labels.add("pontuação do objeto");


            dataset.setLabel(labels.toString());

            PieData data = new PieData(dataset);

            PieChart chart = new PieChart(getApplicationContext());
            setContentView(chart);
            chart.setData(data);
            chart.setContentDescription("pontuações");
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I’m not finding good tutorials on the internet that can help me change the color of entries and Abels. Help please?

this is the current effect: Screenshot

1 answer

1


I worked only yesterday with this lib, and really it’s kind of complicated.

But there goes:

Label:

To add the label of each part of the chart, Voce must instantiate its PieEntry with Valor (float) and Label. For example:

entries.add(new PieEntry(num, "Custo"));

Change colors

From what I researched yesterday, there are several ways to change the colors of the chart. You can take a look at their documentation here

or you can follow what I did, which was:

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

You can also take a look at my project, where I added other questions to Piechart. It’s not complete but it will help you a lot.

  • opa! worked :D but as I change the color of Abels?

  • Type here: Entries = new Arraylist<>(); Entries.add(new Pieentry(pointsCor, "color")); Entries.add(new Pieentry(pointsNumero, "number"); Entries.add(new Pieentry(pointsObject, "object")); the displayed text of each label, just below the value, is white. wanted to change it as well as the size. have idea?

  • I found out already, looking at the code of your project! huauha. mt obg !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.