英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论