13 C
Saturday, October 21, 2023

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]

Chart {
                ForEach(self.sentiments, id: .date) { sentiment in
                        x: .worth("Date", sentiment.date),
                        // TODO: take away -1 for actual sentiment knowledge
                        y: .worth("Sentiment", sentiment.closePrice - 1),
                        sequence: .worth("Sentiment", "B")
                ForEach(self.entries, id: .date) { entry in
                        x: .worth("Date", entry.date),
                        y: .worth("Worth", entry.closePrice),
                        sequence: .worth("Worth", "A")
                    let curGradient = LinearGradient(
                        gradient: Gradient (
                            colours: [
                        startPoint: .prime,
                        endPoint: .backside
                        x: .worth("Date", entry.date),
                        yStart: .worth("Worth", spherical(minY - ((maxY - minY) / 100) * 20)),
                        yEnd: .worth("PriceEnd", entry.closePrice)
//            .chartForegroundStyleScale(["Price": .green, "Sentiment": .blue])
            .chartLegend(place: .prime, alignment: .middle)
            .chartYScale(area: minY - ((maxY - minY) / 100) * 20...maxY + ((maxY - minY) / 100) * 20)
                AxisMarks(values: .stride(by: .day))
                { date in
                    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
Related news


Please enter your comment!
Please enter your name here