gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:40:05 29 4
gpt4 key购买 nike

我需要创建“心率与时间”图表。在 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)

设置图形和绘制数据代码:

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
}

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

image

最佳答案

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

x 轴配置为显示 0 到 1,000 范围内的数字。因此,您需要将时间数据转换为该范围内的数字。跟踪用于格式化轴标签的引用日期(即将其存储在实例变量中),并使用它来将时间数据转换为引用日期的偏移量(以秒为单位)。

关于ios - 如何使用核心图在方法 numberForPlot 中将时间作为 xAxis 数据值传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38184749/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com