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

SOLVED: Configuring the Y axis in Swiftui charts

Forums > SwiftUI

Hi folks,

I'm working on a horizonal bar chart (see attached image) as each bar has an associated date and as you can see the date is obscured by the bar. I'd like to fix that and insert some space between the date and the bar. I could reduce the width of the bar but that does not give me a pleasing result. I like the more chunky bars.

How can I fix this?

Example here: https://imgur.com/a/b3NYL0g

Here's the code I have so far.

        Chart{
            ForEach(last5Crawls) { crawlData in
                BarMark(
                    x: .value("Avg. Discovered by Google", crawlData.discovered),
                    y: .value("Crawl", crawlData.crawlDateLaunched),
                    width: 15
                )
                .foregroundStyle(Color.appBrand.gradient)
                .cornerRadius(10)
                .annotation(position: .trailing) {
                    Text("\(crawlData.discovered)")
                        .font(.system(size: 10, weight: .semibold))
                        .foregroundColor(.gray)
                }
            }
            .foregroundStyle(.gray)
        }
        .listRowSeparator(.hidden)
        .chartForegroundStyleScale([
            "Discovered": Color.appBrand
        ])
        .chartLegend(position: .top, alignment: .bottomLeading)
        .chartXAxis(.hidden)

2      

Not sure if this exactly the reason, but it seems your view is just squeezed due to the limited size available, namely height. Try to add minHeight frame to Chart so that it has enough space to display everthing properly. I just tried to limit the space in one of my projects with chart and the result is the same everthing is squeezed.

3      

Thanks very much @ygeras. That did the trick! Much obliged. :)

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.