gpt4 book ai didi

android - 试图让 BarChart 与 Firebase 一起工作

转载 作者:行者123 更新时间:2023-11-29 02:20:11 25 4
gpt4 key购买 nike

所以我正在尝试让 BarChart 与 Firebase 一起工作。目前,我使用 com.github.PhilJay:MPAndroidChart:v2.2.4 作为图表库。

我想出了加载图表的办法,但是在我更新数据后,我收到了这条错误消息,说明了这一点

java.lang.IllegalArgumentException: One or more of the DataSet Entry arrays are longer than the x-values array of this ChartData object. 

我不知道如何让 Firebase 与 MPAndroidChart 一起工作。

下面是代码。

activity_history.xml

<com.github.mikephil.charting.charts.BarChart 
android:id="@+id/statistik_chart"
android:layout_width="match_parent"
android:layout_height="550px"
android:layout_marginTop="16dp" />

HistoryActivity.java

private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener authStateListener;
private DatabaseReference referenceTicket;

public class HistoryActivity extends AppCompatActivity {

private String uid;
private Long now_ms;
private BarChart chart;

private ArrayList < BarEntry > entries;
private ArrayList < String > labels;
private BarDataSet barDataSet;
private BarData barData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);

firebaseAuth = FirebaseAuth.getInstance();
uid = firebaseAuth.getUid();

entries = new ArrayList < > ();
labels = new ArrayList < > ();

chart = findViewById(R.id.statistik_chart);
String myDate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date()) + " 23:59:59";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
SimpleDateFormat format_tanggal = new SimpleDateFormat("yyyy-MM-dd");

Date date = null;
try {
date = sdf.parse(myDate);
} catch (ParseException e) {
e.printStackTrace();
}

now_ms = date.getTime();
referenceTicket = FirebaseDatabase.getInstance().getReference("log");
Query query_ticket = referenceTicket.orderByChild("created_date").endAt(now_ms);
query_ticket.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int total_pengunjung = (int) dataSnapshot.getChildrenCount();
total_pengunjung_today.setText(String.valueOf(total_pengunjung));
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

labels.add("Jan");
labels.add("Feb");
labels.add("Mar");
labels.add("Apr");
labels.add("May");
labels.add("Jun");

try {
String current_date_time = new SimpleDateFormat("2019-01-01", Locale.getDefault()).format(new Date()) + " 08:00:00";
Date date_current = sdf.parse(current_date_time);
Date convertedDate = dateFormat.parse(current_date_time);
Calendar last_day_current_cal = Calendar.getInstance();
last_day_current_cal.setTime(convertedDate);
last_day_current_cal.set(Calendar.DAY_OF_MONTH, last_day_current_cal.getActualMaximum(Calendar.DAY_OF_MONTH));
String last_day_current_string = format_tanggal.format(last_day_current_cal.getTime()) + " 18:00:00";
Date last_day_current_date = sdf.parse(last_day_current_string);
final long current_start_ms = date_current.getTime();
final long current_last_ms = last_day_current_date.getTime();
Query query_ticket_filter = referenceTicket.orderByChild("created_date").startAt(current_start_ms).endAt(current_last_ms);
query_ticket_filter.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int total_pengunjung = (int) dataSnapshot.getChildrenCount();
int i = 0;
AddValuesToEntry(total_pengunjung, i);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});

} catch (ParseException e) {
e.printStackTrace();
}

try {
String current_date_time = new SimpleDateFormat("2019-02-01", Locale.getDefault()).format(new Date()) + " 08:00:00";
Date date_current = sdf.parse(current_date_time);
Date convertedDate = dateFormat.parse(current_date_time);
Calendar last_day_current_cal = Calendar.getInstance();
last_day_current_cal.setTime(convertedDate);
last_day_current_cal.set(Calendar.DAY_OF_MONTH, last_day_current_cal.getActualMaximum(Calendar.DAY_OF_MONTH));
String last_day_current_string = format_tanggal.format(last_day_current_cal.getTime()) + " 18:00:00";
Date last_day_current_date = sdf.parse(last_day_current_string);
final long current_start_ms = date_current.getTime();
final long current_last_ms = last_day_current_date.getTime();
Query query_ticket_filter = referenceTicket.orderByChild("created_date").startAt(current_start_ms).endAt(current_last_ms);
query_ticket_filter.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int total_pengunjung = (int) dataSnapshot.getChildrenCount();
int i = 1;
AddValuesToEntry(total_pengunjung, i);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});

} catch (ParseException e) {
e.printStackTrace();
}

try {
String current_date_time = new SimpleDateFormat("2019-03-01", Locale.getDefault()).format(new Date()) + " 08:00:00";
Date date_current = sdf.parse(current_date_time);
Date convertedDate = dateFormat.parse(current_date_time);
Calendar last_day_current_cal = Calendar.getInstance();
last_day_current_cal.setTime(convertedDate);
last_day_current_cal.set(Calendar.DAY_OF_MONTH, last_day_current_cal.getActualMaximum(Calendar.DAY_OF_MONTH));
String last_day_current_string = format_tanggal.format(last_day_current_cal.getTime()) + " 18:00:00";
Date last_day_current_date = sdf.parse(last_day_current_string);
final long current_start_ms = date_current.getTime();
final long current_last_ms = last_day_current_date.getTime();
Query query_ticket_filter = referenceTicket.orderByChild("created_date").startAt(current_start_ms).endAt(current_last_ms);
query_ticket_filter.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int total_pengunjung = (int) dataSnapshot.getChildrenCount();
int i = 1;
AddValuesToEntry(total_pengunjung, i);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});

} catch (ParseException e) {
e.printStackTrace();
}

