Android MPChart - 如何更新stackLabel?

时间:2017-09-23 07:13:08

标签: android charts mpandroidchart

我正在使用MPChart并为不同的标签设置了多个图表,因此在切换标签时,应该刷新堆积的条形图及其标签。

代码:

BarDataSet set1;

    if (chart_stacked.getData() != null && chart_stacked.getData().getDataSetCount() > 0)
    {
        set1 = (BarDataSet) chart_stacked.getData().getDataSetByIndex(0);
        set1.setValues(yVals1);
        chart_stacked.getData().notifyDataChanged();
        chart_stacked.notifyDataSetChanged();
    }
    else
    {
        // set1 = new BarDataSet(yVals1, "Toilet Statistics");
        set1 = new BarDataSet(yVals1, "");
        set1.setDrawIcons(false);
        set1.setColors(getColors());

        if (chart_type.equals("feed"))
        {
            set1.setStackLabels(new String[]{"A", "B"});
        }
        if (chart_type.equals("pump"))
        {
            set1.setStackLabels(new String[]{"C", "D"});
        }

        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);

        BarData data = new BarData(dataSets);
        data.setValueTextColor(Color.BLACK);

        chart_stacked.setData(data);
    }
    chart_stacked.invalidate();
    Utilities.custom_toast(Stat.this, "chart type = " + chart_type, "gone", "short");

问题:

最后的吐司显示,当按下不同的标签时,变量chart type会成功更新。条形图更新。但是,当按下C&amp; D页面时,stacklabel仍然显示A和B而不是C和D.

如何刷新stacklabel?谢谢。

1 个答案:

答案 0 :(得分:0)

替换它:

chart_stacked.invalidate();
Utilities.custom_toast(Stat.this, "chart type = " + chart_type, "gone", 
"short");

使用:

chart_stacked.notifyDataSetChanged();
chart_stacked.invalidate();
Utilities.custom_toast(Stat.this, "chart type = " + chart_type, "gone", "short");