質問編集履歴

3

追記

2021/11/13 05:19

投稿

rararara
rararara

スコア11

test CHANGED
@@ -1 +1 @@
1
- chartsを使った棒グラフ
1
+ ああああああああああああああああああ
test CHANGED
@@ -1,281 +1 @@
1
- https://bombrary.github.io/blog/posts/iphone-barchart-ioscharts/
2
-
3
- を参考に棒グラフを作成しています。
4
-
5
- サイトでは仮データとして、Int型とString型の配列を使われていますが、
6
-
7
- オリジナルとして、この棒グラフにDate型とInt型の配列を適用したいです。
8
-
9
- 以下のコードがサイトのコードを少し改変したオリジナルコードです。
10
-
11
- ただ、このままだと、barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: items.map({ $0.date}))の行で、Cannot convert value of type 'Date' to closure result type 'String'エラーが発生します。date型を適用するためには、この行をどのように変更すれば良いか教えて下さい。
12
-
13
-
14
-
15
- ```ここに言語を入力
16
-
17
- import UIKit
18
-
19
- import Charts
20
-
21
- import FirebaseFirestore
22
-
23
-
24
-
25
- struct BarChartModel {
26
-
27
- let plice: Int
28
-
29
- let date: Date
30
-
31
- }
32
-
33
-
34
-
35
- class ChartViewController: UIViewController {
36
-
37
-
38
-
39
- @IBOutlet weak var scrollView: UIScrollView!
40
-
41
-
42
-
43
- lazy var maxVal: Int = barItems.map({ $0.0 }).max()!
44
-
45
- var barItems: [(plice: Int, date: Date)] = []
46
-
47
- var foodData: FoodData!
48
-
49
-
50
-
51
- func setData(_ foodData: FoodData) {
52
-
53
- barItems.append(contentsOf: [(foodData.plice, foodData.date)])
54
-
55
- }
56
-
57
-
58
-
59
- override func viewDidLoad() {
60
-
61
- super.viewDidLoad()
62
-
63
- scrollView.frame = CGRect(x: 0,y: 0,width: scrollView.superview!.frame.width,height: scrollView.superview!.frame.height)
64
-
65
- let contentsView = createContentsView(of: barItems.map({ BarChartModel(plice: $0.0, date: $0.1 ) }),barsCountPerPage: 7)
66
-
67
- scrollView.addSubview(contentsView)
68
-
69
- scrollView.contentSize = contentsView.frame.size
70
-
71
- scrollView.isPagingEnabled = true
72
-
73
- setData(foodData)
74
-
75
- }
76
-
77
-
78
-
79
- func createBarChartView(of items: [BarChartModel]) -> BarChartView {
80
-
81
- let barChartView = BarChartView()
82
-
83
- barChartView.delegate = self
84
-
85
- barChartView.animate(yAxisDuration: 1)
86
-
87
- barChartView.data = createBarChartData(of: items.map({BarChartModel(plice: $0.plice, date: $0.date)}))
88
-
89
- barChartView.xAxis.labelCount = barItems.count
90
-
91
- barChartView.xAxis.labelPosition = .bottom
92
-
93
- barChartView.xAxis.drawGridLinesEnabled = false
94
-
95
- barChartView.xAxis.valueFormatter = DateIntervalFormatter(values: items.map({ $0.date })) as! IAxisValueFormatter
96
-
97
- barChartView.leftAxis.enabled = false
98
-
99
- barChartView.rightAxis.enabled = false
100
-
101
- barChartView.legend.enabled = false
102
-
103
- barChartView.pinchZoomEnabled = false
104
-
105
- barChartView.doubleTapToZoomEnabled = false
106
-
107
- barChartView.leftAxis.axisMaximum = Double(maxVal) + 1
108
-
109
- return barChartView
110
-
111
- }
112
-
113
-
114
-
115
- private func createBarChartData(of items: [BarChartModel]) -> BarChartData {
116
-
117
- let entries: [BarChartDataEntry] = items.enumerated().map {
118
-
119
- let (i, item) = $0
120
-
121
- return BarChartDataEntry(x: Double(i), y: Double(item.plice))
122
-
123
- }
124
-
125
- let barChartDataSet = BarChartDataSet(entries: entries, label: "Label")
126
-
127
- barChartDataSet.valueFont = .systemFont(ofSize: 20)
128
-
129
- barChartDataSet.valueFormatter = ValueFormatter(of: items)
130
-
131
- barChartDataSet.colors = items.map { $0.plice == maxVal ? .systemOrange : .systemBlue }
132
-
133
- let barChartData = BarChartData(dataSet: barChartDataSet)
134
-
135
- return barChartData
136
-
137
- }
138
-
139
-
140
-
141
- private func createContentsView(of items: [BarChartModel], barsCountPerPage: Int) -> UIView {
142
-
143
- let itemsPerPage = stride(from: 0, to: items.count, by: barsCountPerPage).map { Array(items[$0 ..< min($0 + barsCountPerPage, items.count)]) }
144
-
145
- let contentsView = UIView(frame: CGRect(x: 0, y: 0, width: scrollView.frame.width * CGFloat(itemsPerPage.count), height: scrollView.frame.height))
146
-
147
- for (i, items) in itemsPerPage.enumerated() {
148
-
149
- let barChartView = createBarChartView(of: items)
150
-
151
- let percent = CGFloat(items.count) / CGFloat(itemsPerPage[0].count)
152
-
153
- barChartView.frame = CGRect(x: scrollView.frame.width * CGFloat(i), y: 0, width: scrollView.frame.width * percent, height:scrollView.frame.height)
154
-
155
- contentsView.addSubview(barChartView)
156
-
157
- }
158
-
159
- return contentsView
160
-
161
- }
162
-
163
- }
164
-
165
-
166
-
167
- extension ChartViewController: ChartViewDelegate {
168
-
169
- func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
170
-
171
- let axisFormatter = chartView.xAxis.valueFormatter!
172
-
173
- let label = axisFormatter.stringForValue(entry.x, axis: nil)
174
-
175
- print(label, entry.y)
176
-
177
- }
178
-
179
- }
180
-
181
-
182
-
183
- class ValueFormatter: IValueFormatter {
184
-
185
- let items: [BarChartModel]
186
-
187
- init(of items: [BarChartModel]) {
188
-
189
- self.items = items
190
-
191
- }
192
-
193
- func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
194
-
195
- return "(Int(value))"
196
-
197
- }
198
-
199
- }
200
-
201
-
202
-
203
- class XAxisFormatter: IAxisValueFormatter {
204
-
205
-
206
-
207
- let items: [BarChartModel]
208
-
209
- init(of items: [BarChartModel]) {
210
-
211
- self.items = items
212
-
213
- }
214
-
215
- func stringForValue(_ value: Double, axis: AxisBase?) -> String {
216
-
217
- let index = Int(value)
218
-
219
- return self.items[index].date
220
-
221
- }
222
-
223
- }
224
-
225
- ```
226
-
227
-
228
-
229
- ```ここに言語を入力
230
-
231
- import UIKit
232
-
233
- import FirebaseFirestore
234
-
235
- import FirebaseAuth
236
-
237
-
238
-
239
- class FoodData: NSObject {
240
-
241
- var id: String
242
-
243
- var food: String
244
-
245
- var number: Int
246
-
247
- var plice: Int
248
-
249
- var pliceArray: [Int]
250
-
251
- var date: Date
252
-
253
- var dateArray: [Date]
254
-
255
-
256
-
257
- init(document: QueryDocumentSnapshot) {
258
-
259
- self.id = document.documentID
260
-
261
- let foodDic = document.data()
262
-
263
- self.food = foodDic["food"] as! String
264
-
265
- self.number = foodDic["number"] as! Int
266
-
267
- self.plice = foodDic["plice"] as! Int
268
-
269
- self.pliceArray = [self.plice]
270
-
271
- let timeStamp = foodDic["date"] as! Timestamp
272
-
273
- self.date = timeStamp.dateValue()
274
-
275
- self.dateArray = [self.date]
276
-
277
- }
278
-
279
- }
280
-
281
- ```
1
+ ああああああああああああああああああああああああああああああああああ

