2017-09-19 2 views
0

J'ai essayé de faire un graphique linéaire avec l'intrigue centrale mais j'ai un problème. Il n'y a pas de ligne dans le simulateur bien que la fonction number(for:...) (dernières lignes) fonctionne et que les données soient récupérées. J'ai essayé de faire cela pendant 60 heures et je ne sais pas ce qui ne va pas ici. Et il n'y a pas plus de détails.La ligne de graphique de ligne de Coreplot n'est pas visible

Voici mon code:

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 
     } 
    } 
} 

Répondre

0

J'ai vérifié votre code.

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 
    } 
} 

Selon le CPTScatterPlotDataSource ci-dessus de votre code, les données que vous donnez pour chaque enregistrement est le même point de données qui est (X: 2, Y: 3). Étant donné que vous donnez le même point pour tous les enregistrements, votre ensemble de données final pour le nuage de points sera:

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

Vous ne pouvez pas obtenir une ligne avec cet ensemble de données, il sera juste un point. Essayez de donner des données différentes pour différents enregistrements, vous verrez une ligne. Vous pouvez voir l'exemple ci-dessous pour comprendre

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

Omg, merci beaucoup. J'ai enfin compris –