質問編集履歴
2
ああああああ
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
c
|
1
|
+
cあああああああああああああああああ
|
test
CHANGED
@@ -1,235 +1 @@
|
|
1
|
-
https://bombrary.github.io/blog/posts/iphone-barchart-ioscharts/
|
2
|
-
|
3
|
-
を参考にして、棒グラフを作成しています。
|
4
|
-
|
5
|
-
以下コードが、サイトに記述されている全てのコードをまとめたものです。
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
createContentsViewメソッドで、let barChartView = createBarChartView(of: items)が記述されているかと思いますが、
|
10
|
-
|
11
|
-
このままだとエラーが発生するので、let barChartView = createBarChartView()のように修正をかけるとエラーがなくなります。
|
12
|
-
|
13
|
-
ただ、実行してみると、同じ棒グラフが重なって見えるような形になってしまいます。
|
14
|
-
|
15
|
-
この行で何をやりたいのかが見えないため、修正することが出来ません。
|
16
|
-
|
17
|
-
let barChartView = createBarChartView(of: items)ではなく、何を書けばいいのか教えて欲しいです。
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
```
|
22
|
-
|
23
|
-
struct BarChartModel {
|
24
|
-
|
25
|
-
let value: Int
|
26
|
-
|
27
|
-
let name: String
|
28
|
-
|
29
|
-
}
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
class ChartViewController: UIViewController {
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
@IBOutlet weak var scrollView: UIScrollView!
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
lazy var maxVal: Int = barItems.map({ $0.0 }).max()!
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
let barItems = [
|
46
|
-
|
47
|
-
(7, "太郎"), (1, "次郎"), (2, "三郎"), (6, "四郎"), (3, "五郎"),
|
48
|
-
|
49
|
-
(9, "六郎"), (2, "七郎"), (3, "八郎"), (1, "九郎"), (5, "十郎"),
|
50
|
-
|
51
|
-
(1, "十一郎"), (1, "十二郎"), (6, "十三郎")
|
52
|
-
|
53
|
-
]
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
override func viewDidLoad() {
|
58
|
-
|
59
|
-
super.viewDidLoad()
|
60
|
-
|
61
|
-
let barChartView = createBarChartView()
|
62
|
-
|
63
|
-
self.view.addSubview(barChartView)
|
64
|
-
|
65
|
-
barChartView.translatesAutoresizingMaskIntoConstraints = false
|
66
|
-
|
67
|
-
barChartView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 80).isActive = true
|
68
|
-
|
69
|
-
barChartView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -90).isActive = true
|
70
|
-
|
71
|
-
barChartView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
|
72
|
-
|
73
|
-
barChartView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
|
74
|
-
|
75
|
-
scrollView.frame = CGRect(x: 0,y: 0,width: scrollView.superview!.frame.width,height: scrollView.superview!.frame.height)
|
76
|
-
|
77
|
-
let contentsView = createContentsView(of: barItems.map({ BarChartModel(value: $0.0, name: $0.1 ) }),barsCountPerPage: 5)
|
78
|
-
|
79
|
-
|
1
|
+
あああああああああああああああああああああああああああああああああああ
|
80
|
-
|
81
|
-
scrollView.contentSize = contentsView.frame.size
|
82
|
-
|
83
|
-
scrollView.isPagingEnabled = true
|
84
|
-
|
85
|
-
}
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
func createBarChartView() -> BarChartView {
|
90
|
-
|
91
|
-
let barChartView = BarChartView()
|
92
|
-
|
93
|
-
//barChartView.delegate = self
|
94
|
-
|
95
|
-
barChartView.animate(yAxisDuration: 1)
|
96
|
-
|
97
|
-
barChartView.data = createBarChartData(of: barItems.map({BarChartModel(value: $0.0, name: $0.1)}))
|
98
|
-
|
99
|
-
barChartView.xAxis.labelCount = barItems.count
|
100
|
-
|
101
|
-
barChartView.xAxis.labelPosition = .bottom
|
102
|
-
|
103
|
-
barChartView.xAxis.drawGridLinesEnabled = false
|
104
|
-
|
105
|
-
barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: barItems.map({ $0.1 }))
|
106
|
-
|
107
|
-
barChartView.leftAxis.enabled = false
|
108
|
-
|
109
|
-
barChartView.rightAxis.enabled = false
|
110
|
-
|
111
|
-
barChartView.legend.enabled = false
|
112
|
-
|
113
|
-
barChartView.pinchZoomEnabled = false
|
114
|
-
|
115
|
-
barChartView.doubleTapToZoomEnabled = false
|
116
|
-
|
117
|
-
barChartView.leftAxis.axisMaximum = Double(maxVal) + 1
|
118
|
-
|
119
|
-
return barChartView
|
120
|
-
|
121
|
-
}
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
private func createBarChartData(of items: [BarChartModel]) -> BarChartData {
|
126
|
-
|
127
|
-
let entries: [BarChartDataEntry] = items.enumerated().map {
|
128
|
-
|
129
|
-
let (i, item) = $0
|
130
|
-
|
131
|
-
return BarChartDataEntry(x: Double(i), y: Double(item.value))
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
let barChartDataSet = BarChartDataSet(entries: entries, label: "Label")
|
136
|
-
|
137
|
-
barChartDataSet.valueFont = .systemFont(ofSize: 20)
|
138
|
-
|
139
|
-
barChartDataSet.valueFormatter = ValueFormatter(of: items)
|
140
|
-
|
141
|
-
barChartDataSet.colors = items.map { $0.value == maxVal ? .systemOrange : .systemBlue }
|
142
|
-
|
143
|
-
let barChartData = BarChartData(dataSet: barChartDataSet)
|
144
|
-
|
145
|
-
return barChartData
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
private func createContentsView(of items: [BarChartModel], barsCountPerPage: Int) -> UIView {
|
152
|
-
|
153
|
-
let itemsPerPage = stride(from: 0, to: items.count, by: barsCountPerPage).map { Array(items[$0 ..< min($0 + barsCountPerPage, items.count)]) }
|
154
|
-
|
155
|
-
let contentsView = UIView(frame: CGRect(x: 0, y: 0, width: scrollView.frame.width * CGFloat(itemsPerPage.count), height: scrollView.frame.height))
|
156
|
-
|
157
|
-
for (i, items) in itemsPerPage.enumerated() {
|
158
|
-
|
159
|
-
let barChartView = createBarChartView(of: items)
|
160
|
-
|
161
|
-
let percent = CGFloat(items.count) / CGFloat(itemsPerPage[0].count)
|
162
|
-
|
163
|
-
barChartView.frame = CGRect(x: scrollView.frame.width * CGFloat(i), y: 0, width: scrollView.frame.width * percent, height:scrollView.frame.height)
|
164
|
-
|
165
|
-
contentsView.addSubview(barChartView)
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
return contentsView
|
170
|
-
|
171
|
-
}
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
//extension ChartViewController: ChartViewDelegate {
|
178
|
-
|
179
|
-
// func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
|
180
|
-
|
181
|
-
// let axisFormatter = chartView.xAxis.valueFormatter!
|
182
|
-
|
183
|
-
// let label = axisFormatter.stringForValue(entry.x, axis: nil)
|
184
|
-
|
185
|
-
// print(label, entry.y)
|
186
|
-
|
187
|
-
// }
|
188
|
-
|
189
|
-
//}
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
class ValueFormatter: IValueFormatter {
|
194
|
-
|
195
|
-
let items: [BarChartModel]
|
196
|
-
|
197
|
-
init(of items: [BarChartModel]) {
|
198
|
-
|
199
|
-
self.items = items
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
|
204
|
-
|
205
|
-
return "(Int(value))"
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
class XAxisFormatter: IAxisValueFormatter {
|
214
|
-
|
215
|
-
let items: [BarChartModel]
|
216
|
-
|
217
|
-
init(of items: [BarChartModel]) {
|
218
|
-
|
219
|
-
self.items = items
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
|
224
|
-
|
225
|
-
let index = Int(value)
|
226
|
-
|
227
|
-
return self.items[index].name
|
228
|
-
|
229
|
-
}
|
230
|
-
|
231
|
-
}
|
232
|
-
|
233
|
-
コード
|
234
|
-
|
235
|
-
```
|
1
書き忘れ
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,8 +40,6 @@
|
|
40
40
|
|
41
41
|
lazy var maxVal: Int = barItems.map({ $0.0 }).max()!
|
42
42
|
|
43
|
-
var foodData: FoodData!
|
44
|
-
|
45
43
|
|
46
44
|
|
47
45
|
let barItems = [
|
@@ -174,6 +172,64 @@
|
|
174
172
|
|
175
173
|
}
|
176
174
|
|
175
|
+
|
176
|
+
|
177
|
+
//extension ChartViewController: ChartViewDelegate {
|
178
|
+
|
179
|
+
// func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
|
180
|
+
|
181
|
+
// let axisFormatter = chartView.xAxis.valueFormatter!
|
182
|
+
|
183
|
+
// let label = axisFormatter.stringForValue(entry.x, axis: nil)
|
184
|
+
|
185
|
+
// print(label, entry.y)
|
186
|
+
|
187
|
+
// }
|
188
|
+
|
189
|
+
//}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
class ValueFormatter: IValueFormatter {
|
194
|
+
|
195
|
+
let items: [BarChartModel]
|
196
|
+
|
197
|
+
init(of items: [BarChartModel]) {
|
198
|
+
|
199
|
+
self.items = items
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
|
204
|
+
|
205
|
+
return "(Int(value))"
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
class XAxisFormatter: IAxisValueFormatter {
|
214
|
+
|
215
|
+
let items: [BarChartModel]
|
216
|
+
|
217
|
+
init(of items: [BarChartModel]) {
|
218
|
+
|
219
|
+
self.items = items
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
|
224
|
+
|
225
|
+
let index = Int(value)
|
226
|
+
|
227
|
+
return self.items[index].name
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
}
|
232
|
+
|
177
233
|
コード
|
178
234
|
|
179
235
|
```
|