LAST CHANCE: Save 50% on all my Swift books and bundles! >>

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 Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.