Well, it's finally time to tackel the next major feature of my Stocks App - the 5yr Price Chart.
This is maybe the 14th Stock App I've attempted... and maybe this one has a hope of seeing the App Store. All the others started as an App to display charts... all failed. It is really hard to do better than Apple's Stocks App.
This time I focused AWAY from the bright shinny chart! And that focus trick may be the thing that works... so it is with lots of emotion and dred, that I break into the Charts API again.
I wish Big Mountain Studio would focus on the Swift Charts and do a book on it. I've found his books to be the best way I learn - lot's of examples and visual guides.
I was thrilled to see Apple release the Swift Charts API. And I jumped right into learning it... Oh when was that 2016? NO, way too early ... oh iOS16 Swift Charts in iOS16. That was summer of 2022.
Simple bar charts are one thing... a stock market price chart is a real challenge. Oh - not so fast there sparky... too big a challenge for a newbie like me.
But, it's been years... maybe I've learned something... it's now winter of 2025.
For one thing - I've discovered FatBobMan's blog. He's even written about one of the never discussed Charts features - Data Binning.
That 5 year chart is not real... just a placeholder to keep the dream alive...
I could clean up some of the existing features - add the polish that comes with actually using and questioning each and every word on the UI; or I could venture into the design land of charts...
Maybe I'll wait a few days, and do some polishing... there will be far fewer bruises down this path.
I just found Brandfetch - I could add icons.
In 2025 do we really want to see dollars AND CENTS? We could just trunicate to dollars!
Let's start with a simple dot plot of company earning. This could be a great introduction to Charts. One of the keys to successful charting is defining an approprate data structure... getting this correct helps the design just work.
In this case I'm desiring a dot plot of the last 2 years of company quarterly earnings, along with the general per-earnings estimates.
Here is the prototype Earnings chart. The general idea is shown and the chart axis labels need enhancment, as well as being constrained on the Y-axis to more resonable values.
Buy, YEAH! I've broken into the new feature set ... Charts!
Now - how does one extract the existing data in this simple chartable format?
import SwiftUI
import Charts
struct EarningPoint : Identifiable {
let date: String
let value: Double
let estimate: Double
let id: UUID = UUID()
}
struct QtrEarningChart: View {
var body: some View {
Chart(data.reversed() ) {
// flow data from old date to -> new date on x-axis
// plot the gray estimate
PointMark (x: .value("Est", $0.date),
y: .value("Est", $0.estimate)
)
.foregroundStyle(.gray)
.symbolSize(by: .value("Size", $0.estimate) )
// actual EPS value plotted over Estimate in gray
PointMark (x: .value("EPS", $0.date),
y: .value("EPS", $0.value)
)
.foregroundStyle(.blue)
.symbolSize(by: .value("Size", $0.value) )
}
}
var data: [EarningPoint] = [
EarningPoint(date: "2024-12-31", value: 0.0, estimate: 1.75), // projected and unknown
EarningPoint(date: "2024-09-30", value: 1.64, estimate: 1.60),
EarningPoint(date: "2024-06-30", value: 1.40, estimate: 1.35),
EarningPoint(date: "2024-03-31", value: 1.53, estimate: 1.50),
EarningPoint(date: "2023-12-31", value: 2.18, estimate: 2.10),
EarningPoint(date: "2023-09-30", value: 1.46, estimate: 1.39),
EarningPoint(date: "2023-06-30", value: 1.26, estimate: 1.19),
EarningPoint(date: "2023-03-31", value: 1.52, estimate: 1.43)
]
}
#Preview(traits: .sizeThatFitsLayout) {
QtrEarningChart()
}