Header chart view on main page

change layout of main page rows
This commit is contained in:
Trey t
2022-01-16 20:50:41 -06:00
parent d9add2eed8
commit 94888c9ea5
5 changed files with 57 additions and 27 deletions

View File

@@ -11,6 +11,30 @@ import Charts
struct HeaderStatsView : UIViewRepresentable {
//Bar chart accepts data as array of BarChartDataEntry objects
var entries : [BarChartDataEntry]
init(fakeData: Bool, backDays: Int) {
entries = [BarChartDataEntry]()
var moodEntries: [MoodEntry]?
if fakeData {
moodEntries = PersistenceController.shared.randomEntries(count: 10)
} else {
var daysAgo = Calendar.current.date(byAdding: .day, value: -backDays, to: Date())!
daysAgo = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: daysAgo)!
moodEntries = PersistenceController.shared.getData(startDate: daysAgo, endDate: Date(), includedDays: [1,2,3,4,5,6,7])
}
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)))
}
}
}
// this func is required to conform to UIViewRepresentable protocol
func makeUIView(context: Context) -> BarChartView {
//crate new chart
@@ -56,7 +80,7 @@ struct HeaderStatsView : UIViewRepresentable {
//BarChartDataSet is an object that contains information about your data, styling and more
let dataSet = BarChartDataSet(entries: entries)
// change bars color to green
dataSet.colors = [NSUIColor.green]
dataSet.colors = Mood.allValues.map({ NSUIColor( $0.color ) })
//change data label
data.append(dataSet)
return data
@@ -70,13 +94,6 @@ struct HeaderStatsView : UIViewRepresentable {
struct HeaderStatsView_Previews: PreviewProvider {
static var previews: some View {
HeaderStatsView(entries: [
//x - position of a bar, y - height of a bar
BarChartDataEntry(x: 1, y: 1),
BarChartDataEntry(x: 2, y: 4),
BarChartDataEntry(x: 3, y: 3),
BarChartDataEntry(x: 4, y: 2),
BarChartDataEntry(x: 5, y: 1)
]).frame(minHeight: 85, maxHeight: 90)
HeaderStatsView(fakeData: true, backDays: 30).frame(minHeight: 85, maxHeight: 90)
}
}