MPAndroidChart - 删除顶部和底部空间

MPAndroidChart - Remove Top and Bottom Spaces

我正在使用 MPAndroidChart 库。

我在我的应用程序中实现了饼图。一切正常,但在图表的顶部和末尾有一个空白 space。 我需要删除此 space 以正确显示我的 activity 布局

我的问题是:

P.D。在我的布局中,没有 marginTop 或 marginBottom。

布局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>

您试过在 Chart 上调用 setOffsets(float left, float top, float right, float bottom) 吗?

// 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();

这应该以编程方式消除所有偏移量。由于图表在调用 setData(...) 方法时自动调整其偏移量,为了保持偏移量,每次为图表设置新数据对象时都需要调用此方法。

我也知道这个很复杂,以后我会简化流程。

试试这个...

pieChart.setExtraOffsets(-10,-10,-10,-10);

Half Pie Chart Image using MPCHART

您可以像下面这样设置负值偏移量:- transactionTrendPieChart.setExtraOffsets(0,-100,0,-150);

饼图不仅有默认偏移量 10,而且还添加了额外的 space 以让您突出显示 已选择 条目(see this comment on GitHub。要删除所有填充,您还需要删除所选条目的 space:

pieChart.setExtraOffsets(0, 0, 0, 0);

PieDataSet dataSet = new PieDataSet([..]);
dataSet.setSelectionShift(0f);

Andrew Briggschart.setMinOffset(0); 答案对我有用。