【问题标题】:Draw the scatter plot using custom label using core plot使用核心图使用自定义标签绘制散点图
【发布时间】:2016-10-30 09:48:05
【问题描述】:

我正在使用散点图在 x 轴上绘制线条和自定义标签。我希望一些线完全从 y 轴开始,一些线从自定义标签位置开始。PFA 图像。

控制线应该从 y 轴开始,应该一直到“Sat”,数据线应该从“Sun”开始,应该画到“Sat”。 任何人都可以帮助我如何实现这一目标

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

这是我正在编写的用于在 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 你能在这方面帮助我吗

【问题讨论】:

    标签: swift graph core-plot


    【解决方案1】:

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

    对于控制线,这些只需要两个数据点,线的每一端一个。左边的点应该有一个绘图空间xRangelocation 的x 值,右边的点应该有一个xRangeend 的x 值。

    【讨论】:

    • 感谢您的回复。我已经用代码再次编辑了这个问题。你能看看它并帮助我吗?
    • 绘图空间的xRange 是什么?此外,数据线的 x 值是错误的。试试return (record * self.tickLocation.count) as! NSNumber。 (我没有对此进行测试——你可能需要一些演员表。)
    • x 和 y 范围是这样的 plotSpace.xRange = CPTPlotRange(location: 0, length: NSNumber.init(value: self.axisArray.count)) plotSpace.yRange = CPTPlotRange(location: yMin ,长度:范围),我尝试了评论中提到的“返回(Int(记录)* self.tickLocation.count)作为NSNumber”的解决方案。它仍然只从 0 开始。
    • 我的建议仅适用于控制线。问题中的代码对于数据线看起来没问题。 tickLocation 数组还有其他地方可以更改吗?尝试记录数据行的返回 x 值,看看它们是否符合您的预期。
    • 我为控制线做了,控制线从“周六”开始,到“周五”结束。我记录了数据线的 x 值,它返回了正确的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多