try {
String current_date_time = new SimpleDateFormat("2019-04-01", Locale.getDefault()).format(new Date()) + " 08:00:00";
Date date_current = sdf.parse(current_date_time);
Date convertedDate = dateFormat.parse(current_date_time);
Calendar last_day_current_cal = Calendar.getInstance();
last_day_current_cal.setTime(convertedDate);
last_day_current_cal.set(Calendar.DAY_OF_MONTH, last_day_current_cal.getActualMaximum(Calendar.DAY_OF_MONTH));
String last_day_current_string = format_tanggal.format(last_day_current_cal.getTime()) + " 18:00:00";
Date last_day_current_date = sdf.parse(last_day_current_string);
final long current_start_ms = date_current.getTime();
final long current_last_ms = last_day_current_date.getTime();
Query query_ticket_filter = referenceTicket.orderByChild("created_date").startAt(current_start_ms).endAt(current_last_ms);
query_ticket_filter.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int total_pengunjung = (int) dataSnapshot.getChildrenCount();
int i = 1;
AddValuesToEntry(total_pengunjung, i);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});

} catch (ParseException e) {
e.printStackTrace();
}

try {
String current_date_time = new SimpleDateFormat("2019-05-01", Locale.getDefault()).format(new Date()) + " 08:00:00";
Date date_current = sdf.parse(current_date_time);
Date convertedDate = dateFormat.parse(current_date_time);
Calendar last_day_current_cal = Calendar.getInstance();
last_day_current_cal.setTime(convertedDate);
last_day_current_cal.set(Calendar.DAY_OF_MONTH, last_day_current_cal.getActualMaximum(Calendar.DAY_OF_MONTH));
String last_day_current_string = format_tanggal.format(last_day_current_cal.getTime()) + " 18:00:00";
Date last_day_current_date = sdf.parse(last_day_current_string);
final long current_start_ms = date_current.getTime();
final long current_last_ms = last_day_current_date.getTime();
Query query_ticket_filter = referenceTicket.orderByChild("created_date").startAt(current_start_ms).endAt(current_last_ms);
query_ticket_filter.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int total_pengunjung = (int) dataSnapshot.getChildrenCount();
int i = 1;
AddValuesToEntry(total_pengunjung, i);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});

} catch (ParseException e) {
e.printStackTrace();
}

try {
String current_date_time = new SimpleDateFormat("2019-06-01", Locale.getDefault()).format(new Date()) + " 08:00:00";
Date date_current = sdf.parse(current_date_time);
Date convertedDate = dateFormat.parse(current_date_time);
Calendar last_day_current_cal = Calendar.getInstance();
last_day_current_cal.setTime(convertedDate);
last_day_current_cal.set(Calendar.DAY_OF_MONTH, last_day_current_cal.getActualMaximum(Calendar.DAY_OF_MONTH));
String last_day_current_string = format_tanggal.format(last_day_current_cal.getTime()) + " 18:00:00";
Date last_day_current_date = sdf.parse(last_day_current_string);
final long current_start_ms = date_current.getTime();
final long current_last_ms = last_day_current_date.getTime();
Query query_ticket_filter = referenceTicket.orderByChild("created_date").startAt(current_start_ms).endAt(current_last_ms);
query_ticket_filter.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int total_pengunjung = (int) dataSnapshot.getChildrenCount();
int i = 1;
AddValuesToEntry(total_pengunjung, i);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});

} catch (ParseException e) {
e.printStackTrace();
}

}

public void AddValuesToEntry(int val, int x) {
entries.add(new BarEntry(val, x));
barDataSet = new BarDataSet(entries, "12 Bulan");
barData = new BarData(labels, barDataSet);
chart.notifyDataSetChanged();
chart.setData(barData);
chart.animateY(3000);
}

}

任何帮助将不胜感激。谢谢。

最佳答案

删除以下行:

labels.add("Jan");
labels.add("Feb");
labels.add("Mar");
labels.add("Apr");
labels.add("May");
labels.add("Jun");

更改以下代码:

public void AddValuesToEntry(int val, int x)
{
entries.add(new BarEntry(val, x));
barDataSet = new BarDataSet(entries, "12 Bulan");
barData = new BarData(labels, barDataSet);
chart.notifyDataSetChanged();
chart.setData(barData);
chart.animateY(3000);
}

收件人:

public void AddValuesToEntry(int val, int x)
{
entries.add(new BarEntry(val, x));
labels.add("insert label for each entry here respectively");
barDataSet = new BarDataSet(entries, "12 Bulan");
barData = new BarData(labels, barDataSet);
chart.notifyDataSetChanged();
chart.setData(barData);
chart.animateY(3000);
chart.invalidate();
}

关于android - 试图让 BarChart 与 Firebase 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56645515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com