0

I have a HorizontalBarChart with mpAndroidChart and I am having problems to display the labels on the left side, but within the graph. It looks like this:

enter image description here

the labels are chopped of on the left side. This is done via the line

testchart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE)

On the right side, it works smoothly:

enter image description here

The labels are inside the graph and fully displayed. This is done by the line:

testchart.getXAxis().setPosition(XAxis.XAxisPosition.TOP_INSIDE)

Any idea what I am doing wrong?

My Code for the chart is:

BarData data = new BarData(new BarDataSet(entries, "Labeltest"));

        data.setBarWidth(1); // set custom bar width
        data.setDrawValues(false);
        oBinding.testchart.setData(data);
        oBinding.testchart.getXAxis().setLabelCount(labels.size());

        oBinding.testchart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
        oBinding.testchart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));

        // Hide grid lines
        oBinding.testchart.getAxisLeft().setEnabled(false);
        oBinding.testchart.getAxisRight().setEnabled(false);
        // Hide graph description
        oBinding.testchart.getDescription().setEnabled(false);
        // Hide graph legend
        oBinding.testchart.getLegend().setEnabled(false);


        oBinding.testchart.invalidate(); // refresh

and in XML:

<com.github.mikephil.charting.charts.HorizontalBarChart
            android:id="@+id/testchart"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
Apfelsaft23
  • 316
  • 2
  • 23
  • Did you ever find a solution to this? I am having the same problem and my labels are not the same length of text e.g. label1="dev", label2="development process". I wouldn't mind using the xOffset solution if I could find a way to left align the label text. – Scott Evans Sep 25 '19 at 09:07
  • NO, unfortunetely I never found the solution. I dropped the original UI design and worked around this Problem. AFAIK MPandroid was refactored, probably they have improved the horitontal bar diagramm - TBH, I found MPandroid to be quite lackluster in this regard. Please let us know if the new version is better. – Apfelsaft23 Sep 25 '19 at 10:38
  • I also had to abandon that UI design. I went for using BOTTOM_INSIDE as this was the only way to get the data on the chart and looking remotely close to the original design. I'll say this much - MPAndroidCharts is far easier than creating everything from scratch yourself, it just seems to take a lot of fiddling to adjust. Could be worse/better. – Scott Evans Sep 27 '19 at 05:04

2 Answers2

0

I have encountered the same problem, I solved it this way.

chart.getXAxis().setPosition(XAxisPosition.BOTTOM_INSIDE)

In that way the labels should be drawn over the bars. You can further reposition the labels by using the setXOffset(...) and setYOffset(...) methods of the XAxis class.

If you have a better way, please let me know, thank you

Lookaji
  • 1,023
  • 10
  • 21
0

Try this:

XAxis xAxis = chart.getXAxis();  
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);

Added in response to comment from @andreas-zuercher:

This way has always worked for me. This also makes it easy to set other axis parameters like:

    xAxis.setDrawGridLines(true);
    xAxis.setGranularity(1f);
    xAxis.setGranularityEnabled(true);
    final String[] xTime = time.split(",");
    xAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return xTime[(int) value-1];
        }

    });
    xAxis.setValueFormatter(new IndexAxisValueFormatter(xTime));
Bob_in_NJ
  • 11
  • 4