0

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.

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
EK412
  • 71
  • 6
  • What is the new SwiftCharts framework and please add the relevant code to your question. – Joakim Danielson Sep 12 '22 at 13:00
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 13 '22 at 09:31
  • Hi @JoakimDanielson I have added a link to the documentary and posted a code example. Thank you. – EK412 Sep 14 '22 at 07: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 https://developer.apple.com/videos/play/wwdc2022/10136/ – Joakim Danielson Sep 14 '22 at 07:49

1 Answers1

5

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()

        }

    }

}
EK412
  • 71
  • 6