###前提・実現したいこと
CorePlotを用いて、折れ線グラフを作成しようとしています。そこで、下記のようにコードを打ったらエラーが出てしまいました。これは、ソースコードをコピーアンドペーストしたものです。
###発生している問題・エラーメッセージ
Cannot assign value of type 'CorePlotViewController' to type 'CPTPlotDataSource'
dataSourceLinePlot.dataSource = selfの箇所でエラーが出ています。
###該当のソースコード
Swift
1import CorePlot 2import Foundation 3//import Cocoa 4//import UIKit 5 6 7class CorePlotViewController: UIViewController { 8 9 10 private let oneDay : Double = 24 * 60 * 60; 11 12 @IBOutlet var hostView : CPTGraphHostingView? = nil 13 14 private var graph : CPTXYGraph? = nil 15 16 private var plotData = [Double]() 17 18 // MARK: - Initialization 19 override func awakeFromNib() 20 { 21 self.plotData = newPlotData() 22 23 // If you make sure your dates are calculated at noon, you shouldn't have to 24 // worry about daylight savings. If you use midnight, you will have to adjust 25 // for daylight savings time. 26 let refDate = DateFormatter().date(from: "12:00 Oct 29, 2009") 27 28 // Create graph 29 let newGraph = CPTXYGraph(frame: .zero) 30 31 let theme = CPTTheme(named: .darkGradientTheme) 32 newGraph.apply(theme) 33 34 if let host = self.hostView { 35 host.hostedGraph = newGraph; 36 } 37 38 // Setup scatter plot space 39 let plotSpace = newGraph.defaultPlotSpace as! CPTXYPlotSpace 40 41 plotSpace.xRange = CPTPlotRange(location:0.0, length:NSNumber.init(value: oneDay * 5.0)) 42 plotSpace.yRange = CPTPlotRange(location:1.0, length:3.0) 43 44 // Axes 45 let axisSet = newGraph.axisSet as! CPTXYAxisSet 46 if let x = axisSet.xAxis { 47 x.majorIntervalLength = NSNumber.init(value: oneDay) 48 x.orthogonalPosition = 2.0 49 x.minorTicksPerInterval = 0; 50 let dateFormatter = DateFormatter() 51 dateFormatter.dateStyle = .short 52 let timeFormatter = CPTTimeFormatter(dateFormatter:dateFormatter) 53 timeFormatter.referenceDate = refDate; 54 x.labelFormatter = timeFormatter; 55 } 56 57 if let y = axisSet.yAxis { 58 y.majorIntervalLength = 0.5 59 y.minorTicksPerInterval = 5 60 y.orthogonalPosition = NSNumber.init(value: oneDay) 61 62 y.labelingPolicy = .none 63 } 64 65 // Create a plot that uses the data source method 66 let dataSourceLinePlot = CPTScatterPlot(frame: .zero) 67 dataSourceLinePlot.identifier = NSString.init(string: "Date Plot") 68 69 if let lineStyle = dataSourceLinePlot.dataLineStyle?.mutableCopy() as? CPTMutableLineStyle { 70 lineStyle.lineWidth = 3.0 71 lineStyle.lineColor = .green() 72 dataSourceLinePlot.dataLineStyle = lineStyle 73 } 74 75//エラー箇所 76 dataSourceLinePlot.dataSource = self 77 newGraph.add(dataSourceLinePlot) 78 79 self.graph = newGraph 80 } 81 82 func newPlotData() -> [Double] 83 { 84 var newData = [Double]() 85 86 for _ in 0 ..< 5 { 87 newData.append(1.2 * Double(arc4random()) / Double(UInt32.max) + 1.2) 88 } 89 90 return newData 91 } 92 93 // MARK: - Plot Data Source Methods 94 func numberOfRecords(for plot: CPTPlot) -> UInt { 95 return UInt(self.plotData.count) 96 } 97 98 func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any? 99 { 100 switch CPTScatterPlotField(rawValue: Int(field))! { 101 case .X: 102 return (oneDay * Double(record)) as NSNumber 103 104 case .Y: 105 return self.plotData[Int(record)] as NSNumber 106 } 107 } 108 109} 110
###試したこと
上記のソースコードと関連している、DataPlotのAppDelegateのコードを書き直したりなどしましたがダメでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/06 04:18