NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Need some help with suppressing Chart updates

Forums > SwiftUI

Simple LineMark plot, 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 X- and Y-values to the console, there is minimal lag. But when I use Text to display X- and Y-values in the view, the lag is significant. I understand that the X- and Y-values are wrapped in a @State and cause the view to update whenver they change. Is there a way to "decouple" the X- and Y-values such that they do not cause the view to update, but still display in the first panel of the SplitView? Note that the actual 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
                        x: .value("X-axis", index),
                        y: .value("Y-axis", ydata[index])
                .chartOverlay { proxy in
                    GeometryReader { geometry in
                                    .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 value 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 */


Found an acceptable solution. Replaced DragGesture with .onTapGesture. Quick response and gives the location of the cursor on tap.


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.