关于SO如何在CorePlot中固定轴,这里有很多问题,但是所有问题都引用了axisConstraintsCPTXYAxis属性,而在CorePlot 2.0和更高版本中则不存在。

那么问题是如何在核心图2.0和更高版本中固定Y轴?

我在这里没有做任何花哨的事情:

let graph = CPTXYGraph(frame: self.nativeView.graphView.bounds)

let plotSpace = graph.defaultPlotSpace!
plotSpace.setPlotRange(CPTPlotRange(location: -1, length: 20), forCoordinate: .X)
plotSpace.setPlotRange(CPTPlotRange(location: -10, length: 20), forCoordinate: .Y)
plotSpace.allowsUserInteraction = true
plotSpace.delegate = self

let axisSet = graph.axisSet
if let axis = axisSet?.axisForCoordinate(.Y, atIndex: 0) {
    axis.majorIntervalLength = 5
    axis.minorTicksPerInterval = 5

    let formatter = NSNumberFormatter()
    formatter.positiveFormat = "#"
    formatter.negativeFormat = "#"
    axis.labelFormatter = formatter

    //Here I want to fix this thing..
}

//And then I create a scatter plot and add it and that's it

最佳答案

axisConstraints属性是CPTXYAxis类的一部分。在Swift中,将轴设置为CPTXYAxisSet是最简单的方法:

let axisSet = graph.axisSet as! CPTXYAxisSet
let y = axisSet.yAxis
y.axisConstraints = CPTConstraints(lowerOffset: 0.0)

08-18 14:51