Changing label and color of the entries of a pie Chart - Android

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 labels. Help please?

this is the current effect:

    
asked by anonymous 31.05.2017 / 18:13

1 answer

1

I still worked with this lib yesterday, and it really is kind of complicated.

But there it goes:

Add label:

To add the label of each part of the graph, you must instantiate its PieEntry with Value (float) and Label . For example:

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

Change colors

As 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 will help you a lot.

    
31.05.2017 / 18:33