TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

BarMark does not line up inside chart area.

Forums > SwiftUI

Hello:

I have a chart that looks like this:

Chart {
            ForEach(measurements) { measurement in // iterate through all of the measurement data
                ForEach(measurement.values.indices, id: \.self) { index in
                    let datapoint = measurement.values[index]
                    if showData[measurement.name]![index]{
                        BarMark(x: .value(xValueLabel, datapoint.dateTime, unit: .hour), y: .value(measurement.name.description,  datapoint.value), width: 10.0)
                            .foregroundStyle(by: .value("Measurement", measurement.name.description))
                            .clipShape(RoundedRectangle(cornerRadius: 16))
                    } // end if
                } // end nested foreach
            } //end parent foreach

            if let chartSelDates { //shows the details for the selected date
                RectangleMark(
                    xStart: .value("Date",chartSelDates.lowerBound),
                    xEnd: .value("Date",chartSelDates.upperBound),
                    yStart: .value("Value", 0),
                    yEnd: .value("Value", yAxisScale.last!))
                .foregroundStyle(.gray.opacity(0.4))
                .zIndex(-1)
                .annotation(position: .top, spacing: 0, overflowResolution: .init(x: .fit(to: .chart), y: .disabled)) {
                    content
                        .fixedSize(horizontal: true, vertical: false)
                }
            } // end if
        } // end chart

This ends up producing a chart that looks like this. Notice how the last point overflows beyond the y-axis labels.

If I remove the "unit: .hour" bit from the line "BarMark(x: .value(xValueLabel, datapoint.dateTime, unit: .hour), y: .value(measurement.name.description, datapoint.value), width: 10.0)" then I get this chart

Can someone point me in the right direction as to what I should be changing to the bars stay inside of the chart space?

2      

I figured out the problem. The issue was with the ".chartXScale" modifier. I had a range that did not match up with what the data needed. I hope this helps someone else.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.