AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77181656
Accepted
GarySabo
GarySabo
Asked: 2023-09-27 00:01:53 +0800 CST2023-09-27 00:01:53 +0800 CST 2023-09-27 00:01:53 +0800 CST

Swift Charts:如何绘制具有独立于数据的固定梯度的折线图?

  • 772

与这个问题密切相关的是,线性梯度代码运行良好,但它总是与其数据相关联,因此例如下面的代码,如果您有一系列从 70 到 110 的心率,则较低的心率始终为灰色,较高的心率始终为灰色紫色,但 90 到 195 的数组也是如此。如何映射停靠点以使颜色与心率区域相对应?换句话说,心率从 70-110 只会显示蓝色到橙色?

 Chart {
            ForEach(smoothHeartRatesEMA(customHeartRates, decayFactor: 0.3)) { heartRate in
                LineMark(
                    x: .value("Sample Time", heartRate.startDate, unit: .nanosecond), //changed these to .nanosecond to fix Nike Run Club bug (some how Nike Run Club gets more frequent HR samples than other apps?)
                    y: .value("Heart Rate", heartRate.doubleValue)
                )
                .lineStyle(StrokeStyle(lineWidth: 3.0))
                .foregroundStyle(
                    .linearGradient(
                        stops: [
                            .init(color: Color.gray, location: 0.0),
                            .init(color:  TrackerConstants.AppleFitnessBlue, location: 0.16),
                            .init(color: TrackerConstants.AppleFitnessYellow, location: 0.33),
                            .init(color: TrackerConstants.AppleFitnessOrange, location: 0.5),
                            .init(color: TrackerConstants.AppleFitnessRed, location: 0.66),
                            .init(color: TrackerConstants.AppleFitnessPurple, location: 1.0) //how do I get these to allign with a range of e.g. 170-210.  I.e. if no heart rate is above 170bpm, the line is never purple?
                        ],
                        startPoint: .bottom,
                        endPoint: .top)
                         )
            }
        }
        .chartYScale(domain: 50...210)
ios
  • 1 1 个回答
  • 22 Views

1 个回答

  • Voted
  1. Best Answer
    Trent Meyer
    2023-09-27T06:15:15+08:002023-09-27T06:15:15+08:00

    通过应用线性渐变的方式,您将需要动态地将停止位置转换到不同的范围。

    您的止损设置为 0...1,预期图形数据范围为 50...210。但由于 50...210 并不是一成不变的,因此您的止损也不应该是一成不变的。

    正如您所看到的,要在图表上看到渐变的一部分,其停止点必须在 0...1 之内或之上。低于 0 或高于 1 的颜色要么根本看不到,要么只看到部分。我们需要做的是根据线标记的最大值和最小值,将您的止损点映射到大于 0...1 的范围。

    首先,我们需要将止损点从 0...1 形式中去掉,改为 50...210 表示形式。这只是简单地取(210 - 50) * stop_value(例如:blue_stop = (210 - 50) * 0.16) ~= 75)。为了简单起见,我选择对所有值进行四舍五入。

    有了这些值,我们只需将它们映射到图表的绘制范围即可。绘制的范围是minimum_y...maximum_y。下面的函数可以帮助将地图从一个范围映射到另一个范围。

    func transform<T: FloatingPoint>(_ input: T, from inputRange: ClosedRange<T>, to outputRange: ClosedRange<T>) -> T {
        // need to determine what that value would be in (to.low, to.high)
        // difference in output range / difference in input range = slope
        let slope = (outputRange.upperBound - outputRange.lowerBound) / (inputRange.upperBound - inputRange.lowerBound)
        // slope * normalized input + output lower
        let output = slope * (input - inputRange.lowerBound) + outputRange.lowerBound
        return output
    }
    

    现在我们有了要映射的值,以及映射它们的函数。当我们加载图表时,我们可以动态生成止损点。下面是生成止损的函数。

    func generateStops(minValue: Double, maxValue: Double) -> [Gradient.Stop] {
        var realStops = [Gradient.Stop]()
        // ideal stop values, if the range were to be from 50 to 210
        let idealStops: [(Double, Color)] = [
            (50, .gray),
            (75, .blue),
            (100, .yellow),
            (130, .orange),
            (155, .red),
            (210, .purple)
        ]
        for (idealValue, color) in idealStops {
            let transformedValue = transform(idealValue, from: minValue...maxValue, to: 0...1)
            realStops.append(Gradient.Stop(color: color, location: transformedValue))
        }
        return realStops
    }
    

    使用generateStops我们可以更新原始视图以获得始终准确于数据点的梯度。请注意,为了适应该示例,某些内容已从原始内容中更改。

    struct MyGraphView : View {
        @State var values: [(id: Int, x: Date, y: Double)] = []
        @State var stops: [Gradient.Stop] = []
    
        var body: some View {
            Chart {
                ForEach(values, id: \.id) { value in
                    LineMark(
                        x: .value("Sample Time", value.x, unit: .nanosecond),
                        y: .value("Heart Rate", value.y)
                    )
                    .lineStyle(StrokeStyle(lineWidth: 3.0))
                }
            }
            .chartYScale(domain: 50...210)
            .foregroundStyle(
                .linearGradient(
                    stops: stops,
                    startPoint: .bottom,
                    endPoint: .top
                )
            )
            .onAppear(perform: {
                values = exampleValues
                stops = generateStops(minValue: values.first!.y, maxValue: values.last!.y)
            })
        }
    }
    
    • 1

相关问题

  • UITableViewCell 与嵌套 UICollectionView 显示重复数据

  • 为什么IOS多行文本输入在React原生纸张对话框中无法正常工作?

  • 是否可以在不损失视频质量的情况下录制视频的播放?

  • Swift locationManager.requestWhenInUseAuthorization() 不提示用户

  • 获取不带透明区域的图像的UIBezierPath

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve