This is a follow up question to my previous one here:
In the answer there, the stride for the ticks is hardcoded, based on the example data I provided:
AxisMarks(position: .bottom, values: .stride(by: 100)) {
AxisValueLabel(anchor: .top)
}
But the data is variable, and sometimes the stride needs to be for instance only 10, or any other number.
So I started with a local variable:
var xStride = 100
, which gave me the following error:
Cannot convert value of type 'Int' to expected argument type 'Calendar.Component'
Then I tried to cast the variable:
var xStride = Calendar.Component(100)
, and now the error is:
'Calendar.Component' cannot be constructed because it has no accessible initializers
What I also don't understand is, my chart is just an xy plot, and doesn't deal with days or years or whatever.
So, can I do this and how?
You could try declaring
@State private var xStride: Float = 100
and use it in
AxisMarks(position: .bottom, values: .stride(by: xStride))
There is a specific stride, that uses a BinaryFloatingPoint
. Works with Double
as well, but not Int
.
When you use .stride(by: xStride)
when xStride
is an Int
,
Swift automatically tries to use this stride version with Calendar.Component
,
since the other version does not take an Int
.
When you declare @State private var xStride: Float = 100
Swift uses the appropriate
version of stride with BinaryFloatingPoint
types. Similarly, when you use .stride(by: 100)
Swift converts the 100
to a Float or Double automatically for you.