英文:
How do I change my x labels angle with swiftUI?
问题
我是SwiftUI初学者
我想要改变我的数值在x轴上的旋转,就像这张图片中显示的那样
期望图表
我目前的代码是这样的
Chart{
    ForEach(arregloDummy) { arregloDummy in
        LineMark(
            x: .value("Periodo", arregloDummy.periodo.orEmpty),
            y: .value("Consumo", arregloDummy.consumo ?? 0.0))
    }
}.frame(height: 200)
 .padding()
 .foregroundColor(Color(cgColor: ColorSet.accentColor.cgColor))
 .background(Color(red: 213 / 255, green: 218 / 255, blue: 198 / 255))
 .chartYAxis{
     AxisMarks(position: .leading)
 }
这是它的当前显示
当前图表
我尝试使用.rotationEffect,但它旋转了我的整个图表。
英文:
Im beginner in swiftUI
I would like to change the rotation of my values in the x-axis as shown in this image
Desire chart
I currently have it like this in my code
Chart{
                    ForEach(arregloDummy) { arregloDummy in
                        LineMark(
                            x: .value("Periodo", arregloDummy.periodo.orEmpty),
                            y: .value("Consumo", arregloDummy.consumo ?? 0.0))
                    }
                }.frame(height: 200)
                    .padding()
                    .foregroundColor(Color(cgColor: ColorSet.accentColor.cgColor))
                    .background(Color(red: 213 / 255, green: 218 / 255, blue: 198 / 255))
                    .chartYAxis{
                        AxisMarks(position: .leading)
                    }
an this is how it appears:
Current chart
I tried to use .rotationEffect, but it rotates my entire chart.
答案1
得分: 0
我认为你可以使用 .chartXAxis 来自定义标签。如果值是 Int 类型,那么可能会像这样:
    Chart {
        // 之前的内容
    }
    .chartXAxis {
        AxisMarks(position: .bottom, values: .automatic) { value in
            AxisValueLabel() {
                if let intValue = value.as(Int.self) {
                    Text("\(intValue)")
                        .rotationEffect(.degrees(-35))
                }
            }
        }
    }
英文:
I think you can use .chartXAxis to customize the labels. If the values are Int then it might look something like:
    Chart {
        // Content as before
    }
    .chartXAxis {
        AxisMarks(position: .bottom, values: .automatic) { value in
            AxisValueLabel() {
                if let intValue = value.as(Int.self) {
                    Text("\(intValue)")
                        .rotationEffect(.degrees(-35))
                }
            }
        }
    }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论