ios - CorePlot - 在散点图中摆动的注释

标签 ios swift graph annotations core-plot

在我的应用程序中,我有一个 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)] }

问题如图所示,随着图形的不断移动,注释会来回移动 graph with annotation swinging

我只是想将注释粘贴在一个没有任何摆动的位置。任何帮助将非常感激。提前致谢。

最佳答案

注释在滚动时被锁定到绘图空间,但您可以使用每个新数据点更新它们的 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/

相关文章:

iOS 9 - 请求照片的相机授权

Swift 3 - 原子 boolean 值

ios - 2010 年 10 月的 Macbook Airs 是否足以运行所有工具来开发 iPhone 应用程序?

ios - 部署后向 App-Info.plist 添加值

ios - NSTimer 和 NSDateFormatter 麻烦。我的 timerLabel 刷新后消失了吗?

ios - 按索引移动数组中的元素

ios - 我的 CoreGraphics 代码运行非常缓慢。我怎样才能让这个自定义的 UIScrollView 表现得更好?

arrays - 0's and 1' 数组的状态数是多少

python - Python中直线(数字线)上的图形点

graph - 如何影响图形项的布局?