问题
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