MP Chart Android在获取数据快照的实时值后未更新图表

MP Chart Android在获取数据快照的实时值后未更新图表,android,firebase,mpandroidchart,Android,Firebase,Mpandroidchart,我正在研究android MP组合图表。我正在使用firebase将实时数据传递到图表。我成功地获取了数据并将其传递到联合收割机图表中 但问题是,当firebase从其他数据源自我更新时,图表并没有根据数据快照中的值自我更新 而不是聊天正在向前发展,但数据没有更新,正如你所看到的 我看到了许多解决方案,但它们根据需求和图表类型而有所不同。如何解决我的图表问题?这将非常有帮助 这是我的onDataChange()代码。召唤 onCreate 这是我的drawCombinechart()函数,每当f

我正在研究android MP组合图表。我正在使用firebase将实时数据传递到图表。我成功地获取了数据并将其传递到联合收割机图表中

但问题是,当firebase从其他数据源自我更新时,图表并没有根据数据快照中的值自我更新

而不是聊天正在向前发展,但数据没有更新,正如你所看到的

我看到了许多解决方案,但它们根据需求和图表类型而有所不同。如何解决我的图表问题?这将非常有帮助

这是我的onDataChange()代码。召唤 onCreate

这是我的drawCombinechart()函数,每当firebase中的数据更新时都会调用它

public void drawCombinedChart(列表轴,最终列表速率){
//加载图表时设置图表的说明、网格和阴影
combinedChart.setDescription(“”);
combinedChart.setDrawGridBackground(false);
组合图表。设置牵引杆阴影(错误);
combinedChart.setNodeDataText(“加载…”);
//在线后画线
combinedChart.setDrawOrder(新combinedChart.DrawOrder[]{
CombinedChart.DrawOrder.BAR,CombinedChart.DrawOrder.LINE,CombinedChart.DrawOrder.LINE
});
Legend l=combinedChart.getLegend();
l、 setWordWrapEnabled(真);
//设置联合收割机图表的X_轴
XAxis XAxis=combinedChart.getXAxis();
xAxis.setLabelsToSkip(0);
//在联合收割机图表底部设置X_轴位置
xAxis.setPosition(xAxis.XAxisPosition.BOTTOM);//在底部设置xAxis
//仅在右侧设置X_轴
combinedChart.getAxisRight().setDrawLabels(true);
combinedChart.getAxisLeft().setDrawZeroLine(false);
combinedChart.getAxisRight().setAxisMinValue(0);
//将Y_轴设置到联合收割机图表的左侧
YAxis y=combinedChart.getAxisLeft();
y、 setAxisMinValue(0);
y、 设置粒度(1f);
y、 setGranularityEnabled(真);
y、 setAxisLineColor(颜色为黑色);
y、 setTextColor(颜色为黑色);
y、 setEnabled(真);
y、 setAxisLineColor(颜色为黑色);
y、 设置位置(YAxis.YAxisLabelPosition.OUTSIDE_图表);
y、 setDrawGridLines(假);
y、 setLabelCount(5,真);
y、 setDrawZeroLine(假);
y、 IsForceLabelEnabled();
y、 setDrawLabels(true);
y、 setDrawLimitLinesBehindData(真);
y、 setDrawGridLines(真);
y、 setDrawLimitLinesBehindData(真);
y、 setTextColor(颜色为红色);
y、 setDrawZeroLine(真);
//将Y_轴动画设置为5秒
组合图表。动画(0);
//将Y_轴设置到联合收割机图表的右侧
YAxis rightAxis=combinedChart.getAxisRight();
rightAxis.setDrawGridLines(真);
rightAxis.setLabelCount(5,真);
rightAxis.setDrawZeroLine(假);
rightAxis.isForceLabelsEnabled();
rightAxis.setDrawLabels(真);
rightAxis.setDrawLimitLinesBehindData(真);
rightAxis.setDrawGridLines(真);
rightAxis.setDrawLimitLinesBehindData(真);
右轴。setTextColor(颜色。红色);
rightAxis.setDrawZeroLine(真);
//隐藏图表的背景网格线
combinedChart.getXAxis().setDrawGridLines(false);
combinedChart.getAxisLeft().setDrawGridLines(false);
combinedChart.getAxisRight().setDrawGridLines(false);
数据=新的组合数据(轴);
//加载数据时,在图表上显示消息
combinedChart.setNodeDataText(“数据加载”);
//按下开关
flw_Rate_switch.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener()){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
//如果“流速”按钮打开
如果(已检查){
//更改其他开关的状态
lqd_跟随_开关。设置检查(假);
//打开时更改当前开关的颜色
流量开关txt.setTextColor(Color.parseColor(“#536BB5”);
//调用函数生成面积图
mlineData=发电机数据(速率);
data.setData(mlineData);
组合图表。设置数据(数据);
//允许在x轴上同时显示4个值,而不是更多
组合图表。设置VisiblexRangeMaximum(4);
组合图。移动视图TOX(4);
//将y轴动画设置为2秒
组合图表。动画(0);
组合图表。无效();
}
//如果“流速”按钮处于关闭状态
否则{
//更改流量开关的文本颜色
流量\速率\开关\ txt.setTextColor(Color.BLACK);
//关闭开关后,删除面积图的所有数据集
布尔指示符=data.removeDataSet(lineData1.getDataSetByLabel(“流速”,true));
//通知合并已删除数据集的图表
combinedChart.notifyDataSetChanged();
//清晰相对图
combinedChart.clear();
combinedChart.SetNodeDataText(“无数据”);
组合图表。无效();
animateX组合图(500);
}
}
});
以及mygenerateLineData()函数,该函数在打开开关以生成图表数据时调用

专用线路数据生成器数据(列表arr){
LineData d=新的LineData();
ArrayList条目=新的ArrayList();
对于(int index=0;index    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;

                }
            });
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);
            }
        }

    });
 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;
}
    combinedChart.notifyDataSetChanged();
    combinedChart.setData(data);
    combinedChart.invalidate();
chart.refreshDrawableState();