我正在尝试使用BarChart
从Firebase设置ValuEventListener
中的数据。但是,当我尝试在BarEntry
ArrayList中设置数据时,活动崩溃了。我在这里使用MPAndroidChart库。我想在Barchart中将数据设置为ArrayList。
这里是我的Java代码
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.leo.simplearcloader.SimpleArcLoader;
import org.eazegraph.lib.models.BarModel;
import java.util.ArrayList;
public class FindDataByAge extends AppCompatActivity implements ValueEventListener {
BarChart barChart;
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = firebaseDatabase.getReference();
DatabaseReference oneTo10 = databaseReference.child("1-10");
DatabaseReference elevenTo20 = databaseReference.child("11-20");
DatabaseReference twenty1To30 = databaseReference.child("21-30");
DatabaseReference thirty1To40 = databaseReference.child("31-40");
DatabaseReference forty1to50 = databaseReference.child("41-50");
DatabaseReference fiftyTo60 = databaseReference.child("51-60");
DatabaseReference sixty1Plus = databaseReference.child("61+");
String elevenTo20String, stringOneTo10, stringTwenty1To30, stringThirty1To40, string41T050, string51T060, string61;
SimpleArcLoader simpleArcLoader;
TextView tvOneTo10, tvElevenTo20, tvLastUpdate, tvTwenty1To30, tvThirty1To40, tvForty1to50, tvFiftyTo60, tvSixty1Plus;
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_data_by_age);
barChart = findViewById(R.id.barchartM);
barChart.setDrawBarShadow(false);
barChart.setDrawValueAboveBar(true);
barChart.setMaxVisibleValueCount(50);
barChart.setPinchZoom(false);
barChart.setDrawGridBackground(false);
ArrayList<BarEntry> barEntries = new ArrayList<>();
barEntries.add(new BarEntry("1-10",Float.parseFloat(stringOneTo10)));
barEntries.add(new BarEntry("11-20", Float.parseFloat(stringElevenTo20)));
barEntries.add(new BarEntry("21-30", Float.parseFloat(stringTwenty1To30)));
barEntries.add(new BarEntry("31-40", Float.parseFloat(stringThirty1To40)));
barEntries.add(new BarEntry("41-50", Float.parseFloat(string41T050)));
barEntries.add(new BarEntry("51-60", Float.parseFloat(string51T060)));
barEntries.add(new BarEntry("61+", Float.parseFloat(string61)));
BarDataSet barDataSet=new BarDataSet(barEntries,"Data set 1");
BarData barData=new BarData();
barData.addDataSet(barDataSet);
barChart.setData(barData);
barChart.setBackgroundColor(0xFF343456);
barChart.setBorderColor(0xFF873F56);
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
barChart.invalidate();
linearLayout = findViewById(R.id.scrollAge);
simpleArcLoader = findViewById(R.id.loaderAge);
tvOneTo10 = findViewById(R.id.oneTo10);
tvElevenTo20 = findViewById(R.id.elevenTo20);
tvTwenty1To30 = findViewById(R.id.twentyOneTo30);
tvThirty1To40 = findViewById(R.id.thirty1To40);
tvForty1to50 = findViewById(R.id.fortyOneTo50);
tvFiftyTo60 = findViewById(R.id.fiftyOneTo60);
tvSixty1Plus = findViewById(R.id.sixtyOne);
}
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue(String.class) != null) {
simpleArcLoader.setVisibility(View.VISIBLE);
simpleArcLoader.start();
String key = dataSnapshot.getKey();
if (key.equals("1-10")) {
stringOneTo10 = dataSnapshot.getValue(String.class);
tvOneTo10.setText(stringOneTo10);
}
if (key.equals("11-20")) {
stringElevenTo20= dataSnapshot.getValue(String.class);
tvElevenTo20.setText(stringElevenTo20);
}
if (key.equals("21-30")) {
stringTwenty1To30 = dataSnapshot.getValue(String.class);
tvTwenty1To30.setText(stringTwenty1To30);
}
if (key.equals("31-40")) {
stringThirty1To40 = dataSnapshot.getValue(String.class);
tvThirty1To40.setText(stringThirty1To40);
}
if (key.equals("41-50")) {
string41T050 = dataSnapshot.getValue(String.class);
tvForty1to50.setText(string41T050);
}
if (key.equals("51-60")) {
string51T060 = dataSnapshot.getValue(String.class);
tvFiftyTo60.setText(string51T060);
}
if (key.equals("61+")) {
string61 = dataSnapshot.getValue(String.class);
tvSixty1Plus.setText(string61);
}
simpleArcLoader.stop();
simpleArcLoader.setVisibility(View.GONE);
linearLayout.setVisibility(View.VISIBLE);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
@Override
protected void onStart() {
super.onStart();
oneTo10.addValueEventListener(this);
elevenTo20.addValueEventListener(this);
twenty1To30.addValueEventListener(this);
thirty1To40.addValueEventListener(this);
forty1to50.addValueEventListener(this);
fiftyTo60.addValueEventListener(this);
sixty1Plus.addValueEventListener(this);
}
}
您为图表设置了空值。
String elevenTo20String = "0" , stringOneTo10 = "0", stringTwenty1To30 = "0", stringThirty1To40 = "0", string41T050 = "0", string51T060 = "0", string61 = "0";
您可以先替换barEntries开始。
ArrayList<BarEntry> barEntries = new ArrayList<>();
barEntries.add(new BarEntry(0,Float.parseFloat(stringOneTo10)));
barEntries.add(new BarEntry(1, Float.parseFloat(stringElevenTo20)));
barEntries.add(new BarEntry(2, Float.parseFloat(stringTwenty1To30)));
barEntries.add(new BarEntry(3, Float.parseFloat(stringThirty1To40)));
barEntries.add(new BarEntry(4, Float.parseFloat(string41T050)));
barEntries.add(new BarEntry(5, Float.parseFloat(string51T060)));
barEntries.add(new BarEntry(6, Float.parseFloat(string61)));
然后您可以创建自己的ValueFormatter。
public class TitleFormatter extends ValueFormatter {
public TitleFormatter() {
}
@Override
public String getFormattedValue(float value) {
int value = (int) value;
switch (value){
case 0 : return "1-10";
case 1 : return "11-20";
case 2 : return "21-30";
case 3 : return "31-40";
case 4 : return "41-50";
case 5 : return "51-60";
case 6 : return "61+";
default: return "";
}
}
}
为图表设置xAxis格式器。然后您可以在图表上看到诸如“ 1-10,11-20,...”之类的值。
BarDataSet barDataSet=new BarDataSet(barEntries,"Data set 1");
TitleFormatter axisValueFormatter = new TitleFormatter();
XAxis xAxis = barChart.getXAxis();
xAxis.setValueFormatter(axisValueFormatter);
更新图表功能。
private void updateChart(){
if (barChart.getData() != null &&
barChart.getData().getDataSetCount() > 0) {
ArrayList<BarEntry> barEntries = new ArrayList<>();
barEntries.add(new BarEntry("1-10",Float.parseFloat(stringOneTo10)));
barEntries.add(new BarEntry("11-20", Float.parseFloat(stringElevenTo20)));
barEntries.add(new BarEntry("21-30", Float.parseFloat(stringTwenty1To30)));
barEntries.add(new BarEntry("31-40", Float.parseFloat(stringThirty1To40)));
barEntries.add(new BarEntry("41-50", Float.parseFloat(string41T050)));
barEntries.add(new BarEntry("51-60", Float.parseFloat(string51T060)));
barEntries.add(new BarEntry("61+", Float.parseFloat(string61)));
BarDataSet set1 = (BarDataSet) barChart.getData().getDataSetByIndex(0);
set1.setValues(barEntries);
barChart.getData().notifyDataChanged();
barChart.notifyDataSetChanged();
barChart.invalidate();
}
}
在valueListener中调用updateChart:
simpleArcLoader.stop();
simpleArcLoader.setVisibility(View.GONE);
linearLayout.setVisibility(View.VISIBLE);
updateChart(); // like this