Swift Chart LineMark create chart each time different

Hi There,

Same data shows different line drawing. What I am missing here ? Each time I click to see chart, its drawing is different.

Chart{
                ForEach(data)
                { series in
                    ForEach(series.data){ entry in
                        LineMark(
                            x: .value("Day", entry.day),
                            y: .value("Amount", entry.amount)
                        ) .foregroundStyle(by: .value("Doc", series.Doc))
                            .symbol(by: .value("Doc", series.Doc))
                        PointMark(
                            x: .value("Day",  entry.day),
                            y: .value("Sales", entry.amount)
                        )
                        .foregroundStyle(.purple)
                        .symbolSize(symbolSize)
                    }
                    .lineStyle(StrokeStyle(lineWidth: lineWidth))
                }
            }
            .chartLegend(position: .top)
            .chartYScale(domain: .automatic(includesZero: false))
            .chartXAxis {
                AxisMarks(values: .automatic(roundLowerBound: false, roundUpperBound: false)) { value in
                    if value.index < value.count - 1 {
                        AxisValueLabel()
                    }
                    AxisTick()
                    AxisGridLine()
                }
            }

Replies

Did you make sure that the data (series.data) is sorted by date?

Please make sure the data are sorted.

You need to use .sorted(by: { } )

I encounted this problem before, you can see my code for understading.

Chart {
                            ForEach(bodydata.sorted(by: { $0.key.toDate2()! > $1.key.toDate2()! }), id:\.key) { key, value in
                                
                                LineMark(
                                    x: .value("Week Day", key.toDate2()!, unit: .day),
                                    y: .value("HR", value.amount)
                                )
                                .interpolationMethod(.catmullRom)
                                .symbol() {
                                    Rectangle()
                                        .frame(width: 8, height: 8)
                                }
                                .symbolSize(30)            

                            }
                        }