1
        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

That is my code so far. I would like to plot a function. f(x) = x^2 + 10 should be the function value in this case. I want the x-axis and the y-axis to start at 0 and end at 100.

Can someone help me with implementing this f(x)?

Gabriel
  • 37
  • 4

1 Answers1

0

Your initialization logic of the graph should look like below. Use this in 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)
}

Additionally you need to implement CPTScatterPlotDataSource, where you define the numberOfRecords and respective X and Y values

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

enter image description here

SamB
  • 1,560
  • 1
  • 13
  • 19
  • Is there a good way to create a function which takes two input parameters? It should be of the form f(x) = a * x^2 + c, where the user should be able to set the values for a and c. Any ideas? Thank you for your answer, helped a lot. – Gabriel Sep 22 '21 at 15:22
  • Glad this answer helped you :). You can define two variables for a and c in the VIewController and use them inside the CPTScatterPlotDataSource delegate method like a * (record * record) + c. Could you please upvote the answer if it helped you. If it solved your problem please accept the answer. – SamB Sep 22 '21 at 15:39
  • I cannot upvote yet. I need more reputation. – Gabriel Sep 22 '21 at 15:47
  • No worries. thanks for accepting – SamB Sep 22 '21 at 15:53