0

I wanted to create a heatmap with the chart framework in my swiftui app for the Mac. I used a Chart with the RectangleMark for that and displayed my grid data which has 111x71 datapoints. But I have noticed that interacting with the whole view and other charts is very laggy now. The code below shows my implementation.

struct ChartDataView: View {
    @Binding var yValues: [Double]
    @Binding var xValues: [Double]
    @Binding var voltageValues: [[[Double]]]
    @Binding var rowIndex: Double
    @Binding var colIndex: Double
    @Binding var colLength: Double
    
    var body: some View {
        HStack {
            if self.voltageValues[0].count > 0 {
            VStack {
                HStack {
                    Chart {
                        ForEach(0..<self.xValues.count, id:\.self) { x in
                            ForEach(0..<self.yValues.count, id:\.self) { y in
                                RectangleMark(x: .value("x", self.xValues[x]), y: .value("y", self.yValues[y]))
                                    .foregroundStyle(by: .value("voltage", self.voltageValues[0][x][y]))
                            }
                        }
                    }
                    .id(UUID())
                    .chartXScale(domain: self.xValues.min()!...self.xValues.max()!)
                    .chartYScale(domain: self.yValues.min()!...self.yValues.max()!)
                    .chartXAxisLabel("number of datapoints")
                    .chartYAxisLabel("y values")
                    .frame(maxWidth: 500, maxHeight: 500)
                }
                .id(UUID())
                }
            }
        }
    }
}

What can cause the problem for the laggy behavior? Is there any way to update the view smoothly?

BeneKunz
  • 43
  • 6
  • Why are you doing this `.id(UUID())`? That will guarantee that the entire hierarchy is recalculated/re-rendered every time -- this goes against how SwiftUI is supposed to work. – jnpdx Apr 19 '23 at 19:27
  • @jnpdx I saw a fix online on slow updating list where they used the .id(UUID()) to speed up the list update. That's why I used it in my view aswell. – BeneKunz Apr 19 '23 at 19:31
  • 1
    If anything, it would likely do the opposite – jnpdx Apr 19 '23 at 19:33

0 Answers0