1

I'm using the new Swift Charts (https://developer.apple.com/documentation/charts) framework to create a bar chart.

Chart {

    BarMark(
        x: .value("Name", "Item 1"),
        y: .value("Count", 2)
    )

    BarMark(
        x: .value("Name", "Item 2"),
        y: .value("Count", 9)
    )

    BarMark(
        x: .value("Name", "Item 3"),
        y: .value("Count", 4)
    )

    BarMark(
        x: .value("Name", "Item 4"),
        y: .value("Count", 12)
    )

    BarMark(
        x: .value("Name", "Item 5"),
        y: .value("Count", 6)
    )

    BarMark(
        x: .value("Name", "Item 6"),
        y: .value("Count", 5)
    )

}

.chartXAxis {
    
    AxisMarks {

        AxisGridline()
        AxisTick()
        AxisLabel()

    }

}

On the x-axis I want to skip just the label for every second element. I want to keep the grid lines and ticks. Using stride for values won't work.

Has anyone an idea on how to achieve this?

Thanks.

4
  • What is the new SwiftCharts framework and please add the relevant code to your question. Commented Sep 12, 2022 at 13:00
  • Please provide enough code so others can better understand or reproduce the problem. Commented Sep 13, 2022 at 9:31
  • Hi @JoakimDanielson I have added a link to the documentary and posted a code example. Thank you. Commented Sep 14, 2022 at 7:36
  • That was what I expected but you had used a tag for a 3rd party library so it was confusing. Have you seen any of the WWDC videos related to charts, otherwise it's a good place to start developer.apple.com/videos/play/wwdc2022/10136 Commented Sep 14, 2022 at 7:49

1 Answer 1

6

I was able to fix it myself.

Chart {

    BarMark(
        x: .value("Name", "Item 1"),
        y: .value("Count", 2)
    )

    BarMark(
        x: .value("Name", "Item 2"),
        y: .value("Count", 9)
    )

    BarMark(
        x: .value("Name", "Item 3"),
        y: .value("Count", 4)
    )

    BarMark(
        x: .value("Name", "Item 4"),
        y: .value("Count", 12)
    )

    BarMark(
        x: .value("Name", "Item 5"),
        y: .value("Count", 6)
    )

    BarMark(
        x: .value("Name", "Item 6"),
        y: .value("Count", 5)
    )

}

.chartXAxis {
    
    AxisMarks { value in

        AxisGridline()
        AxisTick()
        
        if value.index % 2 == 0 {

            AxisValueLabel()

        }

    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.