2017-09-15 2 views
0

Je ne peux pas comprendre comment faire un tracé de ligne avec CorePlot 2.2 avec Swift 3 (Xcode 8, iOS 10).Comment faire un graphique linéaire utilisant CorePlot framework et swift 3?

Quelqu'un peut-il expliquer comment le faire?

En particulier, je ne comprends pas comment la dernière fonction numbers (ligne 97-103 (dernières lignes)) fonctionne:

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 numbers(for plot: CPTPlot, field fieldEnum: UInt, recordIndexRange indexRange: NSRange) -> [Any]? { 
     print("xxxxxxx") 
     switch CPTScatterPlotField(rawValue: Int(fieldEnum))! { 
     case .X: 
      return xValues[index] as NSNumber 

     case .Y: 
      return yValues[indexRange] as NSNumber 
     } 

    } */ 

    /* func symbols(for plot: CPTScatterPlot, recordIndexRange indexRange: NSRange) -> [CPTPlotSymbol]? { 
     return xValues 
    } */ 

    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

Pour un nuage de points, cette méthode sera appelée une fois pour le X- valeur et une fois pour la valeur y à chaque indice.

Voici cette méthode de la DatePlot exemple app:

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

    case .Y: 
     return self.plotData[Int(record)] as NSNumber 
    } 
} 
+0

J'ai essayé d'insérer cette fonction (avec changement de ce qu'elle retourne), mais cette fonction n'a même pas été appelé –

+0

Assurez-vous de définir le graphique de 'datasource'. –

+0

Est-ce un protocole pour la classe (du contrôleur de vue)? –