UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Odd annotation being displayed on a chart

Forums > SwiftUI

Hi folks. Hellp wanted :) (well, needed)

I have a chart as follows

        Chart {
            ForEach(last5Crawls) { crawlData in
                BarMark(
                    x: .value("description", Data.index),
                    y: .value("date", Data.DateLaunched),
                    width: 23
                )
                .foregroundStyle(Color.appTeal.gradient)
                .cornerRadius(10)
                .annotation(position: .trailing) {
                    Text("\(Data.index)")
                        .foregroundColor(.appTeal)
                        .font(.system(size: 10, weight: .semibold))
                        .foregroundColor(.appGray)
                }
            }
        }

Normally there are 5 elements in the X and Y values. When I have, for example, 1 element with data, I get an annotation displayed which just contains the number 0.

Heres an example: https://imgur.com/a/WsTI7BZ

How can I get rid of that "0" that's displayed.

Any advice greatly appriciated!

1      

Filter the data to only show data greater then 0 then insert that in the the Chart

2      

@NigelGee. Yes thank you. This is the solution.

            ForEach(last5Tests.filter { $0.hasPurchases > 0 }) { testData in
                BarMark(
                    x: .value("Purchased", testData.hasPurchases),
                    y: .value("Test", testData.testDateLaunched),
                    width: 25
                )

The line

            ForEach(last5Tests.filter { $0.hasPurchases > 0 }) { testData in

Only sends non zero data to the chart.

1      

Hello there!

It looks like you're dealing with an issue where an unwanted "0" is displayed in the annotation when you have only one element of data. To resolve this, you can conditionally display the annotation only when there is more than one element. Here's a modified version of your code to achieve this:

swift Copy code Chart { ForEach(last5Crawls) { crawlData in BarMark( x: .value("description", Data.index), y: .value("date", Data.DateLaunched), width: 23 ) .foregroundStyle(Color.appTeal.gradient) .cornerRadius(10) .overlay( Group { if last5Crawls.count > 1 { BarAnnotation(dataIndex: Data.index) } } ) } } In this modified code, I added an overlay with a Group that conditionally includes the BarAnnotation only when there is more than one element in last5Crawls. This should prevent the unwanted "0" from being displayed when you have a single element.

Make sure to replace BarAnnotation(dataIndex: Data.index) with the appropriate annotation code you have in your project.

I hope this helps! If you have any further questions or issues, feel free to ask.

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.