Last active
August 13, 2022 14:16
-
-
Save atrinh0/aa0b668b78562c4961056e67d45434c2 to your computer and use it in GitHub Desktop.
Swift Charts - Heat Map compilation issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} | |
} |
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
Raised
FB10250889