2

追加

2021/11/13 05:19

投稿

rararara
rararara

スコア11

test CHANGED
File without changes
test CHANGED
@@ -44,13 +44,13 @@
44
44
 
45
45
  var barItems: [(plice: Int, date: Date)] = []
46
46
 
47
- var barData: BarData!
47
+ var foodData: FoodData!
48
-
49
-
50
-
48
+
49
+
50
+
51
- func setData(_ data: FoodData) {
51
+ func setData(_ foodData: FoodData) {
52
-
52
+
53
- barItems.append(contentsOf: [(data.plice, data.date)])
53
+ barItems.append(contentsOf: [(foodData.plice, foodData.date)])
54
54
 
55
55
  }
56
56
 
@@ -70,7 +70,7 @@
70
70
 
71
71
  scrollView.isPagingEnabled = true
72
72
 
73
- setData(barData)
73
+ setData(foodData)
74
74
 
75
75
  }
76
76
 
@@ -92,7 +92,7 @@
92
92
 
93
93
  barChartView.xAxis.drawGridLinesEnabled = false
94
94
 
95
- barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: items.map({ $0.1}))
95
+ barChartView.xAxis.valueFormatter = DateIntervalFormatter(values: items.map({ $0.date })) as! IAxisValueFormatter
96
96
 
