5

I want that my y-axis always shows a range from 0-60 even though there are data sets that do not represent the full range. E.g. if I'm showing a data set with values of just 10 and 40 the y-axis range is only from 0-40 instead of 0-60.

Any ideas how to configure the y-axis for Swift Charts (iOS 16) with a fixed range?

Chart { 
        ForEach(model.series.entries, id: \.weekday) { element in
            BarMark(
                x: .value("Day", element.weekday, unit: .day),
                y: .value("Feeling", element.value)
            )
        }
    }
    .chartXAxis {
        let unit: Calendar.Component = model.resolution == .yearly ? .month : .day
        
        AxisMarks(values: .stride(by: unit, count: 1)) { _ in
            AxisGridLine()
            AxisValueLabel(format: .dateTime.weekday(.narrow), centered: true)
        }
    }

Content with values of 40 and 50

Content with values of 10 and 40

gpichler
  • 2,181
  • 2
  • 27
  • 52
  • 1
    add this to your Chart, `.chartYScale(domain: 0...60) ` – workingdog support Ukraine Sep 27 '22 at 02:32
  • @workingdogsupportUkraine That's exactly what I need for my y-axis scale. However, I'm facing a different problem now. Please see my edited answer. – gpichler Sep 27 '22 at 07:31
  • There are too many missing parts to test your code (and any answers). Show a minimal example code: https://stackoverflow.com/help/minimal-reproducible-example. – workingdog support Ukraine Sep 27 '22 at 07:35
  • @workingdogsupportUkraine sry my bad. everything works fine, I made a typo and let the domain start with 10 instead of 0. Do you want to post this as an answer so I can mark it as solved? – gpichler Sep 27 '22 at 18:05

1 Answers1

25

try adding this to your Chart, ...to configure the y-axis for Swift Charts (iOS 16) with a fixed range:

.chartYScale(domain: 0...60)