在 SwiftUI 图表中沿着弯曲的图表线绘制渐变?

问题描述 投票:0回答:1

如何在 SwiftUI Chart 折线图中沿着图表线绘制渐变?

我确实找到了如何实现这样的渐变......

Chart {
                ForEach(viewModel.items) { shape in
                    LineMark(
                        x: .value("Shape Type", shape.createDay, unit: .day),
                        y: .value("Total Count", shape.value)
                    )
                }
                .interpolationMethod(.cardinal(tension: 3))
                .lineStyle(StrokeStyle(lineWidth: 6, lineCap: .round))
                .foregroundStyle(
                        .linearGradient(
                            colors: [.accentColor, .clear],
                            startPoint: .leading,
                            endPoint: .trailing
                        )
                    )
            }

linear gradient from left to right

但我想要的是让渐变遵循线条本身,而不仅仅是从左到右。这可能吗?

ios swift swiftui charts swiftui-charts
1个回答
-1
投票

使用

.foregroundStyle(by: .value("Color", shape.createDay))

LineMark
该框架为您生成渐变。

© www.soinside.com 2019 - 2024. All rights reserved.