How to get label for corresponding swipe in barchart of mpandroidchart?

馋奶兔 提交于 2019-12-13 08:28:27

问题


How to get label for corresponding bar when I swipe the barchart? I have another textview outside of barchart. When I swipe mpandroidchart graph, I need to get x axis label of corresponding position and display in textview. How to do that?

I tried with this.

 barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
            Log.i("Entry", String.valueOf(e));
        }

        @Override
        public void onNothingSelected() {

        }
    });

or OnChartGestureListener too, I tried but I don't know how to get the label of the corresponding bar.


回答1:


In case of OnChartValueSelectedListener, you can do

barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
            String value = barChart.getBarData().getXVals().get(e.getXIndex());
        }

        @Override
        public void onNothingSelected() {

        }
    });

In the similar way you can achieve this for other listeners too.




回答2:


Implement mChart.setOnChartGestureListener method, and you will see onChartTranslate Override method.

mChart.getXValue(mChart.getHighestVisibleXIndex());

This will return you the value of the highest visible index after swipe. Hope this answer will help you to solve your problem.



来源:https://stackoverflow.com/questions/37321426/how-to-get-label-for-corresponding-swipe-in-barchart-of-mpandroidchart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!