ios – Swift chart legend not exhibiting

Spread the love


For the swift chart under, the chart legend just isn’t exhibiting. I attempted so as to add .chartForgroundStyle() modifier for Value and Sentiment, nevertheless it causes the construct to fail for the error “Day trip 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 information
                        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("Value", entry.closePrice),
                        sequence: .worth("Value", "A")
                    )
                    .foregroundStyle(.inexperienced)
                    
                    
                    let curGradient = LinearGradient(
                        gradient: Gradient (
                            colours: [
                                .green.opacity(0.5),
                                .green.opacity(0.2),
                                .green.opacity(0.05),
                            ]
                        ),
                        startPoint: .high,
                        endPoint: .backside
                    )
                    
                    AreaMark(
                        x: .worth("Date", entry.date),
                        yStart: .worth("Value", spherical(minY - ((maxY - minY) / 100) * 20)),
                        yEnd: .worth("PriceEnd", entry.closePrice)
                    )
                    
                    .interpolationMethod(.cardinal)
                    .foregroundStyle(curGradient)
                    .alignsMarkStylesWithPlotArea()
                    
                }
                
            }
//            .chartForegroundStyleScale(["Price": .green, "Sentiment": .blue])
            .chartLegend(place: .high, alignment: .heart)
            .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 information to see how properly sentiment information predicts the worth. The structs for entries and sentiments are added above.

Leave a Reply

Your email address will not be published. Required fields are marked *