質問編集履歴
3
追記
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
ああああああああああああああああああ
|
body
CHANGED
@@ -1,141 +1,1 @@
|
|
1
|
-
|
2
|
-
を参考に棒グラフを作成しています。
|
3
|
-
サイトでは仮データとして、Int型とString型の配列を使われていますが、
|
4
|
-
オリジナルとして、この棒グラフにDate型とInt型の配列を適用したいです。
|
5
|
-
以下のコードがサイトのコードを少し改変したオリジナルコードです。
|
6
|
-
ただ、このままだと、barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: items.map({ $0.date}))の行で、Cannot convert value of type 'Date' to closure result type 'String'エラーが発生します。date型を適用するためには、この行をどのように変更すれば良いか教えて下さい。
|
7
|
-
|
8
|
-
```ここに言語を入力
|
9
|
-
import UIKit
|
10
|
-
import Charts
|
11
|
-
import FirebaseFirestore
|
12
|
-
|
13
|
-
struct BarChartModel {
|
14
|
-
let plice: Int
|
15
|
-
let date: Date
|
16
|
-
}
|
17
|
-
|
18
|
-
class ChartViewController: UIViewController {
|
19
|
-
|
20
|
-
@IBOutlet weak var scrollView: UIScrollView!
|
21
|
-
|
22
|
-
lazy var maxVal: Int = barItems.map({ $0.0 }).max()!
|
23
|
-
var barItems: [(plice: Int, date: Date)] = []
|
24
|
-
var foodData: FoodData!
|
25
|
-
|
26
|
-
func setData(_ foodData: FoodData) {
|
27
|
-
barItems.append(contentsOf: [(foodData.plice, foodData.date)])
|
28
|
-
}
|
29
|
-
|
30
|
-
override func viewDidLoad() {
|
31
|
-
super.viewDidLoad()
|
32
|
-
scrollView.frame = CGRect(x: 0,y: 0,width: scrollView.superview!.frame.width,height: scrollView.superview!.frame.height)
|
33
|
-
let contentsView = createContentsView(of: barItems.map({ BarChartModel(plice: $0.0, date: $0.1 ) }),barsCountPerPage: 7)
|
34
|
-
scrollView.addSubview(contentsView)
|
35
|
-
scrollView.contentSize = contentsView.frame.size
|
36
|
-
scrollView.isPagingEnabled = true
|
37
|
-
setData(foodData)
|
38
|
-
}
|
39
|
-
|
40
|
-
func createBarChartView(of items: [BarChartModel]) -> BarChartView {
|
41
|
-
let barChartView = BarChartView()
|
42
|
-
barChartView.delegate = self
|
43
|
-
barChartView.animate(yAxisDuration: 1)
|
44
|
-
barChartView.data = createBarChartData(of: items.map({BarChartModel(plice: $0.plice, date: $0.date)}))
|
45
|
-
barChartView.xAxis.labelCount = barItems.count
|
46
|
-
barChartView.xAxis.labelPosition = .bottom
|
47
|
-
barChartView.xAxis.drawGridLinesEnabled = false
|
48
|
-
barChartView.xAxis.valueFormatter = DateIntervalFormatter(values: items.map({ $0.date })) as! IAxisValueFormatter
|
49
|
-
barChartView.leftAxis.enabled = false
|
50
|
-
barChartView.rightAxis.enabled = false
|
51
|
-
barChartView.legend.enabled = false
|
52
|
-
barChartView.pinchZoomEnabled = false
|
53
|
-
barChartView.doubleTapToZoomEnabled = false
|
54
|
-
barChartView.leftAxis.axisMaximum = Double(maxVal) + 1
|
55
|
-
return barChartView
|
56
|
-
}
|
57
|
-
|
58
|
-
private func createBarChartData(of items: [BarChartModel]) -> BarChartData {
|
59
|
-
let entries: [BarChartDataEntry] = items.enumerated().map {
|
60
|
-
let (i, item) = $0
|
61
|
-
return BarChartDataEntry(x: Double(i), y: Double(item.plice))
|
62
|
-
}
|
63
|
-
let barChartDataSet = BarChartDataSet(entries: entries, label: "Label")
|
64
|
-
barChartDataSet.valueFont = .systemFont(ofSize: 20)
|
65
|
-
barChartDataSet.valueFormatter = ValueFormatter(of: items)
|
66
|
-
barChartDataSet.colors = items.map { $0.plice == maxVal ? .systemOrange : .systemBlue }
|
67
|
-
let barChartData = BarChartData(dataSet: barChartDataSet)
|
68
|
-
return barChartData
|
69
|
-
}
|
70
|
-
|
71
|
-
private func createContentsView(of items: [BarChartModel], barsCountPerPage: Int) -> UIView {
|
72
|
-
let itemsPerPage = stride(from: 0, to: items.count, by: barsCountPerPage).map { Array(items[$0 ..< min($0 + barsCountPerPage, items.count)]) }
|
73
|
-
let contentsView = UIView(frame: CGRect(x: 0, y: 0, width: scrollView.frame.width * CGFloat(itemsPerPage.count), height: scrollView.frame.height))
|
74
|
-
for (i, items) in itemsPerPage.enumerated() {
|
75
|
-
let barChartView = createBarChartView(of: items)
|
76
|
-
let percent = CGFloat(items.count) / CGFloat(itemsPerPage[0].count)
|
77
|
-
barChartView.frame = CGRect(x: scrollView.frame.width * CGFloat(i), y: 0, width: scrollView.frame.width * percent, height:scrollView.frame.height)
|
78
|
-
contentsView.addSubview(barChartView)
|
79
|
-
}
|
80
|
-
return contentsView
|
81
|
-
}
|
82
|
-
}
|
83
|
-
|
84
|
-
extension ChartViewController: ChartViewDelegate {
|
85
|
-
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
|
86
|
-
let axisFormatter = chartView.xAxis.valueFormatter!
|
87
|
-
let label = axisFormatter.stringForValue(entry.x, axis: nil)
|
88
|
-
print(label, entry.y)
|
89
|
-
}
|
90
|
-
}
|
91
|
-
|
92
|
-
class ValueFormatter: IValueFormatter {
|
93
|
-
let items: [BarChartModel]
|
94
|
-
init(of items: [BarChartModel]) {
|
95
|
-
self.items = items
|
96
|
-
}
|
97
|
-
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
|
98
|
-
return "(Int(value))"
|
99
|
-
}
|
100
|
-
}
|
101
|
-
|
102
|
-
class XAxisFormatter: IAxisValueFormatter {
|
103
|
-
|
104
|
-
let items: [BarChartModel]
|
105
|
-
init(of items: [BarChartModel]) {
|
106
|
-
self.items = items
|
107
|
-
}
|
108
|
-
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
|
109
|
-
let index = Int(value)
|
110
|
-
return self.items[index].date
|
111
|
-
}
|
112
|
-
}
|
113
|
-
```
|
114
|
-
|
115
|
-
```ここに言語を入力
|
116
|
-
import UIKit
|
117
|
-
import FirebaseFirestore
|
118
|
-
import FirebaseAuth
|
119
|
-
|
120
|
-
class FoodData: NSObject {
|
121
|
-
var id: String
|
122
|
-
var food: String
|
123
|
-
var number: Int
|
124
|
-
var plice: Int
|
125
|
-
var pliceArray: [Int]
|
126
|
-
var date: Date
|
127
|
-
var dateArray: [Date]
|
128
|
-
|
129
|
-
init(document: QueryDocumentSnapshot) {
|
130
|
-
self.id = document.documentID
|
131
|
-
let foodDic = document.data()
|
132
|
-
self.food = foodDic["food"] as! String
|
133
|
-
self.number = foodDic["number"] as! Int
|
134
|
-
self.plice = foodDic["plice"] as! Int
|
135
|
-
self.pliceArray = [self.plice]
|
136
|
-
let timeStamp = foodDic["date"] as! Timestamp
|
137
|
-
self.date = timeStamp.dateValue()
|
138
|
-
self.dateArray = [self.date]
|
139
|
-
}
|
140
|
-
}
|
141
|
-
```
|
1
|
+
ああああああああああああああああああああああああああああああああああ
|
2
追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,10 +21,10 @@
|
|
21
21
|
|
22
22
|
lazy var maxVal: Int = barItems.map({ $0.0 }).max()!
|
23
23
|
var barItems: [(plice: Int, date: Date)] = []
|
24
|
-
var
|
24
|
+
var foodData: FoodData!
|
25
25
|
|
26
|
-
func setData(_
|
26
|
+
func setData(_ foodData: FoodData) {
|
27
|
-
barItems.append(contentsOf: [(
|
27
|
+
barItems.append(contentsOf: [(foodData.plice, foodData.date)])
|
28
28
|
}
|
29
29
|
|
30
30
|
override func viewDidLoad() {
|
@@ -34,7 +34,7 @@
|
|
34
34
|
scrollView.addSubview(contentsView)
|
35
35
|
scrollView.contentSize = contentsView.frame.size
|
36
36
|
scrollView.isPagingEnabled = true
|
37
|
-
setData(
|
37
|
+
setData(foodData)
|
38
38
|
}
|
39
39
|
|
40
40
|
func createBarChartView(of items: [BarChartModel]) -> BarChartView {
|
@@ -45,7 +45,7 @@
|
|
45
45
|
barChartView.xAxis.labelCount = barItems.count
|
46
46
|
barChartView.xAxis.labelPosition = .bottom
|
47
47
|
barChartView.xAxis.drawGridLinesEnabled = false
|
48
|
-
barChartView.xAxis.valueFormatter =
|
48
|
+
barChartView.xAxis.valueFormatter = DateIntervalFormatter(values: items.map({ $0.date })) as! IAxisValueFormatter
|
49
49
|
barChartView.leftAxis.enabled = false
|
50
50
|
barChartView.rightAxis.enabled = false
|
51
51
|
barChartView.legend.enabled = false
|
@@ -100,11 +100,12 @@
|
|
100
100
|
}
|
101
101
|
|
102
102
|
class XAxisFormatter: IAxisValueFormatter {
|
103
|
+
|
103
104
|
let items: [BarChartModel]
|
104
105
|
init(of items: [BarChartModel]) {
|
105
106
|
self.items = items
|
106
107
|
}
|
107
|
-
func stringForValue(_ value: Double, axis: AxisBase?) ->
|
108
|
+
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
|
108
109
|
let index = Int(value)
|
109
110
|
return self.items[index].date
|
110
111
|
}
|
1
追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -109,5 +109,32 @@
|
|
109
109
|
return self.items[index].date
|
110
110
|
}
|
111
111
|
}
|
112
|
+
```
|
112
113
|
|
114
|
+
```ここに言語を入力
|
115
|
+
import UIKit
|
116
|
+
import FirebaseFirestore
|
117
|
+
import FirebaseAuth
|
118
|
+
|
119
|
+
class FoodData: NSObject {
|
120
|
+
var id: String
|
121
|
+
var food: String
|
122
|
+
var number: Int
|
123
|
+
var plice: Int
|
124
|
+
var pliceArray: [Int]
|
125
|
+
var date: Date
|
126
|
+
var dateArray: [Date]
|
127
|
+
|
128
|
+
init(document: QueryDocumentSnapshot) {
|
129
|
+
self.id = document.documentID
|
130
|
+
let foodDic = document.data()
|
131
|
+
self.food = foodDic["food"] as! String
|
132
|
+
self.number = foodDic["number"] as! Int
|
133
|
+
self.plice = foodDic["plice"] as! Int
|
134
|
+
self.pliceArray = [self.plice]
|
135
|
+
let timeStamp = foodDic["date"] as! Timestamp
|
136
|
+
self.date = timeStamp.dateValue()
|
137
|
+
self.dateArray = [self.date]
|
138
|
+
}
|
139
|
+
}
|
113
140
|
```
|