97
97
  barChartView.leftAxis.enabled = false
98
98
 
@@ -202,6 +202,8 @@
202
202
 
203
203
  class XAxisFormatter: IAxisValueFormatter {
204
204
 
205
+
206
+
205
207
  let items: [BarChartModel]
206
208
 
207
209
  init(of items: [BarChartModel]) {
@@ -210,7 +212,7 @@
210
212
 
211
213
  }
212
214
 
213
- func stringForValue(_ value: Double, axis: AxisBase?) -> Date {
215
+ func stringForValue(_ value: Double, axis: AxisBase?) -> String {
214
216
 
215
217
  let index = Int(value)
216
218
 

1

追加

2021/11/08 14:11

投稿

rararara
rararara

スコア11

test CHANGED
File without changes
test CHANGED
@@ -220,6 +220,60 @@
220
220
 
221
221
  }
222
222
 
223
-
224
-
225
223
  ```
224
+
225
+
226
+
227
+ ```ここに言語を入力
228
+
229
+ import UIKit
230
+
231
+ import FirebaseFirestore
232
+
233
+ import FirebaseAuth
234
+
235
+
236
+
237
+ class FoodData: NSObject {
238
+
239
+ var id: String
240
+
241
+ var food: String
242
+
243
+ var number: Int
244
+
245
+ var plice: Int
246
+
247
+ var pliceArray: [Int]
248
+
249
+ var date: Date
250
+
251
+ var dateArray: [Date]
252
+
253
+
254
+
255
+ init(document: QueryDocumentSnapshot) {
256
+
257
+ self.id = document.documentID
258
+
259
+ let foodDic = document.data()
260
+
261
+ self.food = foodDic["food"] as! String
262
+
263
+ self.number = foodDic["number"] as! Int
264
+
265
+ self.plice = foodDic["plice"] as! Int
266
+
267
+ self.pliceArray = [self.plice]
268
+
269
+ let timeStamp = foodDic["date"] as! Timestamp
270
+
271
+ self.date = timeStamp.dateValue()
272
+
273
+ self.dateArray = [self.date]
274
+
275
+ }
276
+
277
+ }
278
+
279
+ ```