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?