SwiftUI code for Core Plot for Mac and iOS

28 views
Skip to first unread message

Jeffrey Terry

unread,
Dec 17, 2020, 9:51:00 PM12/17/20
to coreplot-discuss
Hi All, 

I have posted SwiftUI code that can be used to display a Core Plot on a Mac, iPhone, and iPad on GitHub. https://github.com/jterry94/SwiftUICorePlotExample

It updates plot when the data changes, axis labels change, and when the range changes. 

I would like to thank Fred Appelman for all of his assistance. I never would have gotten this working without his help. I learned a ton about SwiftUI from this exercise. 

I cleaned up the code and added comments. 
I hope it helps someone else. 

Jeff

JustFred

unread,
Dec 18, 2020, 5:26:54 AM12/18/20
to coreplot-discuss
@Jeff: Thank you for the kind words.

You have now given CorePlot 7 arguments, and you can imagine that it is not good practice to add 4 more to set the padding, a few more color control etc.

I have updated my example github which now has an option to set the padding using the technique:

            CorePlot(....)
                .padding(left: 10)
                .padding(right: 10)

Obviously CorePlot has many more settings that you would like to expose, but I just went for the easy ones :-)

Maybe you can add that to your code example.

Jeffrey Terry

unread,
Dec 19, 2020, 1:24:32 AM12/19/20
to coreplot-discuss
Hi Fred, 

I have absolutely no idea how and why what you did there works. 
I don't know how .padding(left:10) somehow sets options.paddingLeft = 10. 

Can you point me to a tutorial or something on using options?

I trying adding another variable, a simple string, and tried setting it but failed. I think this one may be way beyond my understanding of SwiftUI. 

Jeffrey Terry

unread,
Dec 19, 2020, 1:39:17 AM12/19/20
to coreplot-discuss
Never mind. I figured it out. You just chose the function name padding for the extension. You did not have to use a keyword like .padding() from SwiftUI. That function could have been named anything in your extension of CorePlot. I have it working now. 

JustFred

unread,
Dec 19, 2020, 4:40:44 AM12/19/20
to coreplot-discuss
You figured it out :-)

e.g.

    func paddingLeft(_ left: CGFloat) -> CorePlot {
        options.paddingLeft = left
        return self
    }

would have worked as well.

Jeffrey Terry

unread,
Dec 19, 2020, 10:59:25 AM12/19/20
to coreplot-discuss
Added those changes but they only work for static parameters. I made an observable object so I could pass all the changing parameters at once rather than individually. That reduced the number of passed parameters to two. 

JustFred

unread,
Dec 19, 2020, 11:15:38 AM12/19/20
to coreplot-discuss
I would think it should work for dynamic parameters as well.

Something like this:
     @State var paddingSmall = false

     CorePlot(...)
         .padding(left: paddingSmall ? 5 : 25)

should cause a redraw if paddingSmall would change value.

JustFred

unread,
Dec 19, 2020, 11:16:54 AM12/19/20
to coreplot-discuss
The observable object is a good idea btw.

On Saturday, December 19, 2020 at 4:59:25 PM UTC+1 ter...@iit.edu wrote:

Jeffrey Terry

unread,
Dec 19, 2020, 3:25:36 PM12/19/20
to coreplot-discuss

I had tried is as @Binding rather than @State. If I ever figure out when to use @Binding, @State, @EnvironmentObject, etc., it will be a miracle. 

Jeffrey Terry

unread,
Dec 19, 2020, 3:26:35 PM12/19/20
to coreplot-discuss
I think I'm going to leave it that way and pass static and variable plot parameters in the different manner so it is easier to tell the difference between the two. 
Reply all
Reply to author
Forward
0 new messages