2017-09-19 7 views
0

코어 선이있는 선 그래프를 만들려고했으나 문제가 있습니다. number(for:...) 함수 (마지막 행)가 작동하고 데이터가 검색되지만 시뮬레이터에는 아무런 선이 없습니다. 나는 이것을 60 시간 동안 해보려고 노력해 왔으며 여기서 무엇이 잘못되었는지를 모른다. 그리고 더 이상의 세부 사항은 없습니다. 내가 코드를 확인코어 플롯 선 차트 선이 보이지 않습니다.

import UIKit 
import CorePlot 

class dottedLine: UIViewController { 
    @IBOutlet var hostView: CPTGraphHostingView! 

    var plot: CPTScatterPlot! 

    override func viewDidLayoutSubviews() { 
     super.viewDidLayoutSubviews() 
     initPlot() 
    } 

    let xValues: [NSNumber] = [1,2,3,4] 
    let yValues: [NSNumber] = [1,5,4,3] 

    func initPlot() { 
     configureHostView() 
     configureGraph() 
     configureChart() 
     configureAxes() 
    } 

    func configureHostView() { 
     hostView.allowPinchScaling = false 
    } 

    func configureGraph() { 
     // 1 - Create the graph 
     let graph = CPTXYGraph(frame: hostView.bounds) 
     graph.plotAreaFrame?.masksToBorder = false 
     hostView.hostedGraph = graph 

     // 2 - Configure the graph 
     //graph.apply(CPTTheme(named: CPTThemeName.plainWhiteTheme)) 
     //graph.fill = CPTFill(color: CPTColor.clear()) 
     graph.paddingBottom = 30.0 
     graph.paddingLeft = 30.0 
     graph.paddingTop = 0.0 
     graph.paddingRight = 0.0 

     // 3 - Set up styles 
     let titleStyle = CPTMutableTextStyle() 
     titleStyle.color = CPTColor.black() 
     titleStyle.fontName = "HelveticaNeue-Bold" 
     titleStyle.fontSize = 16.0 
     titleStyle.textAlignment = .center 
     graph.titleTextStyle = titleStyle 

     let title = "Just title" 
     graph.title = title 
     graph.titlePlotAreaFrameAnchor = .top 
     graph.titleDisplacement = CGPoint(x: 0.0, y: -16.0) 

     // 4 - Set up plot space 
     let xMin = 0.0 
     let xMax = 5.0 
     let yMin = 0.0 
     let yMax = 15.0 
     guard let plotSpace = graph.defaultPlotSpace as? CPTXYPlotSpace else { return } 
     plotSpace.xRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(xMin), lengthDecimal: CPTDecimalFromDouble(xMax - xMin)) 
     plotSpace.yRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(yMin), lengthDecimal: CPTDecimalFromDouble(yMax - yMin)) 
    } 

    func configureChart() { 
     // 1 - Set up the plot 
     plot = CPTScatterPlot() 

     // 2 - Set up style 
     let plotLineStile = CPTMutableLineStyle() 
     plotLineStile.lineWidth = 1 
     plotLineStile.lineColor = CPTColor.black() 
     plot.dataLineStyle = plotLineStile 

     // 3- Add plots to graph 
     guard let graph = hostView.hostedGraph else { return } 
     plot.dataSource = self 
     plot.delegate = self 
     graph.add(plot, to: graph.defaultPlotSpace) 
    } 

    func configureAxes() { 
    } 
} 

extension dottedLine: CPTScatterPlotDataSource, CPTScatterPlotDelegate { 
    func numberOfRecords(for plot: CPTPlot) -> UInt { 
     // number of points 
     return UInt(xValues.count) 
    } 

    func scatterPlot(_ plot: CPTScatterPlot, plotSymbolWasSelectedAtRecord idx: UInt, with event: UIEvent) { 
    } 

    func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any? { 
     switch CPTScatterPlotField(rawValue: Int(field))! { 
     case .X: 
      return 2 as NSNumber 
     case .Y: 
      return 3 as NSNumber 
     } 
    } 
} 

답변

0

:

여기 내 코드입니다.

func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any? { 
    switch CPTScatterPlotField(rawValue: Int(field))! { 
    case .X: 
     return 2 as NSNumber 
    case .Y: 
     return 3 as NSNumber 
    } 
} 

은 위의 코드 CPTScatterPlotDataSource에 따르면, 당신은 각각 제공하는 데이터 및 모든 기록은 (X: 2, Y: 3)입니다 동일한 데이터 포인트입니다. 당신이 모든 레코드에 대해 동일한 포인트를주고있는 점을 감안, 산점도에 대한 최종 데이터 세트가 될 것입니다 :

[ 
    (X: 2, Y: 3), 
    (X: 2, Y: 3), 
    (X: 2, Y: 3), 
    (X: 2, Y: 3) 
] 

당신은이 데이터 세트 라인을 얻을 수없는, 그냥 포인트가 될 것입니다.

다른 레코드에 대해 다른 데이터를 제공하려고 시도하면 선이 표시됩니다. 아래 예를 보면 알 수 있습니다.

func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any? { 
    switch CPTScatterPlotField(rawValue: Int(field))! { 
    case .X: 
     let xVal = self.xValues[Int(record)] 
     return xVal 
    case .Y: 
     let yVal = self.yValues[Int(record)] 
     return yVal 
    } 
} 
+0

감사합니다. 나는 마침내 그것을 얻었다. –