-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwiftChartsView.swift
53 lines (44 loc) · 1.35 KB
/
SwiftChartsView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// SwiftChartsView.swift
// swift-charts
//
// Created by Oskar Kwaśniewski on 15/03/2023.
//
import SwiftUI
class DataStore: ObservableObject {
@Published var data: [ValuePerCategory] = []
@Published var color: Color = .accentColor
}
class SwiftChartsView: UIView {
private var dataStore: DataStore = DataStore()
@objc var data: NSArray = [] {
didSet {
dataStore.data = processData(data)
}
}
@objc var color: String = "" {
didSet {
dataStore.color = Color(hex: color) ?? .accentColor
}
}
override init(frame: CGRect) {
super.init(frame: frame)
let hostingController = UIHostingController(rootView: ChartView().environmentObject(dataStore))
addSubview(hostingController.view)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
subviews.first?.frame = self.frame
}
private func processData(_ data: NSArray) -> [ValuePerCategory] {
let dictArray = data.compactMap { $0 as? NSDictionary }
return dictArray.map { data in
return ValuePerCategory(
category: data["category"] as? String ?? "",
value: data["value"] as? Double ?? 0
)
}
}
}