【问题标题】:CorePlot - Annotations swinging in the Scatter PlotCorePlot - 在散点图中摆动的注释
【发布时间】:2019-05-15 09:55:22
【问题描述】:

在我的应用程序中,我有一个托管三个散点图的 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)

当绘图数据更新时,注释会动态更新。代码sn-p如下图,

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)] }

问题如图所示,注解随着图形的不断移动而前后移动

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

【问题讨论】:

    标签: ios swift graph annotations core-plot


    【解决方案1】:

    注释在滚动时被锁定到绘图空间,但是您使用每个新数据点更新它们的锚点位置,导致它们跳转到下一个位置。使用图层注释而不是绘图空间注释将它们锁定到位。如果 y 轴的比例发生变化,您需要重新计算 y 位置,但随着图表在其下方滚动,标签将保持原位。

    在构建视图层次结构并布置好所有内容后执行此操作,以便视图计算正确。

    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)
    }
    

    【讨论】:

    • 感谢@Eric,你是 CorePlot 之神!您能否提供任何代码 sn-ps,因为我在这里失去了线索!
    • 现在我已经尝试您的代码得到“'CPTLayerAnnotation' 类型的值没有成员'xConstraints'”这个错误。
    • 你需要使用release-2.3分支上的代码。约束属性是在 2.2 版本之后添加的。
    猜你喜欢
    • 2016-10-18
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多