如何删除 xAxis 和图表之间的 space?

How can i remove the space between xAxis and the chart?

在下图中,图表的 x 轴和条形底部之间有一个 space,即红色区域。

我想知道如何删除这样的 space 以便标签更接近条形图。这是我的代码:

        let chart = BarChartView()
               
        chart.leftAxis.drawGridLinesEnabled = true
        chart.leftAxis.drawAxisLineEnabled = false
        chart.leftAxis.axisLineColor = .paleBlue
        
        chart.rightAxis.drawGridLinesEnabled = false
        chart.rightAxis.drawAxisLineEnabled = false
        chart.rightAxis.drawLabelsEnabled = false
        
        chart.xAxis.drawGridLinesEnabled = false
        chart.xAxis.labelPosition = .bottom
        chart.xAxis.centerAxisLabelsEnabled = true
        chart.xAxis.granularityEnabled = true
        chart.xAxis.enabled = true
        
        chart.legend.enabled = true
        chart.legend.horizontalAlignment = .center
        
        chart.doubleTapToZoomEnabled = false
        chart.pinchZoomEnabled = false
        
        chart.noDataText = ""

修补代码

chart.xAxis.yOffset = 0

来自源代码,

开始于

@objc(ChartXAxis)
open class XAxis: AxisBase

然后

/// Base class for all axes
@objc(ChartAxisBase)
open class AxisBase: ComponentBase

明白了,lib作者已经在评论里解释了

/// This class encapsulates everything both Axis, Legend and LimitLines have in common
@objc(ChartComponentBase)
open class ComponentBase: NSObject
{
    /// flag that indicates if this component is enabled or not
    @objc open var enabled = true
    

    /// The offset this component has on the x-axis
    /// **default**: 5.0
    @objc open var xOffset = CGFloat(5.0)
    
    /// The offset this component has on the x-axis
    /// **default**: 5.0 (or 0.0 on ChartYAxis)
    @objc open var yOffset = CGFloat(5.0)
    
    public override init()
    {
        super.init()
    }

    @objc open var isEnabled: Bool { return enabled }
}

为了解决我的问题,我发现我必须删除 xAxis 线,即红色矩形内的线。

我通过将属性 xAxis.drawAxisLineEnabled 更改为 false 来做到这一点。 然后我得到了一个白色的space区域,这里涂成了红色。

为了删除此 space,我将属性 xAxis.yOffset 更改为 -10,这使 xAxis 标签更接近图表。

我必须将 xAxis.drawAxisLineEnabled 更改为 false,因为此行不受 xAxis.yOffset 影响,如果我没有将其设置为 false,结果将是这样。