Skip to content

Instantly share code, notes, and snippets.

@atrinh0
Last active August 13, 2022 14:16
Show Gist options
  • Save atrinh0/aa0b668b78562c4961056e67d45434c2 to your computer and use it in GitHub Desktop.
Save atrinh0/aa0b668b78562c4961056e67d45434c2 to your computer and use it in GitHub Desktop.
Swift Charts - Heat Map compilation issue
import SwiftUI
import Charts
struct ContentView: View {
@State private var grid = Grid(numRows: 10, numCols: 10)
var body: some View {
if #available(iOS 16.0, *) {
VStack {
Spacer()
Chart(grid.points) { point in
RectangleMark(
xStart: PlottableValue.value("xStart", point.x),
xEnd: PlottableValue.value("xEnd", point.x + 1),
yStart: PlottableValue.value("yStart", point.y),
yEnd: PlottableValue.value("yEnd", point.y + 1)
)
.foregroundStyle(point.color)
// Uncommenting annotations causes code to not compile,
// unless either chartYAxis or chartXAxis is commented
// .annotation(position: .overlay) {
// Text(String(format: "%.0f", point.val))
// }
}
.chartYAxis {
AxisMarks(values: .automatic(desiredCount: grid.numRows, roundLowerBound: false, roundUpperBound: false)) { _ in
AxisGridLine()
AxisTick()
AxisValueLabel(centered: true)
}
}
.chartXAxis {
AxisMarks(values: .automatic(desiredCount: grid.numCols, roundLowerBound: false, roundUpperBound: false)) { _ in
AxisGridLine()
AxisTick()
AxisValueLabel(centered: true)
}
}
.aspectRatio(contentMode: .fit)
Spacer()
}
} else {
EmptyView()
}
}
}
struct Grid {
let numRows: Int
let numCols: Int
var points = [Point]()
init(numRows: Int, numCols: Int) {
self.numRows = numRows
self.numCols = numCols
generateData()
}
mutating func generateData() {
for rowIndex in 0..<numRows {
for colIndex in 0..<numCols {
let maxValue = numRows + numCols - 2
let variance = Double.random(in: 0..<20) - 10
let value = (Double(rowIndex + colIndex) * 100)/Double(maxValue) + variance
let point = Point(x: colIndex, y: rowIndex, val: value)
points.append(point)
}
}
}
struct Point: Hashable, Identifiable {
let id = UUID()
let x: Int
let y: Int
let val: Double
var color: Color {
switch val {
case _ where val < 20.0:
return .blue
case 20..<40:
return .green
case 40..<60:
return .yellow
case 60..<80:
return .orange
case _ where val >= 80:
return .red
default:
return .red
}
}
}
}
@atrinh0
Copy link
Author

atrinh0 commented Jun 14, 2022

Uncommenting the annotation above causes Xcode to fail compiling.

Xcode Version 14.0 beta (14A5228q)
macOS Monterey, Version 12.4
Mac mini (M1, 2020)

Screenshot 2022-06-14 at 17 28 43

annotation chartYAxis chartXAxis Output
Code does not compile (See above image)
Commented out
Commented out
Commented out

@atrinh0
Copy link
Author

atrinh0 commented Jun 14, 2022

Raised FB10250889

@atrinh0
Copy link
Author

atrinh0 commented Aug 13, 2022

Issue still exists in Xcode 14 beta 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment