1
votes

I use the library MPAndroidChart to draw a bar chart.

I have the following problem: When I have more than 4 or 5 bars in my chart x label is not drawed properly.

enter image description here

How can I resolve this issue?

2

2 Answers

3
votes

I would suggest that you either increase the space between the axis labels:

xAxis.setSpaceBetweenLabels(...);

or that you decrease the length of your labels in general, e.g. only first two letters or something like that.

0
votes

Increasing the space between labels can make the labels displace from the bars center position, however you can try spacing.

if spacing doesn't give you expected result ellipsis the label using this function.

public static String ellipsize(String input, int maxCharacters) {
    if (input == null || input.length() < maxCharacters) {
        return input;
    }
    return input.substring(0, maxCharacters - 3) + "...";
}

and implement IAxisValueFormatter to get the formatted text

xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float v, AxisBase axisBase) {
                if((int)v != -1 && (int)v < xAxisValues.size())
                    return ellipsize(xAxisValues.get((int)v),10);
                else
                    return "";
            }
        });