Integration CorePlot in SwiftUI on MacOS

This walkthrough shows how to get an example application as present in the core-plot
library working using SwiftUI on MacOS.

Although this example is done on the Mac, there is no reason to assume that the same process would not work for IOS.

1. Create the project

Select: File -> New -> Project...

Create project

Make certain that MacOS, App is selected and press Next.

Select Swift

Make certain the interface is using SwiftUI and the language is Swift.

Press next and save the project.

2. Integrate CorePlot

Select: File -> Add packages...

Add package

Insert the proper URL in selection field.

Select the branch release-2.4. The default master branch will not work, because it does not have support for the Swift Package
Manager (SPM). Select Add Package and on the next screen select also Add Package.

Just for testing run the application and you should see an hello world but this has nothing to do yet with CorePlot.

3. Create the CorePlot integration

Select File -> New -> File...

Coreplot file

Make certain the MacOS and Swift File are selected.Select Next.

Save the file under the name CorePlot.swift

The outline of the source code is as follows:

Outline

  1. NSViewRepresentable is the glue that allows a non SwiftUI view to be used inside SwiftUI
  2. makeNSView is a mandatory method that creates the actual View. The code is coming mostly from the example DatePlot application.
  3. The Coordinator is a class that can be used to manage the view as created in the previous step
  4. updateNSView() is a method that is called by SwiftUI whenever a change is detected. This method can apply those on the view.
  5. Coordinator is the manager of the CorePlot view. Note also that the coordinator functions as the DataSource for CorePlot
  6. External data is initialized when the CorePlot structure is initialized.

3a. makeNSView(Context)

This is the definition of makeNSView:

makeNSView

The changes as compared to the awakeFromNib() from the demo application are as follows:

  1. The plotData is passed into the struct, hence it is not generated here.
  2. The hostView is no longer optional since the view is generated here. So instead of passing the newGraph to the hostView, the newGraph is assigned to a newly generated CTPGraphHostingView.
  3. The data source is not the struct itself, but instead the coordinator which is passed in the context.
  4. The graph is not stored and the created hostview is returned.

3b. makeCoordinator()

makeCoordinator

This is straight forward. The coordinator is passed self so that it can access the content of the CorePlot instance.

3c. updateNSView()

updateNSView

This method must be present to fulfil the protocol requirements. It is called when the view needs updating, hence the call to reloadData().

3d. Coordinator

The coordinator has no mandatory content.

Coordinator

In this implementation

  1. it has a reference to the CorePlot struct.
  2. it implements the Data Source methods

4. Use the new CorePlot view

Change the ContentView to look like this:

ContentView

  1. A helper method to generate the test data
  2. The instantiation of the CorePlot view. It must pass the mandatory plotData argument.

5. Run

This now is a functioning application. Press run and see the result:

View of running application

The source of this project can be found on github.

Comments 4

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.