androidmpandroidchart

MPAndroidChart - Remove Top and Bottom Spaces


I am using MPAndroidChart library.

I implement a PieChart in my application. Everything works fine, but there is a blank space at the top and at the end of the Chart. I need to remove this space to properly visualize my activity layout

My question is:

P.D. In my layout, there is non marginTop or marginBottom.

Layout xml:

<com.github.mikephil.charting.charts.PieChart
   android:id="@+id/pieChart"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_below="@id/_home_consumed" >
</com.github.mikephil.charting.charts.PieChart>

Solution

  • Have you tried calling setOffsets(float left, float top, float right, float bottom) on the Chart?

    // add data...
    chart.setData(...);
    
    // define new offsets
    chart.setOffsets(0, 0, 0, 0);
    
    // update transformation matrix
    chart.getTransformer().prepareMatrixValuePx(chart);
    chart.getTransformer().prepareMatrixOffset(chart);
    
    chart.getContentRect().set(0, 0, chart.getWidth(), chart.getHeight());
    
    // redraw
    chart.invalidate();
    

    This should programamtically eliminate all offsets. Since the chart automatically adjusts it's offsets when calling the setData(...) method, in order to keep the offsets, this method needs to be called everytime a new data object is set for the chart.

    I also know this is very complicated, I will simplify the process in the future.