I thought I'd pick this up and test it with the new iOS 18 PointPlots.
I setup this code to switch between a ForEach on the PointMarks and a Point Plot.
import SwiftUI
import Charts
@available(iOS 18.0, *)
struct ManyPointsChart: View {
// Reduced data point count to 10,000 as 100,000 still V. poor performance
let dataPoints: [DataPoint] = (0..<10_000).map { DataPoint(xValue: $0, yValue: Double($0)) }
@State var selectedIndex:Int?
var body: some View {
Chart {
if let selectedIndex {
RuleMark(x: .value("selected", selectedIndex))
}
// Original Method
// ForEach(dataPoints) { dataPoint in
// PointMark(x: .value("Index", dataPoint.xValue),
// y: .value("Y Value", dataPoint.yValue))
// }
//New in iOS 18
PointPlot(
dataPoints,
x: .value("Longitude", \.xValue),
y: .value("Capacity density", \.yValue)
)
}
.chartXSelection(value: $selectedIndex)
}
}
@available(iOS 18.0, *)
#Preview {
ManyPointsChart()
}
struct DataPoint: Identifiable {
var id: Double { Double(xValue) }
let xValue: Int
let yValue: Double
}
Despite reducing the data points count to 10,000 I still found Selection performance very slow in Preview. (M3 Max MBP)
Performance was improved with the PointPlot vs the PointMarks, but not quite as much as I hoped.
I think I'm using the APIs correctly but would welcome any feedback if not!
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: