我正在处理Android MP组合图。我正在使用Firebase将实时数据传递到图表。而且我成功完成了获取数据并将其传递到组合图的操作。
但是问题是,当Firebase从其他数据源进行自身更新时,图表未根据值(在数据快照中获取)进行自身更新。
正如您看到的here一样,聊天并没有向前发展,而是数据没有更新。
我看到了许多解决方案,但是根据需求和图表类型的不同,它们也有所不同。如何解决我的图表问题?会很有帮助的。
这是我的 onDataChange()代码。呼入 onCreate
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
//get FLOW RATE
String rate = ds.child("flowRate").getValue(String.class);
//get TIMESTAMP
long timestamp = ds.child("timestamp").getValue(Long.class);
//add, got TIMESTAMP to xAxisData (list)
xAxisData.add(getDatafTimeStamp(timestamp));
//add, got FLOW RATE to flow_rate (list)
flow_rate.add(rate);
combinedChart.notifyDataSetChanged();
combinedChart.invalidate();
}
drawCombinedChart(xAxisData, flow_rate, quantity, followed);
@Override
public void onCancelled(DatabaseError databaseError) {
datafetched = false;
}
});
这是我的 drawCombinechart()函数,每次在Firebase中更新数据时都会调用
public void drawCombinedChart(List<String> axis, final List<String> rate) {
//set chart's description, grid, shadow when chart is loaded
combinedChart.setDescription("");
combinedChart.setDrawGridBackground(false);
combinedChart.setDrawBarShadow(false);
combinedChart.setNoDataText("Loading.....");
// draw bars behind lines
combinedChart.setDrawOrder(new CombinedChart.DrawOrder[]{
CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.LINE, CombinedChart.DrawOrder.LINE
});
Legend l = combinedChart.getLegend();
l.setWordWrapEnabled(true);
// Setting X_Axis of combine chart
XAxis xAxis = combinedChart.getXAxis();
xAxis.setLabelsToSkip(0);
// Setting X_Axis position in bottom of combine chart
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // to set xAxis in Bottom
//setting X_Axis only on right side
combinedChart.getAxisRight().setDrawLabels(true);
combinedChart.getAxisLeft().setDrawZeroLine(false);
combinedChart.getAxisRight().setAxisMinValue(0);
// Setting Y_Axis to left side of combine chart
YAxis y = combinedChart.getAxisLeft();
y.setAxisMinValue(0);
y.setGranularity(1f);
y.setGranularityEnabled(true);
y.setAxisLineColor(Color.BLACK);
y.setTextColor(Color.BLACK);
y.setEnabled(true);
y.setAxisLineColor(Color.BLACK);
y.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
y.setDrawGridLines(false);
y.setLabelCount(5, true);
y.setDrawZeroLine(false);
y.isForceLabelsEnabled();
y.setDrawLabels(true);
y.setDrawLimitLinesBehindData(true);
y.setDrawGridLines(true);
y.setDrawLimitLinesBehindData(true);
y.setTextColor(Color.RED);
y.setDrawZeroLine(true);
//animate Y_axis to 5 seconds
combinedChart.animateY(0);
// Setting Y_Axis to right side of combine chart
YAxis rightAxis = combinedChart.getAxisRight();
rightAxis.setDrawGridLines(true);
rightAxis.setLabelCount(5, true);
rightAxis.setDrawZeroLine(false);
rightAxis.isForceLabelsEnabled();
rightAxis.setDrawLabels(true);
rightAxis.setDrawLimitLinesBehindData(true);
rightAxis.setDrawGridLines(true);
rightAxis.setDrawLimitLinesBehindData(true);
rightAxis.setTextColor(Color.RED);
rightAxis.setDrawZeroLine(true);
//hide background grid lines of chart
combinedChart.getXAxis().setDrawGridLines(false);
combinedChart.getAxisLeft().setDrawGridLines(false);
combinedChart.getAxisRight().setDrawGridLines(false);
data = new CombinedData(axis);
//when data loaded show msg onto chart
combinedChart.setNoDataText("Data loaded");
//switch pressed
flw_Rate_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//if FlOW RATE button is on
if (isChecked) {
//change state of other switches
lqd_followed_switch.setChecked(false);
//change color of current switch when ON
flow_rate_switch_txt.setTextColor(Color.parseColor("#536BB5"));
//call function to generate area chart
mlineData = generateLineData(rate);
data.setData(mlineData);
combinedChart.setData(data);
// allow 4 values to be displayed at once on the x-axis, not more
combinedChart.setVisibleXRangeMaximum(4);
combinedChart.moveViewToX(4);
//animate y axis to 2 sec
combinedChart.animateY(0);
combinedChart.invalidate();
}
//if FlOW RATE button is off
else {
//change text color of flow rate switch
flow_rate_switch_txt.setTextColor(Color.BLACK);
//remove all data sets of area chart when switch turned OFF
boolean indicator = data.removeDataSet(lineData1.getDataSetByLabel("Flow Rate", true));
//notify to combine chart that data set has removed
combinedChart.notifyDataSetChanged();
//clear relative chart
combinedChart.clear();
combinedChart.setNoDataText("No data");
combinedChart.invalidate();
combinedChart.animateX(500);
}
}
});
还有我的 generateLineData()函数,该函数在打开开关以生成图表数据时被调用
private LineData generateLineData(List<String> arr) {
LineData d = new LineData();
ArrayList<Entry> entries = new ArrayList<Entry>();
for (int index = 0; index < arr.size(); index++)
entries.add(new Entry(Float.parseFloat(arr.get(index)), index));
LineDataSet set = new LineDataSet(entries, "Flow Rate");
//set highlight line color
set.setColor(Color.parseColor("#536BB5"));
set.setLineWidth(2.5f);
//set data points color
set.setCircleColor(Color.RED);
//set color to area occupied by chart
set.setFillColor(Color.parseColor("#536BB5"));
set.setCircleRadius(4f);
//draw chart lines in cubic shape
set.setDrawCubic(true);
//set area occupied by chart must be filled
set.setDrawFilled(true);
set.setDrawValues(false);
set.setHighLightColor(Color.MAGENTA);
set.setLabel("Flow rate");
set.setAxisDependency(YAxis.AxisDependency.LEFT);
d.addDataSet(set);
lineData1 = d;
return d;
}
答案 0 :(得分:0)
更新值后,您必须将值设置为mpChart。 而要更新mpchat,只需隐藏并显示mpChart。
答案 1 :(得分:0)
保持开关状态为ON,我可以实时获取图表。
答案 2 :(得分:0)
您需要告诉图表数据集已更新,并且需要刷新图表以添加以下行:
combinedChart.notifyDataSetChanged();
combinedChart.setData(data);
combinedChart.invalidate();
答案 3 :(得分:0)
在显示图表方法的开头添加此代码
raw