“Swift Chart” 中的 “Extra Line”

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

Extra Line in Swift Chart

问题

我正在尝试让新的Swift Charts正常工作,但图表中有一条从第一个点到最后一个点的奇怪线,我无法解决。

以下是代码:


var ChartView: some View {
        HStack   {
            //Anx_6
            Chart(genRtrv.TUHist_Chart) { series in
                
                ForEach(genRtrv.TUHist_Chart.reversed(), id: \.Timestamp) { data in
                    let vdate = data.Timestamp
                    let minus_3m = Calendar.current.date(byAdding: .month, value: -3, to:     Date())!
                    let minus_6m = Calendar.current.date(byAdding: .month, value: -6, to: Date())!
                    
                    if vdate > minus_3m {
                        LineMark(
                            x: .value("Date", data.Timestamp, unit: .day),
                            y: .value("Thumbs Up", data.TU)
                        )
                        PointMark(
                            x: .value("Date", data.Timestamp, unit: .day),
                            y: .value("Thumbs Up", data.TU)
                        )
                    }
                }
            }
        }
    }

数据:
开始TuChart的枚举

绘制值: 5 - 时间: 2023-01-11 13:32:55 +0000
绘制值: 5 - 时间: 2023-01-11 22:20:37 +0000
绘制值: 4 - 时间: 2023-01-26 22:17:50 +0000
绘制值: 3 - 时间: 2023-02-04 22:20:36 +0000
绘制值: 4 - 时间: 2023-02-10 22:19:16 +0000
绘制值: 3 - 时间: 2023-02-21 22:19:19 +0000
绘制值: 4 - 时间: 2023-03-04 22:18:27 +0000
绘制值: 5 - 时间: 2023-03-11 03:50:51 +0000
绘制值: 4 - 时间: 2023-03-17 15:51:19 +0000
绘制值: 3 - 时间: 2023-03-23 13:54:39 +0000
绘制值: 3 - 时间: 2023-04-11 12:32:55 +0000

结果如下

(https://i.stack.imgur.com/YaAxK.png)

我在苹果的文档和Stack Overflow上查看了,我只希望图表不要有递归线跑过它。

英文:

I am trying to get the new Swift Charts to work, but there is a funny line running from the first to last point in the chart which I cannot resolve.

Here is the code:


var ChartView: some View {
        HStack   {
            //Anx_6
            Chart(genRtrv.TUHist_Chart) { series in
                
                ForEach(genRtrv.TUHist_Chart.reversed(), id: \.Timestamp) { data in
                    let vdate = data.Timestamp
                    let minus_3m = Calendar.current.date(byAdding: .month, value: -3, to:     Date())!
                    let minus_6m = Calendar.current.date(byAdding: .month, value: -6, to: Date())!
                    
                    if vdate > minus_3m {
                        LineMark(
                            x: .value("Date", data.Timestamp, unit: .day),
                            y: .value("Thumbs Up", data.TU)
                        )
                        PointMark(
                            x: .value("Date", data.Timestamp, unit: .day),
                            y: .value("Thumbs Up", data.TU)
                        )
                    }
                }
            }
        }
    }

Data:
Start Enum of TuChart

Charted Val: 5 - Time: 2023-01-11 13:32:55 +0000
Charted Val: 5 - Time: 2023-01-11 22:20:37 +0000
Charted Val: 4 - Time: 2023-01-26 22:17:50 +0000
Charted Val: 3 - Time: 2023-02-04 22:20:36 +0000
Charted Val: 4 - Time: 2023-02-10 22:19:16 +0000
Charted Val: 3 - Time: 2023-02-21 22:19:19 +0000
Charted Val: 4 - Time: 2023-03-04 22:18:27 +0000
Charted Val: 5 - Time: 2023-03-11 03:50:51 +0000
Charted Val: 4 - Time: 2023-03-17 15:51:19 +0000
Charted Val: 3 - Time: 2023-03-23 13:54:39 +0000
Charted Val: 3 - Time: 2023-04-11 12:32:55 +0000

Result are below

(https://i.stack.imgur.com/YaAxK.png)

I looked on line at Apple's documentation and stackoverflow. I would just like the chart to not have the recursive line running across it.

答案1

得分: 0

问题在于你传递数据并两次循环遍历它,首先使用以数据作为参数的Chart初始化程序

Chart(genRtrv.TUHist_Chart) { series in
    // 代码的其余部分
}

其次,当使用ForEach循环时

ForEach(genRtrv.TUHist_Chart.reversed(), id: \.Timestamp) { data in
    // ...
}

这里最直接的解决方案是通过改变Chart的初始化程序来代替

Chart {
    // 代码的其余部分
}
英文:

The problem is that you are passing in the data and looping over it two times, first by using a Chart initialiser that takes the data as an argument

Chart(genRtrv.TUHist_Chart) { series in
    // rest of code
}

and secondly when using a ForEach loop

ForEach(genRtrv.TUHist_Chart.reversed(), id: \.Timestamp) { data in
    // ...
}

The most straightforward solution here is to change the Chart initialiser by instead doing

Chart {
    // rest of code
}

huangapple
  • 本文由 发表于 2023年4月10日 23:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978236.html
匿名

发表评论

匿名网友

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

确定