質問編集履歴

2

あああああ

2021/11/13 05:22

投稿

rararara
rararara

スコア11

test CHANGED
@@ -1 +1 @@
1
- chartsを使った棒グラフ
1
+ あああああああああああああああああ
test CHANGED
@@ -1,241 +1 @@
1
- https://bombrary.github.io/blog/posts/iphone-barchart-ioscharts/
2
-
3
- を参考に棒グラフを作成しています。ただ、このサイトをそのままコピペしてもタップイベントがサイト通りにいきません。
4
-
5
- サイトは、5刻みで画面表示されていますが、配列の要素全てが1画面に表示され、タップして横スクロールすると同じ画面が表示されてしまいます。
6
-
7
-
8
-
9
- viewdidload()のlet contentsView = createContentsView(of: barItems.map({ BarChartModel(value: $0.0, name: $0.1 ) }),barsCountPerPage: 5)によって、createContentsView関数が呼ばれています。createContentsView関数がやっていることは、配列の要素数を5刻みでページをスクロールできるようにしています。なので、コード上は、サイト通りにいくと思うのですが、思い通りに行きません。
10
-
11
- 何が間違っているのか、何が足りないのか教えて下さい。また、createContentsView関数の認識に間違いがあれば教えて下さい。
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 value: Int
28
-
29
- let name: String
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 foodData: FoodData!
46
-
47
-
48
-
49
- let barItems = [
50
-
51
- (7, "太郎"), (1, "次郎"), (2, "三郎"), (6, "四郎"), (3, "五郎"),
52
-
53
- (9, "六郎"), (2, "七郎"), (3, "八郎"), (1, "九郎"), (5, "十郎"),
54
-
55
- (1, "十一郎"), (1, "十二郎"), (6, "十三郎")
56
-
57
- ]
58
-
59
-
60
-
61
- override func viewDidLoad() {
62
-
63
- super.viewDidLoad()
64
-
65
- let barChartView = createBarChartView()
66
-
67
- self.view.addSubview(barChartView)
68
-
69
- barChartView.translatesAutoresizingMaskIntoConstraints = false
70
-
71
- barChartView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 80).isActive = true
72
-
73
- barChartView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -90).isActive = true
74
-
75
- barChartView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
76
-
77
- barChartView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
78
-
79
- barChartView.isHidden = true
80
-
81
- scrollView.frame = CGRect(x: 0,y: 0,width: scrollView.superview!.frame.width,height: scrollView.superview!.frame.height)
82
-
83
- let contentsView = createContentsView(of: barItems.map({ BarChartModel(value: $0.0, name: $0.1 ) }),barsCountPerPage: 5)
84
-
85
- scrollView.addSubview(contentsView)
86
-
87
- scrollView.contentSize = contentsView.frame.size
88
-
89
- scrollView.isPagingEnabled = true
90
-
91
- }
92
-
93
-
94
-
95
- func createBarChartView() -> BarChartView {
96
-
97
- let barChartView = BarChartView()
98
-
99
- barChartView.delegate = self
100
-
101
- barChartView.animate(yAxisDuration: 1)
102
-
103
- barChartView.data = createBarChartData(of: barItems.map({BarChartModel(value: $0.0, name: $0.1)}))
104
-
105
- barChartView.xAxis.labelCount = barItems.count
106
-
107
- barChartView.xAxis.labelPosition = .bottom
108
-
109
- barChartView.xAxis.drawGridLinesEnabled = false
110
-
111
- barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: barItems.map({ $0.1 }))
112
-
113
- barChartView.leftAxis.enabled = false
114
-
115
- barChartView.rightAxis.enabled = false
116
-
117
- barChartView.legend.enabled = false
118
-
119
- barChartView.pinchZoomEnabled = false
120
-
121
- barChartView.doubleTapToZoomEnabled = false
122
-
123
- barChartView.leftAxis.axisMaximum = Double(maxVal) + 1
124
-
125
- return barChartView
126
-
127
- }
128
-
129
-
130
-
131
- private func createBarChartData(of items: [BarChartModel]) -> BarChartData {
132
-
133
- let entries: [BarChartDataEntry] = items.enumerated().map {
134
-
135
- let (i, item) = $0
136
-
137
- return BarChartDataEntry(x: Double(i), y: Double(item.value))
138
-
139
- }
140
-
141
- let barChartDataSet = BarChartDataSet(entries: entries, label: "Label")
142
-
143
- barChartDataSet.valueFont = .systemFont(ofSize: 20)
144
-
145
- barChartDataSet.valueFormatter = ValueFormatter(of: items)
146
-
147
- barChartDataSet.colors = items.map { $0.value == maxVal ? .systemOrange : .systemBlue }
148
-
149
- let barChartData = BarChartData(dataSet: barChartDataSet)
150
-
151
- return barChartData
152
-
153
- }
154
-
155
-
156
-
157
- private func createContentsView(of items: [BarChartModel], barsCountPerPage: Int) -> UIView {
158
-
159
- let itemsPerPage = stride(from: 0, to: items.count, by: barsCountPerPage).map { Array(items[$0 ..< min($0 + barsCountPerPage, items.count)]) }
160
-
161
- let contentsView = UIView(frame: CGRect(x: 0, y: 0, width: scrollView.frame.width * CGFloat(itemsPerPage.count), height: scrollView.frame.height))
162
-
163
- for (i, items) in itemsPerPage.enumerated() {
164
-
165
- let barChartView = createBarChartView()
166
-
167
- let percent = CGFloat(items.count) / CGFloat(itemsPerPage[0].count)
168
-
169
- barChartView.frame = CGRect(x: scrollView.frame.width * CGFloat(i), y: 0, width: scrollView.frame.width * percent, height:scrollView.frame.height)
170
-
171
- contentsView.addSubview(barChartView)
172
-
173
- }
174
-
175
- return contentsView
176
-
177
- }
178
-
179
- }
180
-
181
-
182
-
183
- extension ChartViewController: ChartViewDelegate {
184
-
185
- func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
186
-
187
- let axisFormatter = chartView.xAxis.valueFormatter!
188
-
189
- let label = axisFormatter.stringForValue(entry.x, axis: nil)
190
-
191
- print(label, entry.y)
192
-
193
- }
194
-
195
- }
196
-
197
-
198
-
199
- class ValueFormatter: IValueFormatter {
200
-
201
- let items: [BarChartModel]
202
-
203
- init(of items: [BarChartModel]) {
204
-
205
- self.items = items
206
-
207
- }
208
-
209
- func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
210
-
211
- return "(Int(value))"
212
-
213
- }
214
-
215
- }
216
-
217
-
218
-
219
- class XAxisFormatter: IAxisValueFormatter {
220
-
221
- let items: [BarChartModel]
222
-
223
- init(of items: [BarChartModel]) {
224
-
225
- self.items = items
226
-
227
- }
228
-
229
- func stringForValue(_ value: Double, axis: AxisBase?) -> String {
230
-
231
- let index = Int(value)
232
-
233
- return self.items[index].name
234
-
235
- }
236
-
237
- }
238
-
239
-
240
-
241
- ```
1
+ ああああああああああああああああああああああああああああああ

1

書き忘れ

2021/11/13 05:22

投稿

rararara
rararara

スコア11

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,10 @@
1
1
  https://bombrary.github.io/blog/posts/iphone-barchart-ioscharts/
2
2
 
3
- を参考に棒グラフを作成しています。ただ、このサイトをそのままコピペしてもタップイベントが上手くかないので質問さて下さい
3
+ を参考に棒グラフを作成しています。ただ、このサイトをそのままコピペしてもタップイベントがサイト通りにきま
4
+
5
+ サイトは、5刻みで画面表示されていますが、配列の要素全てが1画面に表示され、タップして横スクロールすると同じ画面が表示されてしまいます。
6
+
7
+
4
8
 
5
9
  viewdidload()のlet contentsView = createContentsView(of: barItems.map({ BarChartModel(value: $0.0, name: $0.1 ) }),barsCountPerPage: 5)によって、createContentsView関数が呼ばれています。createContentsView関数がやっていることは、配列の要素数を5刻みでページをスクロールできるようにしています。なので、コード上は、サイト通りにいくと思うのですが、思い通りに行きません。
6
10