Simple LineMark plot (macOS), but the ydata-array has 100,000 points. Need to include most of the data points as there is fine structure. When I print the Xvalue and Yvalue to the console, there is minimal lag. But when I use Text to display Xvalue and Yvalue in the view, the lag is significant. I understand that the X- and Y-values are wrapped in an @State and cause the view to update whenever they change. Is there a way to "decouple" the Xvalue and Yvalue such that they do not cause the view to update, but still display in a view? Note that the chart does not change during DragGesture.
import SwiftUI
import Charts
struct ContentView: View {
@State private var Xvalue: Int = 0
@State private var Yvalue: Int = 0
var body: some View {
NavigationSplitView {
Text(String("X: \(Xvalue), Y: \(Yvalue)")) /* This Text bogs down with DragGesture */
}
detail: {
Chart(0..<ydata.count, id: \.self) { index in
LineMark(
x: .value("X-axis", index),
y: .value("Y-axis", ydata[index])
)
}
.chartOverlay { proxy in
GeometryReader { geometry in
Rectangle().fill(.clear).contentShape(Rectangle())
.gesture(
DragGesture()
.onChanged { value in
// Convert the gesture location to the coordiante space of the plot area.
let origin = geometry[proxy.plotAreaFrame].origin
let location = CGPoint(
x: value.location.x - origin.x,
y: value.location.y - origin.y
)
// Get the x and y values from the location.
let XYvalue = proxy.value(at: location, as: ((Int, Int).self))
Xvalue = XYvalue!.0
Yvalue = XYvalue!.1
print(Xvalue, Yvalue) /* This printout can keep up with DragGesture */
}
)
}
}
}
}
}