如何将时间作为xAxis数据值在方法numberForPlot中使用核心图?

我需要创build“心率与时间”图。 在xAxis中,我显示的格式是“HH:mm:ss”,y轴是它的十进制值。 我的代码如下:

图的数据源:

var graphData:[(String,Double)] = [] let data = ("17:56:35",Double(65)) let data1 = ("18:04:13",Double(95)) let data2 = ("18:8:35",Double(25)) self.graphData.append(data) self.graphData.append(data1) self.graphData.append(data2) 

设置graphics和绘图数据代码:

 func setGraph() { let tts = CPTMutableTextStyle() tts.fontSize = 75.0 tts.color = CPTColor(CGColor: UIColor.blackColor().CGColor) tts.fontName = "HelveticaNeue-Bold" self.graph.titleTextStyle = tts self.graph.title = "Heart Rate vs Time" self.graph.applyTheme(CPTTheme(named:kCPTPlainWhiteTheme)) let plotSpace = graph.defaultPlotSpace as! CPTXYPlotSpace! plotSpace.allowsUserInteraction = false let xRange = plotSpace.xRange.mutableCopy() as! CPTMutablePlotRange xRange.locationDouble = Double(0) xRange.lengthDouble = Double(1000) plotSpace.xRange = xRange let yRange = plotSpace.yRange.mutableCopy() as! CPTMutablePlotRange yRange.locationDouble = Double(0) yRange.lengthDouble = Double(210) plotSpace.yRange = yRange graph.addPlotSpace(plotSpace) graph.plotAreaFrame!.paddingTop = 0 graph.plotAreaFrame!.paddingRight = 0 graph.plotAreaFrame!.paddingBottom = graphOffset graph.plotAreaFrame!.paddingLeft = graphOffset graph.plotAreaFrame!.masksToBorder = false // Grid line styles let majorLineStyle = CPTMutableLineStyle() majorLineStyle.lineWidth = 0.75 majorLineStyle.lineColor = CPTColor.redColor() let minorLineStyle = CPTMutableLineStyle() minorLineStyle.lineWidth = 0.25 minorLineStyle.lineColor = CPTColor.blackColor() //Axis Line colors let axisLineStyle = CPTMutableLineStyle() axisLineStyle.lineWidth = 2.0 axisLineStyle.lineColor = CPTColor.blackColor() //Axis Label colors let labelTextStyle = CPTMutableTextStyle() labelTextStyle.textAlignment = CPTTextAlignment.Left labelTextStyle.color = CPTColor.blackColor() //Axis title color let titleTextStyle = CPTMutableTextStyle() titleTextStyle.textAlignment = CPTTextAlignment.Left titleTextStyle.color = CPTColor.blackColor() titleTextStyle.fontSize = 15 let dataSourceLinePlot = CPTScatterPlot() let lineStyle = CPTMutableLineStyle() lineStyle.lineWidth = 3.0 lineStyle.lineColor = CPTColor.blueColor() dataSourceLinePlot.dataLineStyle = lineStyle dataSourceLinePlot.identifier = kPlotIdentifier // dataSourceLinePlot.dataSource = self let xts = CPTMutableTextStyle() xts.color = CPTColor(componentRed: 255.0, green: 255.0, blue: 255.0, alpha: 1.0) let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "HH:mm:ss" let timeformatter = CPTTimeFormatter(dateFormatter: dateFormatter) timeformatter.referenceDate = NSDate() let axisSet = graph.axisSet as! CPTXYAxisSet! let xAxis = axisSet.xAxis as CPTXYAxis! xAxis.axisTitle = CPTAxisTitle(text: "Elapsed Time", textStyle: xts) xAxis.labelFormatter = timeformatter xAxis.majorGridLineStyle = majorLineStyle xAxis.minorGridLineStyle = minorLineStyle xAxis.majorIntervalLength = NSNumber(double: 60.0) let yAxis = axisSet.yAxis as CPTXYAxis! yAxis.axisTitle = CPTAxisTitle(text: "Heart Rate", textStyle: xts) let axisFormatter = NSNumberFormatter() axisFormatter.maximumFractionDigits = 0 axisFormatter.minimumIntegerDigits = 1 yAxis.labelFormatter = axisFormatter yAxis.titleOffset = 35.0 yAxis.majorIntervalLength = 20 yAxis.majorGridLineStyle = majorLineStyle yAxis.minorGridLineStyle = minorLineStyle graph.addPlot(dataSourceLinePlot) self.IBviewGraph.hostedGraph = self.graph } 

绘制数据:

 func setGraphData() { dispatch_sync(dispatch_get_main_queue(), { let plot = CPTScatterPlot() plot.dataSource = self let actualPlotStyle = plot.dataLineStyle!.mutableCopy() as! CPTMutableLineStyle actualPlotStyle.lineWidth = 2.0 actualPlotStyle.lineColor = CPTColor(CGColor: (UIColor.yellowColor().CGColor)) plot.dataLineStyle = actualPlotStyle plot.interpolation = .Linear self.graph.addPlot(plot) }) } 

数据源方法

  func numberOfRecordsForPlot(plot: CPTPlot) -> UInt { return 3 } func numberForPlot(plot: CPTPlot, field: UInt, recordIndex: UInt) -> AnyObject?{ var num = NSNumber() let index = Int(recordIndex) switch field { case 0://xaxis return graphData[index].0 case 1://yaxis return graphData[index].1 default: break } print("coordintes = ",num) return num } 

我的图是淹没的,但只有y轴的点是正确的,它不考虑xAxis值,所以,因为这些点在图中错误的位置。 我认为我传递给xAxis的方法numberForPlot的值是错误的,但不知道如何传递它。 给我一些解决scheme。

图片

graphData数组中的x值是string。 Core Plot期望数据源为每个索引返回一个数字值。 它从时间string读取小时数,并将其用于x值。

x轴configuration为显示范围在0到1000之间的数字。 因此,您需要将时间数据转换为该范围内的数字。 跟踪用于格式化轴标签的参考date(即将其存储在实例variables中),并使用该date将时间数据转换为距参考date(以秒为单位)的偏移量。