Add charts for tags

This commit is contained in:
Thomas Ricouard 2023-12-13 09:05:30 +01:00
parent d31af12bb6
commit 232e031559
5 changed files with 40 additions and 3 deletions

View File

@ -0,0 +1,24 @@
import SwiftUI
import Charts
import Models
public struct TagChartView: View {
let tag: Tag
public init(tag: Tag) {
self.tag = tag
}
public var body: some View {
Chart(tag.sortedHistory) { data in
AreaMark(x: .value("day", tag.sortedHistory.firstIndex(where: { $0.id == data.id }) ?? 0),
y: .value("uses", Int(data.uses) ?? 0))
.interpolationMethod(.catmullRom)
}
.chartLegend(.hidden)
.chartXAxis(.hidden)
.chartYAxis(.hidden)
.frame(width: 70, height: 40)
.clipShape(RoundedRectangle(cornerRadius: 4))
}
}

View File

@ -21,6 +21,7 @@ public struct TagRowView: View {
.foregroundStyle(.secondary) .foregroundStyle(.secondary)
} }
Spacer() Spacer()
TagChartView(tag: tag)
} }
.contentShape(Rectangle()) .contentShape(Rectangle())
.onTapGesture { .onTapGesture {

View File

@ -1,7 +1,10 @@
import Foundation import Foundation
public struct Tag: Codable, Identifiable, Equatable, Hashable { public struct Tag: Codable, Identifiable, Equatable, Hashable {
public struct History: Codable { public struct History: Codable, Identifiable {
public var id: String {
day
}
public let day: String public let day: String
public let accounts: String public let accounts: String
public let uses: String public let uses: String
@ -24,6 +27,11 @@ public struct Tag: Codable, Identifiable, Equatable, Hashable {
public let following: Bool public let following: Bool
public let history: [History] public let history: [History]
public var sortedHistory: [History] {
history.sorted {
Int($0.day) ?? 0 < Int($1.day) ?? 0
}
}
public var totalUses: Int { public var totalUses: Int {
history.compactMap { Int($0.uses) }.reduce(0, +) history.compactMap { Int($0.uses) }.reduce(0, +)
} }

View File

@ -5,8 +5,8 @@
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",
"location" : "https://github.com/divadretlaw/EmojiText", "location" : "https://github.com/divadretlaw/EmojiText",
"state" : { "state" : {
"revision" : "b5b0a30933a6dcb6601ad3625690a823fa3f6965", "revision" : "bb1fd025fe6da0d98b57088e9b40a37277e95503",
"version" : "2.6.0" "version" : "3.1.0"
} }
}, },
{ {

View File

@ -7,6 +7,7 @@ import Status
import SwiftData import SwiftData
import SwiftUI import SwiftUI
import SwiftUIIntrospect import SwiftUIIntrospect
import Charts
@MainActor @MainActor
public struct TimelineView: View { public struct TimelineView: View {
@ -154,6 +155,9 @@ public struct TimelineView: View {
if let tag = viewModel.tag { if let tag = viewModel.tag {
headerView { headerView {
HStack { HStack {
TagChartView(tag: tag)
.padding(.top, 12)
VStack(alignment: .leading, spacing: 4) { VStack(alignment: .leading, spacing: 4) {
Text("#\(tag.name)") Text("#\(tag.name)")
.font(.scaledHeadline) .font(.scaledHeadline)