带有Swift格式轴的iOS核心图

时间:2015-09-18 14:05:21

标签: ios swift core-plot axis scatter-plot

嗨,我是核心情节的新手,我有这个问题,而且它有点困扰我... 问题是X和Y轴都不会显示轴标签,轴瓦片,轴majorlengthinterval等。基本上,我不能设置除轴线样式之外的轴属性。以下是我的代码。

class testVC: UIViewController, CPTPlotDataSource, CPTPlotDelegate, CPTPlotSpaceDelegate {

var hostView: CPTGraphHostingView!

let xData = ["2","4"] 
let yData = ["1","2"]

override func viewDidLoad() {
    super.viewDidLoad()


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func symbolForScatterPlot(aPlot: CPTScatterPlot, recordIndex index: UInt) -> CPTPlotSymbol {
    let dotStyle = CPTPlotSymbol()
    dotStyle.size = CGSizeMake(6, 6)
    dotStyle.fill = CPTFill(color: CPTColor.blueColor())
    dotStyle.symbolType = CPTPlotSymbolType.Ellipse
    return dotStyle
}

@IBAction func plotClick(sender: AnyObject) {

    let frame = self.view.frame
    //add graph
    let graph = CPTXYGraph(frame: CGRect(x: 0, y: 50, width: frame.width, height: frame.height - 250))
    graph.paddingBottom = 10
    graph.paddingLeft = 10
    graph.paddingRight = 10
    graph.paddingTop = 10
    graph.title = "Scatter Plot"

    //hostView
    hostView = CPTGraphHostingView(frame: graph.frame)
    self.view.addSubview(hostView)

    //add scatter plot and plot space
    var scatterPlot = CPTScatterPlot()
    scatterPlot = CPTScatterPlot(frame: hostView.frame)
    scatterPlot.delegate = self
    scatterPlot.dataSource = self
    let plotSpace = graph.defaultPlotSpace as! CPTXYPlotSpace
    plotSpace.delegate = self
    plotSpace.allowsUserInteraction = true
    plotSpace.xRange = CPTPlotRange(location: 0, length: 10)
    plotSpace.yRange = CPTPlotRange(location: 0, length: 18)

    scatterPlot.dataLineStyle = nil //hide line

    graph.addPlot(scatterPlot)

    //set axis
    let axes: CPTXYAxisSet = CPTXYAxisSet(layer: graph.axisSet!); let x = axes.xAxis; let y = axes.yAxis
    let lineStyle = CPTMutableLineStyle()
    lineStyle.lineWidth = 3
    x!.axisLineStyle = lineStyle; y!.axisLineStyle = lineStyle
    x!.title = "X"; y!.title = "Y"
    x!.orthogonalPosition = 0; y!.orthogonalPosition = 0
    x!.majorIntervalLength = 1; y!.majorIntervalLength = 1
    x!.minorTickLength = 4; y!.minorTickLength = 4


    hostView.hostedGraph = graph


}

func numberOfRecordsForPlot(plot: CPTPlot) -> UInt {
    return 2
}

func numberForPlot(plot: CPTPlot, field fieldEnum: UInt, recordIndex idx: UInt) -> AnyObject? {
    if fieldEnum == 0 {
        return xData[Int(idx)]
    } else {
        return yData[Int(idx)]
    }

}

1 个答案:

答案 0 :(得分:1)

您没有在属于图表的属性上设置轴属性 - 您正在设置副本然后消失。试试这个:

let axes = graph.axisSet as? CPTXYAxisSet