admin管理员组

文章数量:1267436

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?

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?

Share Improve this question asked Feb 26 at 16:59 koenkoen 5,7417 gold badges58 silver badges101 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: swiftuiDynamic stride in AxisMarksStack Overflow