【问题标题】:How do I add values to my graph in core plot swift?如何在核心绘图中快速向我的图表添加值?
【发布时间】:2021-09-21 16:39:53
【问题描述】:
        let graph = CPTXYGraph(frame: hostview.bounds)
        hostview.hostedGraph = graph
        graph.paddingLeft = 0.0
        graph.paddingTop = 0.0
        graph.paddingRight = 0.0
        graph.paddingBottom = 0.0
        graph.axisSet = nil

到目前为止,这是我的代码。我想绘制一个函数。 f(x) = x^2 + 10 在这种情况下应该是函数值。我希望 x 轴和 y 轴从 0 开始,到 100 结束。

有人可以帮我实现这个 f(x) 吗?

【问题讨论】:

    标签: ios swift plot core-plot math-functions


    【解决方案1】:

    图表的初始化逻辑应如下所示。在viewDidLoad中使用它

    func initPlot() {
        let graph = CPTXYGraph(frame: hostView.bounds)
        graph.plotAreaFrame?.masksToBorder = false
        hostView.hostedGraph = graph
        graph.backgroundColor = UIColor.white.cgColor
        graph.paddingBottom = 40.0
        graph.paddingLeft = 40.0
        graph.paddingTop = 40.0
        graph.paddingRight = 40.0
    
        //configure title
        let title = "f(x) = x*x + 10"
        graph.title = title
        
        //configure axes
        let axisSet = graph.axisSet as! CPTXYAxisSet
    
        if let x = axisSet.xAxis {
            x.majorIntervalLength   = 20
            x.minorTicksPerInterval = 1
        }
    
        if let y = axisSet.yAxis {
            y.majorIntervalLength   = 5
            y.minorTicksPerInterval = 5
        }
    
        let xMin = 0.0
        let xMax = 100.0
        let yMin = 0.0
        let yMax = 100.0
        guard let plotSpace = graph.defaultPlotSpace as? CPTXYPlotSpace else { return }
        plotSpace.xRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(xMin), lengthDecimal: CPTDecimalFromDouble(xMax - xMin))
        plotSpace.yRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(yMin), lengthDecimal: CPTDecimalFromDouble(yMax - yMin))
        
        //create the plot
        plot = CPTScatterPlot()
        plot.dataSource = self
        graph.add(plot, to: graph.defaultPlotSpace)
    }
    

    此外,您需要实现 CPTScatterPlotDataSource,在其中定义 numberOfRecords 以及相应的 X 和 Y 值

    extension ViewController: CPTScatterPlotDataSource {
        func numberOfRecords(for plot: CPTPlot) -> UInt {
            return 100
        }
        
        func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any? {
            switch CPTScatterPlotField(rawValue: Int(field))! {
            case .X:
                return record
            case .Y:
                return (record * record) + 10
            default:
                return 0
            }
        }
    }
    

    【讨论】:

    • 有没有好的方法来创建一个接受两个输入参数的函数?它应该是 f(x) = a * x^2 + c 的形式,用户应该能够设置 a 和 c 的值。有任何想法吗?谢谢你的回答,帮了大忙。
    • 很高兴这个答案对您有所帮助:)。您可以在 VIewController 中为 a 和 c 定义两个变量,并在 CPTScatterPlotDataSource 委托方法中使用它们,如 * (record * record) + c。如果对您有帮助,您能否为答案投票。如果它解决了您的问题,请接受答案。
    • 我还不能投票。我需要更多的声誉。
    • 不用担心。感谢接受
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多