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)
}
Spacer()
TagChartView(tag: tag)
}
.contentShape(Rectangle())
.onTapGesture {

View File

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

View File

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

View File

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