2017-09-19 1 views
0

Ich habe versucht, ein Liniendiagramm mit Core-Plot zu machen, aber ich habe ein Problem. Es gibt keine Linie im Simulator, obwohl die Funktion number(for:...) (letzte Zeilen) funktioniert und die Daten abgerufen werden. Ich habe das 60 Stunden lang versucht, und ich weiß nicht, was hier los ist. Und es gibt keine weiteren Details.Coreplot Liniendiagramm Linie ist nicht sichtbar

Hier ist mein 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 
     } 
    } 
} 

Antwort

0

ich Ihren Code überprüft.

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

Gemäß der oben CPTScatterPlotDataSource des Codes, die Daten, die Sie für jeden Datensatz geben ist die gleiche Datenpunkt, die (X: 2, Y: 3) ist. Vorausgesetzt, dass Sie den gleichen Punkt für alle Datensätze geben, Ihre endgültige Datensatz für den Scatterplot wird:

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

Sie keine Zeile mit diesem Datensatz bekommen, es wird nur ein Punkt sein.

Versuchen Sie, verschiedene Daten für verschiedene Datensätze zu geben, Sie werden eine Zeile sehen. Sie können das Beispiel unten sehen, um zu verstehen

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, vielen Dank. Ich habe es endlich verstanden –

Verwandte Themen