0

I have a view where I display data inside a chart with the Charts package in SwiftUI. When I only use LineMark to display my data everything works fine. But when I want to add a PointMark to this chart I get the error, that Xcode cannot compile that view:

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions.

This is my code:

struct VoltageDataVView: View {
    
    @Binding var yValues: [Double]
    @Binding var voltageValues: [[[Double]]]
    @Binding var rowIndex: Double
    @Binding var rowPoint: Double
    
    var body: some View {
        Chart {
            ForEach(0..<self.voltageValues.count, id:\.self) { i in
                if self.voltageValues[i].count > 0 {
                    ForEach(0..<self.voltageValues[i][0].count, id:\.self) { j in
                        LineMark(
                            x: .value("x1", self.yValues[j]),
                            y: .value("y1", self.voltageValues[i][Int(self.rowIndex)][j])
                        )
                    }
                    .foregroundStyle(by: .value("value1", i))
                }
            }
            PointMark(x: .value("x-point-row", self.xValues[Int(self.rowPoint)]),
                      y: .value("y-point-row", self.voltageValues[0][Int(self.rowIndex)][Int(self.rowPoint)]))
        }
        .chartXScale(domain: self.yValues.min()!...self.yValues.max()!)
        .chartXAxisLabel("number of datapoints")
        .chartYAxisLabel("y values")
        .frame(width: 400, height: 300)
        .rotationEffect(.degrees(-90))
        .offset(y: 50)
    }
}

I have no idea what I can simplify or breakdown there. What can I do to avoid the error?

koen
  • 5,383
  • 7
  • 50
  • 89
BeneKunz
  • 43
  • 6
  • 3
    I would start with better modeling voltageValues. An [[[Double]]] is a really nasty thing to have to deal with. – Yrb Apr 30 '23 at 17:19
  • 1
    `ranges` are considered unsafe when dealing with swiftui, you can watch Demystify SwiftUI. You should simplify your view and Provide SwiftUI data that has already been filtered and modified for display – lorem ipsum Apr 30 '23 at 20:12

1 Answers1

-1

The main issue you are facing is on this line

PointMark(x: .value("x-point-row", self.xValues[Int(self.rowPoint)])

This line is referencing xValues which is not defined anywhere. This could potentially lead to the error you are facing.

Additionally, you have to make sure that yValues count is equal to each count in the voltageValues, and the reason is your loop.

ForEach(0..<self.voltageValues[i][0].count, id:\.self) { j in

if for example self.voltageValues[i][0].count = 3 while yValues is equal to 2 then the app would crash in runtime.

Note: The fact you are using [[[Double]]] is really bad practice to say the least. I would recommend what @Yrb recommended and that is try to model your data better.

Muhand Jumah
  • 1,726
  • 1
  • 11
  • 26