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 badges1 Answer
Reset to default 1You 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
版权声明:本文标题:swiftui - Dynamic stride in AxisMarks - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741105064a2340430.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论