iOS-Charts xAxis Labels cut off

iOS-Charts xAxis Labels cut off

我正在使用 iOS-Charts 来显示水平条形图。 左侧的 x 轴标签被截断。 只有当我双击图表时,才会显示正确的尺寸并且标签不再被切断。

这是我正在使用的代码

    func setChart(_ dataPoints: [(String,Int)], chart: HorizontalBarChartView) {

    chart.noDataText = "No data available."

    var dataEntries: [BarChartDataEntry] = []

    let maxNumberEntries = dataPoints.count

    var xAxisLabel: [String] = []

    var counter:Int = maxNumberEntries-1
    for _ in 0..<maxNumberEntries {
        let dataEntry = BarChartDataEntry(x: Double(counter), yValues: [Double(dataPoints[counter].1)], label: dataPoints[counter].0)
        dataEntries.append(dataEntry)
        xAxisLabel.append(dataPoints[counter].0)
        counter -= 1
    }

    xAxisLabel = xAxisLabel.reversed()

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "")
    let chartData = BarChartData(dataSet: chartDataSet)
    chart.data = chartData
    chart.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)

    // disable zoom of chart
    chart.pinchZoomEnabled = false
    chart.scaleXEnabled = false
    chart.scaleYEnabled = false

    chart.chartDescription?.text = ""
    chart.legend.enabled = false

    // disable selection of bars
    chartDataSet.highlightEnabled = false
    chartDataSet.valueFont = NSUIFont.systemFont(ofSize: 10)

    let numberFormatter = ValueFormatter()
    chartData.setValueFormatter(numberFormatter)

    // specify the width each bar should have
    let barWidth = 0.8
    chartData.barWidth = barWidth

    let formato:BarChartFormatter = BarChartFormatter()
    formato.strings = xAxisLabel
    let xaxis:XAxis = XAxis()

    _ = formato.stringForValue(Double(1), axis: xaxis)
    xaxis.valueFormatter = formato
    chart.xAxis.valueFormatter = xaxis.valueFormatter

    let xAxis = chart.xAxis
    xAxis.labelPosition = XAxis.LabelPosition.bottom // label at bottom
    xAxis.drawGridLinesEnabled = false
    xAxis.granularity = 1.0
    xAxis.labelCount = maxNumberEntries
    xAxis.labelRotationAngle = 0

    // Don't show other axis
    let leftAxis = chart.leftAxis
    leftAxis.enabled = false
    let rightAxis = chart.rightAxis
    rightAxis.enabled = false

}

知道如何解决这个问题吗?

截图:

cut-off xAxis labels

after double tap the labels are not cut-off anymore

我打电话解决了这个问题

chart.fitScreen()

所有数据通过后,对每个条形图。

如果有人一直在努力并且回答没有帮助,一旦你用新数据设置图表,不要忘记在图表上调用notifyDataSetChanged,否则标签会被切断。我就是这样。

如果即使在 notifyDataSetChanged 之后您的值仍被切断,请尝试像这样抵消它:

mChart.extraTopOffset = 20

HorizontalBarChart 标签剪切问题已解决,您只需将此代码放入图表设置即可:

chart.extraRightOffset = 30