Post

Replies

Boosts

Views

Activity

Reply to How can I include a function (or calculation) in the values that Charts receives to draw the chart, and overcome this bug?
Ok, I've figured out what the problem is. I've passed all the calculations back to the viewmodel, including the max, min and average. The value has been recalculated. The problem is in the scale of the Y axis, and it keeps happening (if I remove it the problem disappears). Everything works the first time, if I change the value from fahrenheit to celsius the error reappears (all the fields are recalculated perfectly), however, if I run the application again the information works correctly. The bad thing is that Charts does not calculate automatically and with these values the scales look very bad. ** I changed everything from AppStorage to UserDefaults ** Chart Chart { if statsByUser.takens > 1 { RuleMark(y: .value("AVG", statsByUser.avg)) .lineStyle(StrokeStyle(lineWidth: 2, lineCap: .round, lineJoin: .round, dash: [4, 6])) .foregroundStyle(.white.opacity(0.8)) } ForEach(0..<temperatureByUserId.count, id:\.self) { item in LineMark(x: .value("Date", item + 1), y: .value("Temperature", temperatureByUserId[item].convertedDegrees)) .lineStyle(.init(lineWidth: 2, lineCap: .round, lineJoin: .round)) .foregroundStyle(.white) // [.pink, .purple, .mint] } .interpolationMethod(.cardinal) } // X-Axis .chartXScale(domain: 1 ... statsByUser.takens) .chartXAxis(.hidden) // Y-Axis .chartYScale(domain: statsByUser.min ... statsByUser.max) .chartYAxis { AxisMarks{ _ in AxisGridLine().foregroundStyle(.white) AxisValueLabel(centered: true).foregroundStyle(.white) } } Model func getMax(user: Int64) -> Double { let tempsUser: [Double] = self.tempeartures.filter{ $0.user == user }.map{ $0.degress } if tempsUser.isEmpty { return 0.0 } let maxValue = getViewDegrees(tempsUser.max() ?? 0, loadUnitDefault()) return maxValue } func getMin(user: Int64) -> Double { let tempsUser: [Double] = self.tempeartures.filter{ $0.user == user }.map{ $0.degress } if tempsUser.isEmpty { return 0.0 } let minValue = getViewDegrees(tempsUser.min() ?? 0, loadUnitDefault()) return minValue } Without Scale Y Axis shows: instead this:
Oct ’22