gpt4 book ai didi

swift - 使用核心图使用自定义标签绘制散点图

转载 作者:行者123 更新时间:2023-11-30 12:56:38 25 4
gpt4 key购买 nike

我使用散点图在 x 轴上绘制线条和自定义标签。我希望一些线条完全从 y 轴开始,一些线条从自定义标签 location.PFA 图像开始。 enter image description here

控制线应从 y 轴开始,一直到“Sat”,数据线应从“Sun”开始,一直绘制到“Sat”。谁能帮我实现这一目标

编辑1:-正如 Eric 所建议的,我尝试使用 2 条记录作为控制线和数据线,对于图 x,我返回 x 轴的刻度位置数组,对于图 y,我返回图形的 y 值。通过这样做,控制线是正确的,但数据线总是从零开始。 PFA 附图enter image description here

这是我正在编写的用于在 x 轴上创建标签的代码

if(self.axisArray.count == 7) {
tickLocation = [1, 2, 3, 4, 5, 6, 7]
}
else {
tickLocation = [1, 2, 3, 4, 5, 6, 7, 8]
}

var xLocations = [NSNumber]()
var labelLocation: Int = -1
for number in tickLocation {
labelLocation += 1
let newLabel = CPTAxisLabel.init(text: axisArray[labelLocation] as? String, textStyle: axisLabelStyle)
newLabel.tickLocation = number as! NSNumber
newLabel.offset = x.labelOffset + x.majorTickLength
xLocations.append(NSNumber(value: number as! Double))
customLabel.append(newLabel)
}
x.labelingPolicy = CPTAxisLabelingPolicy.none
x.axisLabels = Set(customLabel)
x.majorTickLocations = Set(xLocations)

下面的数据源是代码

func numberOfRecords(for plot: CPTPlot) -> UInt
{
if(plot.identifier as! String == BloodGlucoseGraphView.kUpperControlLineIdentifier) {
return UInt(2)
}
else if(plot.identifier as! String == BloodGlucoseGraphView.kLowerControlLineIdentifier) {
return UInt(2)
}
else if(plot.identifier as! String == BloodGlucoseGraphView.kFirstGraphIdentfifier) {
return UInt(self.beforeMealData.count)
}
else if(plot.identifier as! String == BloodGlucoseGraphView.kSecondGraphIdentfifier) {
return UInt(self.afterMealData.count)
}
else {
return UInt(0)
}
}

func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any?
{
let plotField = CPTScatterPlotField(rawValue: Int(field))
if(plot.identifier as! String == BloodGlucoseGraphView.kUpperControlLineIdentifier) {
if(plotField == .X) {
return Int(record)
}
else if(plotField == .Y) {
return 6
}
}
else if(plot.identifier as! String == BloodGlucoseGraphView.kLowerControlLineIdentifier) {
if(plotField == .X) {
return Int(record)
}
else if(plotField == .Y) {
return 5.2
}
}
if(plot.identifier as! String == BloodGlucoseGraphView.kFirstGraphIdentfifier) {
if(plotField == .X) {
return self.tickLocation[Int(record)] as! NSNumber
}
else if(plotField == .Y) {
return self.afterMealData[Int(record)]
}
}
else if(plot.identifier as! String == BloodGlucoseGraphView.kSecondGraphIdentfifier) {
if(plotField == .X) {
return self.tickLocation[Int(record)] as! NSNumber
}
else if(plotField == .Y) {
return self.beforeMealData[Int(record)]
}
}
return nil
}

但是数据线也是从零开始的,而不是从“太阳”开始。 @Eric 你能在这方面帮助我

最佳答案

对于数据行,请确保从数据源返回的 x 值与相应 x 轴标签的刻度位置匹配。从图片来看,它看起来像是一个相差一的错误。

对于控制线,仅需要两个数据点,线的每一端各一个。左侧点应具有绘图空间 xRangelocation 的 x 值,右侧点应具有 end 的 x 值xRange 的 >。

关于swift - 使用核心图使用自定义标签绘制散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40327797/

25 4 0