Hi, I’ve found that there is no example of using SciChart in SwiftUI, the swift examples are using storyboard.
I’ve tried that, using SciChart in SwiftUI, and it worked. But I have some confusions about axis update and data update.
in SwiftUI, first I create a chartview struct as NSViewRepresentable, and in func makeNSView(context: Context) -> SCIChartSurface{} to initialize the chartview as any supported SciChart view type.
second, in func updateNSView(_ nsView: SCIChartSurface, context: Context){} to update the data and axis.
When updating axis(like visiablerange), every time I need call nsView.xAxes.clear() to remove axis first and then add axis again.
When updating data, every time I need to call nsView.renderableSeries.clear() to clear current plot data first and then call nsView.renderableSeries.add() to update the data.
My confusion is that is this the right way to do that? It seems not quite efficient.
- Keyu Yan asked 2 years ago
- You must login to post comments
Hi there
We have an example of how to use SciChart iOS in SwiftUI here:
Creating your first SciChart iOS App
Scroll down to
Adding SCIChartSurface to SwiftUI App
Since SCIChartSurface is an UIKit UIView under the hood, you can use
UIViewRepresentable protocol to integrate our SCIChartSurface into a
SwiftUI view hierarchy. There are two approaches here:
- Configure SCIChartSurface all in one place
- Using SwiftUI-style helper functions
Method 1: Configure SCIChartSurface all in one place
We begin by declaring a SurfaceView struct, which conforms to the
UIViewRepresentable protocol. Import SciChart, instantiate a
SCIChartSurface, add axes, renderable series, and other elements to
your SCIChartSurface as described above. Your final code will look
something like this:import SwiftUI import SciChart struct SurfaceView: UIViewRepresentable { func makeUIView(context: Context) -> SCIChartSurface { let surface = SCIChartSurface() let xAxis = SCINumericAxis() xAxis.growBy = SCIDoubleRange(min: 0.05, max: 0.05) let yAxis = SCINumericAxis() yAxis.growBy = SCIDoubleRange(min: 0.05, max: 0.05) let count = 1000 let xValues = SCIDoubleValues(capacity: count) let yValues = SCIDoubleValues(capacity: count) for i in 0 ..< count { let x: Double = 10.0 * Double(i) / Double(count) let y: Double = sin(2 * x) xValues.add(x) yValues.add(y) } let dataSeries = SCIXyDataSeries(xType: .double, yType: .double) dataSeries.append(x: xValues, y: yValues) let renderableSeries = SCIFastLineRenderableSeries() renderableSeries.dataSeries = dataSeries SCIUpdateSuspender.usingWith(self.surface) { self.surface.xAxes.add(xAxis) self.surface.yAxes.add(yAxis) self.surface.renderableSeries.add(renderableSeries) } return surface } func updateUIView(_ uiView: SCIChartSurface, context: Context) {} }
Next, place your SurfaceView instance into your ContentView body, same
as any other SwiftUI view, like this:struct ContentView: View { var body: some View { SurfaceView() } }
Method 2: SwiftUI helper functions
In the example above, we configure SCIChartSurface the old-fashioned
imperative way. But you can turn this code into a set of more readable
declarative functions, so it looks similar to any other SwiftUI view
modifier.
Let me know if this helps
Best regards,
Andrew
- Andrew Burnett-Thompson answered 2 years ago
- last edited 2 years ago
-
Thanks, I’ll study this example.
- You must login to post comments
Please login first to submit.