title: How to create a pie chart using data from Firestore in Java
body: <p>Right now my code is looking like this</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  Spinner spinner;
  TextView textView;
  String item;
  private EditText amount;
  private Button save;
  private Button report;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      amount = findViewById(R.id.Amount);
      save = findViewById(R.id.Save);
      String [] category = {&quot;Food&quot;, &quot;Gift&quot;, &quot;Education&quot;, &quot;Health&quot;, &quot;Car&quot;};
      save.setOnClickListener(new View.OnClickListener(){
          @Override
          public void onClick(View v){
              int txt_amount = Integer.parseInt(amount.getText().toString());
              item = spinner.getSelectedItem().toString();
  
  
              if (txt_amount == 0){
                  Toast.makeText(MainActivity.this, &quot;Empty credentials!&quot;, Toast.LENGTH_SHORT).show();
              }
              else{
                  FirebaseFirestore db = FirebaseFirestore.getInstance();
                  Map&lt;String, Object&gt; users = new HashMap&lt;&gt;();
                  users.put(&quot;Category&quot;, item);
                  users.put(&quot;Amount&quot;, txt_amount);
  
                  db.collection(&quot;Expenses&quot;).add(users).addOnCompleteListener(new OnCompleteListener&lt;DocumentReference&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;DocumentReference&gt; task) {
                          if(task.isSuccessful()){
                              Toast.makeText(MainActivity.this, &quot;Values added&quot;, Toast.LENGTH_SHORT).show();
                          }
                      }
                  });
  
  
              }
          }
      });
  
      //SPINNER CODE
      spinner = findViewById(R.id.spinnerCategory);
      ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, category);
      arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(arrayAdapter);
  
  
      report = (Button) findViewById(R.id.Report);
      report.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              openActivityReport();
          }
      });
      FirebaseFirestore.getInstance().collection(&quot;Expenses&quot;).whereEqualTo(&quot;Category&quot;, &quot;Expense&quot;);
  }
  
  public void openActivityReport(){
      Intent intent = new Intent(this, ReportActivity.class);
      startActivity(intent);
  }
  </code></pre>
  <p>public class ReportActivity extends AppCompatActivity {</p>
  <pre><code>private PieChart pieChart;
  FirebaseFirestore db = FirebaseFirestore.getInstance();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_report);
  
  
      pieChart = findViewById(R.id.activity_report_piechart);
  
      loadPieChartData();
      setupPieChart();
  }
  
  public void setupPieChart(){
      pieChart.setDrawHoleEnabled(true);
      pieChart.setUsePercentValues(true); // to use the percentage values on the chart
      pieChart.setEntryLabelColor(Color.BLACK);
      pieChart.setEntryLabelTextSize(12);
      pieChart.setCenterText(&quot;Spending by Category&quot;);
      pieChart.setCenterTextSize(24);
      pieChart.getDescription().setEnabled(false);
  }
  
  private void loadPieChartData(){
  
      db.collection(&quot;Expenses&quot;).get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
          @Override
          public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
              if(task.isSuccessful()){
                  for(QueryDocumentSnapshot document : task.getResult()){
                      Log.d(&quot;&quot;, document.getLong(&quot;Amount&quot;) + document.getString(&quot;Category&quot;));
                      ArrayList&lt;PieEntry&gt; entries = new ArrayList&lt;PieEntry&gt;();
                      entries.add(new PieEntry(Float.valueOf(document.getLong(&quot;Amount&quot;)),document.getString(&quot;Category&quot;)));
                      entries.add(new PieEntry(Float.valueOf(document.getLong(&quot;Amount&quot;)),document.getString(&quot;Category&quot;)));
                      entries.add(new PieEntry(Float.valueOf(document.getLong(&quot;Amount&quot;)),document.getString(&quot;Category&quot;)));
                      entries.add(new PieEntry(Float.valueOf(document.getLong(&quot;Amount&quot;)),document.getString(&quot;Category&quot;)));
  
                      ArrayList&lt;Integer&gt; colors = new ArrayList&lt;&gt;();
                      for(int color: ColorTemplate.MATERIAL_COLORS){
                          colors.add(color);
                      }
                      for(int color: ColorTemplate.VORDIPLOM_COLORS){
                          colors.add(color);
                      }
  
                      PieDataSet dataSet = new PieDataSet(entries, &quot;Expense Category&quot;);
                      dataSet.setColors(colors);
                      PieData data = new PieData(dataSet);
                      data.setDrawValues(true); //draws the values of the different percentages
                      data.setValueFormatter(new PercentFormatter(pieChart)); //Presents the percentage
                      data.setValueTextColor(Color.BLACK);
                      data.setValueTextSize(16f);
  
                      pieChart.setData(data);
                      pieChart.invalidate();
                  }
              }
              else{
                  Log.d(&quot;&quot;, &quot;Error getting documents &quot;, task.getException());
              }
          }
      });
  </code></pre>
  <p>And my firestore database is looking like this</p>
  <p><a href="https://i.stack.imgur.com/vijfb.png" rel="nofollow noreferrer">https://i.stack.imgur.com/vijfb.png</a></p>
  <p>Is there anyway to arrange my code and to be able to create a pie chart from the data that is being stored in the firestore database. Hopefully this is enough code and the image is enough to be able to get some help</p>