how to set values for xlabels and ylabels in mpandroidchart

旧时模样 提交于 2019-12-10 11:15:43

问题


I am using mpandroidchart android library. I am implementing line charts. Here can i set the x and y labels on my own? currently it is adding the values based on the dataset provided to the chart. Can you please give some idea on this?


回答1:


You have to use a formatter on your axis object.

There is two kind of formatter XAxisValueFormatter and YAxisValueFormatter.

This the code i used to change the X label number with suffix "h" for hours :

//get reference on chart line view
LineChart chart = (LineChart) pActivity.findViewById(R.id.chart);
//set formater for x Label
chart.getXAxis().setValueFormatter(new XAxisValueFormatter() {

        @Override
        public String getXValue(String original, int index, ViewPortHandler viewPortHandler) {
            //return number + "h" here
            // but you can do everything you want here. The string returned will be displayed on chart x label
            return original + "h";
        }
    });
//axis to the bottom
chart.getXAxis().setPosition(XAxisPosition.BOTTOM);
//populate with data
chart.setData(data);
// refresh
chart.invalidate();



回答2:


Could you be more specific? For the x-labels you can set whatever you want in the data object you provide. For the y-labels, call setYRange(...) to set a fixed range of values to show.



来源:https://stackoverflow.com/questions/27061803/how-to-set-values-for-xlabels-and-ylabels-in-mpandroidchart

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