Skip to content

The setAvoidFirstLastClipping of XAxis doesn't work as expected #3819

@longup

Description

@longup

Hi:

I have issue on my phone, when I have setAvoidFirstLastClipping as true, the XAxis's last label entry
in the chart "clip" off the edge of the screen.

Here is my code:

lineChart.getXAxis().setAvoidFirstLastClipping(true);
lineChart.getXAxis().setLabelCount(3,true);
lineChart.setData(new LineData(dataSets));

    xDataList.add("2018-01.01");
    xDataList.add("2018-01.02");
    xDataList.add("2018-01.03");
    xDataList.add("2018-01.04");
    xDataList.add("2018-01.05");
    xDataList.add("2018-01.06");
    xDataList.add("2018-01.07");
    xDataList.add("2018-01.08");
    xDataList.add("2018-01.09");
    xDataList.add("2018-01.10");
    xDataList.add("2018-01.11");
    xDataList.add("2018-01.12");
    xDataList.add("2018-01.13");
    xDataList.add("2018-01.14");
    xDataList.add("2018-01.15");
    xDataList.add("2018-01.16");
    xDataList.add("2018-01.17");
    xDataList.add("2018-01.18");
    xDataList.add("2018-01.19");
    xDataList.add("2018-01.20");
    xDataList.add("2018-01.21");
    xDataList.add("2018-01.22");
    xDataList.add("2018-01.23");
    xDataList.add("2018-01.24");
    xDataList.add("2018-01.25");
    xDataList.add("2018-01.26");
    xDataList.add("2018-01.27");
    xDataList.add("2018-01.28");
    xDataList.add("2018-01.29");
    xDataList.add("2018-01.30");

    IAxisValueFormatter xFormatter = new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return xDataList.get((int)value);
        }
    };
    lineChart.getXAxis().setValueFormatter(xFormatter);

    final DecimalFormat df1 = new DecimalFormat("#.00");;

    IAxisValueFormatter yFormatter = new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return df1.format(value);
        }
    };
    lineChart.getAxisLeft().setValueFormatter(yFormatter);

I found that the method “drawLabels” in class XAxisRenderer has one bug, and I have fixed this issue. If you have the same issue, please update your library after I commit.

String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);

            if (mXAxis.isAvoidFirstLastClippingEnabled()) {

                // avoid clipping of the last
                **#**if (i  / 2 == mXAxis.mEntryCount - 1 && mXAxis.mEntryCount > 1)**** {
                    float width = Utils.calcTextWidth(mAxisLabelPaint, label);

                    if (width > mViewPortHandler.offsetRight() * 2
                            && x + width > mViewPortHandler.getChartWidth())
                        x -= width / 2;

                    // avoid clipping of the first
                } else if (i == 0) {

                    float width = Utils.calcTextWidth(mAxisLabelPaint, label);
                    x += width / 2;
                }
            }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions