English 中文(简体)
在BarMark使用快速图,我如何能够将轴心上单一日期的颜色改变为蓝色?
原标题:Using Swift Charts, in a BarMark, how can I change the color of single date in the x axis to blue?

Using Swift Charts, I have a bar chart that has dates on the x-axis and points on the y-axis. This data comes from the property chartData which contains the integer points for each date. The data point representing today s date and points could be anywhere in the chartData.

因此,我要将轴心标签的颜色改为今天的蓝色。 所有其他日期都应保留在其缺席时。 这使用户能够直观地显示今天的日期。

I know how to change the color of all x-axis labels, but this is not what I want. I want to change the color of the one x-axis label that matches todays date. Thank you!

struct DailyPointsChart: View {
    @Binding var scrollPosition: Date
    let chartData: [(Date, Int)]
    
    var body: some View {
        Chart {
            ForEach(chartData, id: .0) { data in
                BarMark(
                    x: .value("Day", data.0, unit: .day),
                    y: .value("Points", data.1)
                )
            }
            .cornerRadius(10)
            .foregroundStyle(Color(UIColor.purple))

        }
        .chartScrollableAxes(.horizontal)
        .chartXVisibleDomain(length: 3600 * 24 * 7) // 7 visible days on chart
        .chartScrollPosition(x: $scrollPosition)
        
        .chartXAxis {
            AxisMarks(values: .stride(by: .day, count: 1)) {
                AxisTick()
                AxisValueLabel(format: .dateTime.month().day())
                    .foregroundStyle(Color.blue) // This changes the color of all x-axis labels, which is not what we want
            }
        }
    }
}


问题回答

Try this approach where you specify a particular time value for the AxisMarks, as shown in the example code (adjust for your data type)

 .chartXAxis {
     // all values, black
     AxisMarks(values: .stride(by: .day, count: 1)) {
         AxisTick()
         AxisValueLabel(format: .dateTime.month().day())
             .foregroundStyle(Color.black)
     }
     // just the specified value, blue
     AxisMarks(values: [chartData[2].0]) { // <-- here, a specific time value
         AxisTick()
         AxisValueLabel(format: .dateTime.month().day())
             .foregroundStyle(Color.blue)
     }
 }




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签