Ist there a proper way to remove weekend gaps from e.g. stock data? See picture.
The data are Linemarks with
x value : Date
y value : Double
Datapoint class:
public class HistoricalDataPoint : ObservableObject, Hashable, Identifiable, Codable, Comparable {
public static func < (lhs: HistoricalDataPoint, rhs: HistoricalDataPoint) -> Bool {
lhs.date < rhs.date
}
var timestamp : Double
let priceLow : Double
let priceHigh: Double
let priceOpen : Double
let priceClose: Double
let volume : Double
let date : Date
let identifier : String
let dataCreatedTimeStamp : Date
let code: String
var normalized = 0.0
public init(timestamp: Double, priceLow: Double, priceHigh: Double, priceOpen: Double, priceClose: Double, volume: Double, dataCreatedTimeStamp: Date, code: String) {
self.identifier = "\(timestamp)\(priceLow)\(priceHigh)\(priceOpen)\(priceClose)\(volume)"
self.timestamp = timestamp
self.priceLow = priceLow
self.priceHigh = priceHigh
self.priceOpen = priceOpen
self.priceClose = priceClose
self.volume = volume
self.date = Date(timeIntervalSince1970: TimeInterval(timestamp))
self.dataCreatedTimeStamp = dataCreatedTimeStamp
self.code = code
}
public static func == (lhs: HistoricalDataPoint, rhs: HistoricalDataPoint) -> Bool {
return lhs.date == rhs.date
}
public func hash(into hasher: inout Hasher) {
return hasher.combine(identifier)
}
}
I check the data for the weekday and only add if it's weekdays. But the stock data has no data for weekends anyway:
let gregorian = Calendar(identifier: .gregorian)
var components = gregorian.dateComponents([.weekday], from: date)
if(components.weekday != 1 && components.weekday != 7){
historicalData.append(HistoricalDataPoint(timestamp: date.timeIntervalSince1970, priceLow: Double(low), priceHigh: Double(high), priceOpen: Double(open), priceClose: Double(close), volume: Double(volume), dataCreatedTimeStamp: Date(),code: asset.code))
}
And the ChartView:
Chart(historicalData, id: \.date){
LineMark(
x: .value("Time", $0.date),
y: .value("Close", $0.priceClose)
)
.chartYScale(domain: lowY...highY)
.frame(height: height)