SwiftUI图表区域标记颜色

huangapple go评论75阅读模式
英文:

SwiftUI Charts AreaMark Colors

问题

如何将AreaMarks设置为我想要的颜色,而不是标准颜色,同时仍然正确显示图例的颜色。如果我按照下面的方式使用foregroundStyle(by:),我无法选择颜色。或者,如果我设置自己的颜色数组并使用已注释掉的行,那么我将无法在图表底部获得图例。如果保留这两行,那么我将在图表中获得正确的颜色,并获得图例,但图例使用标准颜色,而不是我的颜色。

Chart(simulationRange) {
    AreaMark(
        x: .value("Date", $0.date),
        yStart: .value("Minimum Value", $0.minValue),
        yEnd: .value("Maximum Value", $0.maxValue)
    )
    // .foregroundStyle(areaColors[$0.series]!)
    .foregroundStyle(by: .value("Quartile", $0.series))
}
英文:

How can i set the AreaMarks to the colors I want and not the standard colors, while still getting the legend to show with the correct colors. If i do it the way below usng foregroundStyle(by:), I don't get to select the colors. Alternatively, if i setup my own array of colors and use the commented out line, then I don't get the legend at the bottom of the chart. If i leave in both lines, then i get the right colors in the chart, and i get the legend, but the legend has the standard colors, not mine.

        Chart(simulationRange) {
            AreaMark(
                x: .value("Date", $0.date),
                yStart: .value("Minimum Value", $0.minValue),
                yEnd: .value("Maximum Value", $0.maxValue)
            )

// .foregroundStyle(areaColors[$0.series]!)
.foregroundStyle(by: .value("Quartile", $0.series))

答案1

得分: 0

I think you are looking for the chartForegroundStyle modifier on the Chart. You can pass a mapping from the values of your data's series property to the Colors you want.

For example:

Chart(points) { point in
    AreaMark(
        x: .value("X", point.x),
        yStart: .value("minY", point.minY),
        yEnd: .value("maxY", point.maxY)
    )
    .foregroundStyle(by: .value("Series", point.series))
}
.chartForegroundStyleScale([
    "red": Color.red, 
    "blue": Color.blue
])

with points looking like this:

@State var points = [
    DataPoint(x: 0, minY: 1, maxY: 2, series: "red"),
    DataPoint(x: 1, minY: 2, maxY: 4, series: "red"),
    DataPoint(x: 2, minY: 3, maxY: 6, series: "red"),
    DataPoint(x: 3, minY: 4, maxY: 8, series: "blue"),
    DataPoint(x: 4, minY: 5, maxY: 10, series: "blue"),
]

The output looks like:

SwiftUI图表区域标记颜色

英文:

I think you are looking for the chartForegroundStyle modifier on the Chart. You can pass a mapping from the values of your data's series property to the Colors you want.

For example:

Chart(points) { point in
    AreaMark(
        x: .value("X", point.x),
        yStart: .value("minY", point.minY),
        yEnd: .value("maxY", point.maxY)
    )
    .foregroundStyle(by: .value("Series", point.series))
}
.chartForegroundStyleScale([
    "red": Color.red, 
    "blue": Color.blue
])

with points looking like this:

@State var points = [
    DataPoint(x: 0, minY: 1, maxY: 2, series: "red"),
    DataPoint(x: 1, minY: 2, maxY: 4, series: "red"),
    DataPoint(x: 2, minY: 3, maxY: 6, series: "red"),
    DataPoint(x: 3, minY: 4, maxY: 8, series: "blue"),
    DataPoint(x: 4, minY: 5, maxY: 10, series: "blue"),
]

The output looks like:

SwiftUI图表区域标记颜色

huangapple
  • 本文由 发表于 2023年8月5日 03:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定