在我的应用程序中,我有一个 CorePlot,它托管三个散点图,我还配置如下,
let borderStyle = CPTMutableLineStyle()
borderStyle.lineColor = maxPlotColor
let textLayer = CPTTextLayer(text: "MAX")
textLayer.fill = CPTFill(color: .white())
textLayer.cornerRadius = 10.0
textLayer.borderLineStyle = borderStyle
maxLabelAnnotation = CPTPlotSpaceAnnotation(plotSpace: maxLinePlot.plotSpace!, anchorPlotPoint: [5, NSNumber(value: kMaxLineValue)])
maxLabelAnnotation.contentLayer = textLayer
maxLinePlot.graph?.plotAreaFrame?.plotArea?.addAnnotation(maxLabelAnnotation)
当绘图数据更新时,注释也会动态更新。代码片段如下所示,
func newData(_ theTimer: Timer) {
// MARK: Dynmic annotations
maxLabelAnnotation.anchorPlotPoint = [NSNumber(value: currentIndex - 18), NSNumber(value: kMaxLineValue)]
riskLabelAnnotation.anchorPlotPoint = [NSNumber(value: currentIndex - 18), NSNumber(value: kRiskLineValue)] }
我只是想将注释粘贴在一个没有任何摆动的位置。任何帮助将非常感激。提前致谢。
最佳答案
注释在滚动时被锁定到绘图空间,但您可以使用每个新数据点更新它们的 anchor 位置,从而使它们跳转到下一个位置。使用图层注释而不是绘图空间注释将它们锁定到位。如果 y 轴的比例发生变化,您将需要重新计算 y 位置,但当图形在其下方滚动时标签将保持在原位。
在构建 View 层次结构并布置所有内容后执行此操作,以便 View 计算正确。
maxLinePlot.layoutIfNeeded()
if let plotArea = maxLinePlot.graph?.plotAreaFrame?.plotArea {
let plotSpace = maxLinePlot.plotSpace!
let viewPoint = plotSpace.plotAreaViewPoint(forPlotPoint: [0.0, kMaxLineValue])
let maxLabelAnnotation = CPTLayerAnnotation(anchorLayer: plotArea)
maxLabelAnnotation.contentLayer = textLayer
maxLabelAnnotation.xConstraints = CPTConstraints(relativeOffset: 0.25)
maxLabelAnnotation.yConstraints = CPTConstraints(lowerOffset: viewPoint.y)
plotArea.addAnnotation(maxLabelAnnotation)
}
关于ios - CorePlot - 在散点图中摆动的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56146516/