Charts - Exclude dates on x-axis

Hi!

I'm building an app that uses Swift Charts to visualize stock market data, and I'm encountering a couple of issues.

The stock API I’m using provides data only for the trading days when the market is open. The problem is that I need to skip over the missing dates (non-trading days) in the chart, but still keep the x-axis formatted correctly (e.g., group ticks by month). If I convert the dates to String to handle missing data, I lose the correct x-axis formatting, and the date labels become inaccurate among with its data.

Here’s som of the code I’m using for parsing the dates and structuring the data:

struct StockDataPoint: Identifiable, Decodable { var id: String { datetime } let datetime: String let close: String

var date: Date {
datetime.toDate() ?? Date()
}
var closePrice: Double {
Double(close) ?? 0.0
}

}

extension String { func toDate() -> Date? { let formatter = DateFormatter() formatter.locale = Locale(identifier: "en_US_POSIX") formatter.timeZone = TimeZone(abbreviation: "UTC") formatter.dateFormat = self.count == 10 ? "yyyy-MM-dd" : "yyyy-MM-dd HH:mm:ss" return formatter.date(from: self) } }

And:

LineMark( x: .value("Datum", point.date), y: .value("Pris", point.closePrice) ) .interpolationMethod(.cardinal) .lineStyle(StrokeStyle(lineWidth: 0.7)) .foregroundStyle(.linearGradient(colors: [.blue, .yellow, .orange], startPoint: .bottomTrailing, endPoint: .topLeading)) .frame(height: 300) .background(Color.black.opacity(0.6)) .chartYScale( domain: ( (stockAPI.stockData.map { $0.closePrice }.min() ?? 0) * 0.98 ... (stockAPI.stockData.map { $0.closePrice }.max() ?? 100) * 1.02 ) ) .chartXAxis { AxisMarks(values: .automatic(desiredCount: 5)) { value in AxisGridLine().foregroundStyle(Color.gray.opacity(0.5)) AxisTick().foregroundStyle(Color.gray) AxisValueLabel().foregroundStyle(Color.gray) } } .chartYAxis { AxisMarks(values: .automatic(desiredCount: 5)) { value in AxisGridLine().foregroundStyle(Color.gray.opacity(0.5)) AxisTick().foregroundStyle(Color.gray) AxisValueLabel().foregroundStyle(Color.gray) } }

What I need help with: Skipping missing dates on the x-axis and show correct data for corresponding days. Keeping the x-axis well formatted (grouped by month, accurate labels). Thanks in advance for any suggestions!

Answered by DTS Engineer in 836333022

This question is answered by an engineer from the Swift Charts team in the following forums post:

Handling of dates with no value when creating a date bar chart in Charts

This question is answered by an engineer from the Swift Charts team in the following forums post:

Handling of dates with no value when creating a date bar chart in Charts

Swift Charts doesn't support interrupted scales. If you need a piecewise continuous scale, fairly common in finance (such as showing market hours only) you can model your time scale via Double values that represent market time (eg. market hours). Like some epoch time, but it's only incremented when the markets are open. Friday afternoon plus 1 (1 hour) lands you on Monday morning. In this case you are responsible for maintaining the mapping between your "market epoch" time and whatever wall time that corresponds to. For example, you'd determine Double values for where you want axis ticks, tick labels and grid lines, and you'd map from the Double back to eg. a calendar Date to then format that as an axis tick label. Similarly, if you use selection on the chart (.chartXSelection) you'd need to map the Double back to the way you represent time in your data.

An option: instead of using a Double, you can use other numeric types, or conform your own custom time type that represents market time to the Plottable protocol. That way, you can directly plot with your market time type, as the conformance methods do the mappings for you. You'll still need to take care of the above listed elements.

Charts - Exclude dates on x-axis
 
 
Q