14 C
London
Monday, September 9, 2024

ios – Swift chart legend not displaying


For the swift chart under, the chart legend is just not displaying. I attempted so as to add .chartForgroundStyle() modifier for Worth and Sentiment, but it surely causes the construct to fail for the error “Day out for a thunk to construct”. How do I repair this and add a legend for the 2 Line Marks?

struct Entry {
    let date: Date
    let closePrice: Float
}
... View
    @State var entries: [Entry]
    @State var sentiments: [Entry]

...physique
Chart {
                ForEach(self.sentiments, id: .date) { sentiment in
                    LineMark(
                        x: .worth("Date", sentiment.date),
                        // TODO: take away -1 for actual sentiment knowledge
                        y: .worth("Sentiment", sentiment.closePrice - 1),
                        sequence: .worth("Sentiment", "B")
                    )
                    .foregroundStyle(.blue)
                    
                }
                ForEach(self.entries, id: .date) { entry in
                    LineMark(
                        x: .worth("Date", entry.date),
                        y: .worth("Worth", entry.closePrice),
                        sequence: .worth("Worth", "A")
                    )
                    .foregroundStyle(.inexperienced)
                    
                    
                    let curGradient = LinearGradient(
                        gradient: Gradient (
                            colours: [
                                .green.opacity(0.5),
                                .green.opacity(0.2),
                                .green.opacity(0.05),
                            ]
                        ),
                        startPoint: .prime,
                        endPoint: .backside
                    )
                    
                    AreaMark(
                        x: .worth("Date", entry.date),
                        yStart: .worth("Worth", spherical(minY - ((maxY - minY) / 100) * 20)),
                        yEnd: .worth("PriceEnd", entry.closePrice)
                    )
                    
                    .interpolationMethod(.cardinal)
                    .foregroundStyle(curGradient)
                    .alignsMarkStylesWithPlotArea()
                    
                }
                
            }
//            .chartForegroundStyleScale(["Price": .green, "Sentiment": .blue])
            .chartLegend(place: .prime, alignment: .middle)
            .chartYScale(area: minY - ((maxY - minY) / 100) * 20...maxY + ((maxY - minY) / 100) * 20)
            .chartXAxis
            {
                AxisMarks(values: .stride(by: .day))
                { date in
                    AxisGridLine()
                    AxisValueLabel(format: .dateTime.weekday(.slim), centered: true)
                }
            }.chartYAxis {
                AxisMarks(place: .main)
            }
            

Edit: For context, the chart plots inventory value and sentiment knowledge to see how effectively sentiment knowledge predicts the worth. The structs for entries and sentiments are added above.

Latest news

Relying on You

Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here