Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Swift: Make Int Enum conform to Plottable protocol in order to show a legend using Swift Charts

Ask Question

I am trying to make my Int enum conform to Plottable in order to use a legend in my Swift Charts. I'm not sure if I've understood the way on how to implement it correctly. Here is how my enum:

enum Type: Int, CaseIterable, Identifiable, Plottable {
      init?(primitivePlottable: Int) {
           self.init(rawValue: primitivePlottable)
      var primitivePlottable: Int {
           return rawValue
      var id: Int {
           return rawValue
      case first = 32
      case second = 434

My chart looks like this:

 Chart {
      if let allObejcts = object.objects(type: Type.first) {
           ForEach(allObejcts) { object in
                BarMark(
                     x: .value("#", object.indexInSegment),
                     y: .value("Distance", object.distance)
                .foregroundStyle(by: .value("type", object.type))
.chartLegend(position: .bottom, spacing: 8)
.chartLegend(SwiftUI.Visibility.visible)
.chartForegroundStyleScale([
                    Type.first: Color.pink,
                    Type.second: Color.yellow
.frame(minHeight: 200)
Despite compiling and running with no errors, the legends isn't visible. 🤷‍♂️

As documentation states for enum it should be String as raw type

so making next works (tested with Xcode 14b5 / iOS 16)

enum Type: String, CaseIterable, Identifiable, Plottable {
    init?(primitivePlottable: String) {
        self.init(rawValue: primitivePlottable)
    var primitivePlottable: String {
        return rawValue
    var id: String {
        return rawValue
    case first = "32"
    case second = "434"
                Thanks, so using String is the only way to use the enum as categorical data? If I cannot use Int, I have to change the database scheme and convert all stored values and it would also take more disk space to use String instead of Int.
– Daniel
                Aug 19, 2022 at 12:25
                Don't think it is required for data-base, you can convert it in init, from Int to String, and back where needed, but run-time enum should be String.
– Asperi
                Aug 19, 2022 at 12:28
                Thanks, I took a shortcut for now and made my own LegendView: struct ChartLegendDescriptionView: View {     var color: Color     var description: String     var body: some View {         HStack {             Circle().fill()                 .foregroundColor(color)                 .frame(maxHeight: 8)             Text(description)                 .font(.footnote)                 .foregroundColor(.secondary)         }     } }
– Daniel
                Aug 19, 2022 at 16:42
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.