Replace ChartsPackage with native Swift Charts
- Rewrite HeaderStatsView using SwiftUI Charts framework - Delete unused GraphView.swift (only used in previews) - Remove unused `import Charts` from DayView.swift - Remove ChartsPackage SPM dependency from project - Native Swift Charts is simpler and has no external dependencies 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,20 +8,19 @@
|
||||
import SwiftUI
|
||||
import Charts
|
||||
|
||||
struct HeaderStatsView : UIViewRepresentable {
|
||||
//Bar chart accepts data as array of BarChartDataEntry objects
|
||||
var entries : [BarChartDataEntry]
|
||||
var moodTints: [Color]
|
||||
var textColor: Color
|
||||
|
||||
var tmpHolderToMakeViewDiffefrent: Color
|
||||
|
||||
struct MoodBarData: Identifiable {
|
||||
let id = UUID()
|
||||
let mood: Mood
|
||||
let count: Int
|
||||
let color: Color
|
||||
}
|
||||
|
||||
struct HeaderStatsView: View {
|
||||
let barData: [MoodBarData]
|
||||
let textColor: Color
|
||||
|
||||
init(fakeData: Bool, backDays: Int, moodTint: [Color], textColor: Color) {
|
||||
self.moodTints = moodTint
|
||||
self.textColor = textColor
|
||||
assert(moodTints.count == 5, "mood tint count should be 5")
|
||||
self.tmpHolderToMakeViewDiffefrent = Color.random()
|
||||
entries = [BarChartDataEntry]()
|
||||
|
||||
var moodEntries: [MoodEntryModel]?
|
||||
|
||||
@@ -30,106 +29,56 @@ struct HeaderStatsView : UIViewRepresentable {
|
||||
} else {
|
||||
guard let daysAgoDate = Calendar.current.date(byAdding: .day, value: -backDays, to: Date()),
|
||||
let daysAgo = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: daysAgoDate) else {
|
||||
self.barData = []
|
||||
return
|
||||
}
|
||||
|
||||
moodEntries = DataController.shared.getData(startDate: daysAgo, endDate: Date(), includedDays: [1,2,3,4,5,6,7])
|
||||
}
|
||||
|
||||
var data: [MoodBarData] = []
|
||||
if let moodEntries = moodEntries {
|
||||
for (index, mood) in Mood.allValues.enumerated() {
|
||||
entries.append(
|
||||
BarChartDataEntry(x: Double(index + 1),
|
||||
y: Double(moodEntries.filter({
|
||||
Int($0.moodValue) == mood.rawValue
|
||||
}).count))
|
||||
)
|
||||
let count = moodEntries.filter { Int($0.moodValue) == mood.rawValue }.count
|
||||
let color = index < moodTint.count ? moodTint[index] : .gray
|
||||
data.append(MoodBarData(mood: mood, count: count, color: color))
|
||||
}
|
||||
}
|
||||
self.barData = data
|
||||
}
|
||||
|
||||
// this func is required to conform to UIViewRepresentable protocol
|
||||
func makeUIView(context: Context) -> BarChartView {
|
||||
//crate new chart
|
||||
let chart = BarChartView()
|
||||
chart.drawGridBackgroundEnabled = false
|
||||
chart.drawValueAboveBarEnabled = false
|
||||
|
||||
chart.xAxis.drawAxisLineEnabled = false
|
||||
chart.xAxis.labelTextColor = .clear
|
||||
|
||||
chart.rightAxis.drawAxisLineEnabled = false
|
||||
chart.rightAxis.labelTextColor = .clear
|
||||
|
||||
chart.leftAxis.drawAxisLineEnabled = false
|
||||
chart.leftAxis.labelTextColor = .clear
|
||||
|
||||
chart.xAxis.drawGridLinesEnabled = false
|
||||
chart.leftAxis.drawGridLinesEnabled = false
|
||||
chart.rightAxis.drawGridLinesEnabled = false
|
||||
|
||||
chart.leftAxis.axisLineColor = .clear
|
||||
chart.rightAxis.axisLineColor = .clear
|
||||
|
||||
chart.legend.textColor = .clear
|
||||
chart.legend.enabled = false
|
||||
|
||||
chart.drawBordersEnabled = false
|
||||
chart.drawMarkers = false
|
||||
chart.borderColor = .clear
|
||||
|
||||
chart.doubleTapToZoomEnabled = false
|
||||
chart.leftAxis.axisMinimum = 0
|
||||
|
||||
chart.minOffset = 0
|
||||
|
||||
let data = BarChartData()
|
||||
let dataSet = dataSet()
|
||||
data.append(dataSet)
|
||||
|
||||
chart.data = data
|
||||
dataSet.valueFormatter = DefaultValueFormatter(decimals: 0)
|
||||
|
||||
return chart
|
||||
}
|
||||
|
||||
// this func is required to conform to UIViewRepresentable protocol
|
||||
func updateUIView(_ uiView: BarChartView, context: Context) {
|
||||
let data = BarChartData()
|
||||
let dataSet = dataSet()
|
||||
data.append(dataSet)
|
||||
uiView.data = data
|
||||
|
||||
dataSet.valueFormatter = DefaultValueFormatter(decimals: 0)
|
||||
}
|
||||
|
||||
func dataSet() -> BarChartDataSet {
|
||||
let dataSet = BarChartDataSet(entries: entries)
|
||||
|
||||
// change bars color to green
|
||||
dataSet.colors = moodTints.map({ NSUIColor( $0 ) })
|
||||
dataSet.secondaryTextColor = UIColor(textColor)
|
||||
dataSet.valueColors = [UIColor(textColor)]
|
||||
dataSet.highlightAlpha = 0.0
|
||||
dataSet.roundedCornerValue = 10
|
||||
|
||||
if let descriptor = UIFontDescriptor.preferredFontDescriptor(
|
||||
withTextStyle: .title1).withSymbolicTraits([.traitBold]) {
|
||||
dataSet.valueFont = UIFont(descriptor: descriptor, size: 0)
|
||||
} else {
|
||||
dataSet.valueFont = UIFont.preferredFont(forTextStyle: .title1)
|
||||
var body: some View {
|
||||
Chart(barData) { item in
|
||||
BarMark(
|
||||
x: .value("Mood", item.mood.widgetDisplayName),
|
||||
y: .value("Count", item.count)
|
||||
)
|
||||
.foregroundStyle(item.color)
|
||||
.cornerRadius(10)
|
||||
.annotation(position: .top) {
|
||||
Text("\(item.count)")
|
||||
.font(.title.bold())
|
||||
.foregroundColor(textColor)
|
||||
}
|
||||
}
|
||||
|
||||
return dataSet
|
||||
.chartXAxis(.hidden)
|
||||
.chartYAxis(.hidden)
|
||||
.chartLegend(.hidden)
|
||||
.chartYScale(domain: 0...(maxCount + 1))
|
||||
}
|
||||
|
||||
private var maxCount: Int {
|
||||
max(barData.map(\.count).max() ?? 1, 1)
|
||||
}
|
||||
|
||||
typealias UIViewType = BarChartView
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct HeaderStatsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
HeaderStatsView(fakeData: true, backDays: 30, moodTint: [Color.green, Color.blue, Color.yellow, Color.red, Color.orange], textColor: .white).frame(minHeight: 85, maxHeight: 90)
|
||||
HeaderStatsView(
|
||||
fakeData: true,
|
||||
backDays: 30,
|
||||
moodTint: [Color.green, Color.blue, Color.yellow, Color.red, Color.orange],
|
||||
textColor: .white
|
||||
)
|
||||
.frame(minHeight: 85, maxHeight: 90)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user