1

In this chart I'd like to have control over the number of xAxis labels as well as the placement, however I want the framework to control what is actually in the label, i.e. I don't want custom xAxis labels.

See my code below and the resulting chart.

I would instead like (1) the first date to be on the far left and (2) the last date to not be cut off?

                        Chart {
                            ForEach(unwrappedVo2Max) { vo2MaxSample in
                                AreaMark(
                                    x: .value("Date", vo2MaxSample.startDate),
                                    y: .value("Cardio Fitness", vo2MaxSample.quantity.doubleValue(for: HKUnit(from: "ml/kg*min")))
                                )
                                //.interpolationMethod(.cardinal)
                                .opacity(0.6)
                                .alignsMarkStylesWithPlotArea()
                                .foregroundStyle( .linearGradient(
                                    colors: \[Color.clear, TrackerConstants.AppleFitnessPurple\],
                                    startPoint: .bottom, endPoint: .top
                                ))
                                LineMark(
                                    x: .value("Date",  vo2MaxSample.startDate),
                                    y: .value("Cardio Fitness", vo2MaxSample.quantity.doubleValue(for: HKUnit(from: "ml/kg*min")))
                                )
                                .lineStyle(StrokeStyle(lineWidth: 2))
                                .foregroundStyle(TrackerConstants.AppleFitnessPurple)
                            }
                        }
                        .frame(height: TrackerConstants.chartHeight)
                        .padding(.horizontal)
                        .chartYScale(domain: SwiftChartHelpers.getYScaleMin(values: unwrappedVo2Max.map { $0.quantity.doubleValue(for: HKUnit(from: "ml/kg*min")) }, padding: 10)...SwiftChartHelpers.getYScaleMax(values: unwrappedVo2Max.map { $0.quantity.doubleValue(for: HKUnit(from: "ml/kg*min")) }, padding: 10.0))
                        .chartYAxis {
                            AxisMarks(position: .leading)
                        }

enter image description here

GarySabo
  • 5,806
  • 5
  • 49
  • 124
  • You're already doing something similar with the Y scale, you need to do the same for x - calculate a range of values and pass it to `.chartXScale(domain:...` – jrturton Feb 17 '23 at 15:43
  • @jrturton thanks but that's what I meant in my question I don't want to take over complete control of the xAxis labels, Apple is better than me at choosing which dates to use but I'd like to have some input over the placement of the labels and/or the formatting of the dates. – GarySabo Feb 22 '23 at 14:32

0 Answers0