MPChart xAxis值无法在iOS Swift中正确显示

时间:2019-05-09 06:21:44

标签: ios swift bar-chart

我在ios swift中使用MPChart绘制多个条形图。

参考链接-https://github.com/danielgindi/Charts/issues

我的图表绘制得很好,但是在绘制图表的xAxis时遇到了问题。以下是我用于xAxis的代码段

    let xAxis = chartView.xAxis
    xAxis.labelFont = .systemFont(ofSize: 10, weight: .light)
    xAxis.centerAxisLabelsEnabled = true
    let formatter = CustomLabelsAxisValueFormatter()
    formatter.labels = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
    xAxis.valueFormatter = formatter
    xAxis.granularity = 1
    xAxis.drawLabelsEnabled = true
    xAxis.drawGridLinesEnabled = false
    xAxis.labelPosition = .bottom

CustomLabelAxisValueFormatter类-

class CustomLabelsAxisValueFormatter : NSObject, IAxisValueFormatter {

var labels: [String] = []

func stringForValue(_ value: Double, axis: AxisBase?) -> String {

    let count = self.labels.count

    guard let axis = axis, count > 0 else {

        return "-"
    }

    let factor = axis.axisMaximum / Double(count)

    let index = Int((value / factor).rounded(.down))

    if index >= 0 && index < count {

        return self.labels[index]
    }

    return "-"
 }
}

如您在输出屏幕截图中所见,xAxis缺少“ Sun”,其他标签也未与条对齐 enter image description here 有人可以帮我解决这个问题。

**请注意,我的图表既不能垂直滚动也不能水平滚动

0 个答案:

没有答案