質問編集履歴
2
ああああああ
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
cあああああああああああああああああ
|
body
CHANGED
@@ -1,118 +1,1 @@
|
|
1
|
-
https://bombrary.github.io/blog/posts/iphone-barchart-ioscharts/
|
2
|
-
を参考にして、棒グラフを作成しています。
|
3
|
-
以下コードが、サイトに記述されている全てのコードをまとめたものです。
|
4
|
-
|
5
|
-
createContentsViewメソッドで、let barChartView = createBarChartView(of: items)が記述されているかと思いますが、
|
6
|
-
このままだとエラーが発生するので、let barChartView = createBarChartView()のように修正をかけるとエラーがなくなります。
|
7
|
-
ただ、実行してみると、同じ棒グラフが重なって見えるような形になってしまいます。
|
8
|
-
この行で何をやりたいのかが見えないため、修正することが出来ません。
|
9
|
-
let barChartView = createBarChartView(of: items)ではなく、何を書けばいいのか教えて欲しいです。
|
10
|
-
|
11
|
-
```
|
12
|
-
struct BarChartModel {
|
13
|
-
let value: Int
|
14
|
-
let name: String
|
15
|
-
}
|
16
|
-
|
17
|
-
class ChartViewController: UIViewController {
|
18
|
-
|
19
|
-
@IBOutlet weak var scrollView: UIScrollView!
|
20
|
-
|
21
|
-
lazy var maxVal: Int = barItems.map({ $0.0 }).max()!
|
22
|
-
|
23
|
-
let barItems = [
|
24
|
-
(7, "太郎"), (1, "次郎"), (2, "三郎"), (6, "四郎"), (3, "五郎"),
|
25
|
-
(9, "六郎"), (2, "七郎"), (3, "八郎"), (1, "九郎"), (5, "十郎"),
|
26
|
-
(1, "十一郎"), (1, "十二郎"), (6, "十三郎")
|
27
|
-
]
|
28
|
-
|
29
|
-
override func viewDidLoad() {
|
30
|
-
super.viewDidLoad()
|
31
|
-
let barChartView = createBarChartView()
|
32
|
-
self.view.addSubview(barChartView)
|
33
|
-
barChartView.translatesAutoresizingMaskIntoConstraints = false
|
34
|
-
barChartView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 80).isActive = true
|
35
|
-
barChartView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -90).isActive = true
|
36
|
-
barChartView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
|
37
|
-
barChartView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
|
38
|
-
scrollView.frame = CGRect(x: 0,y: 0,width: scrollView.superview!.frame.width,height: scrollView.superview!.frame.height)
|
39
|
-
let contentsView = createContentsView(of: barItems.map({ BarChartModel(value: $0.0, name: $0.1 ) }),barsCountPerPage: 5)
|
40
|
-
|
1
|
+
あああああああああああああああああああああああああああああああああああ
|
41
|
-
scrollView.contentSize = contentsView.frame.size
|
42
|
-
scrollView.isPagingEnabled = true
|
43
|
-
}
|
44
|
-
|
45
|
-
func createBarChartView() -> BarChartView {
|
46
|
-
let barChartView = BarChartView()
|
47
|
-
//barChartView.delegate = self
|
48
|
-
barChartView.animate(yAxisDuration: 1)
|
49
|
-
barChartView.data = createBarChartData(of: barItems.map({BarChartModel(value: $0.0, name: $0.1)}))
|
50
|
-
barChartView.xAxis.labelCount = barItems.count
|
51
|
-
barChartView.xAxis.labelPosition = .bottom
|
52
|
-
barChartView.xAxis.drawGridLinesEnabled = false
|
53
|
-
barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: barItems.map({ $0.1 }))
|
54
|
-
barChartView.leftAxis.enabled = false
|
55
|
-
barChartView.rightAxis.enabled = false
|
56
|
-
barChartView.legend.enabled = false
|
57
|
-
barChartView.pinchZoomEnabled = false
|
58
|
-
barChartView.doubleTapToZoomEnabled = false
|
59
|
-
barChartView.leftAxis.axisMaximum = Double(maxVal) + 1
|
60
|
-
return barChartView
|
61
|
-
}
|
62
|
-
|
63
|
-
private func createBarChartData(of items: [BarChartModel]) -> BarChartData {
|
64
|
-
let entries: [BarChartDataEntry] = items.enumerated().map {
|
65
|
-
let (i, item) = $0
|
66
|
-
return BarChartDataEntry(x: Double(i), y: Double(item.value))
|
67
|
-
}
|
68
|
-
let barChartDataSet = BarChartDataSet(entries: entries, label: "Label")
|
69
|
-
barChartDataSet.valueFont = .systemFont(ofSize: 20)
|
70
|
-
barChartDataSet.valueFormatter = ValueFormatter(of: items)
|
71
|
-
barChartDataSet.colors = items.map { $0.value == maxVal ? .systemOrange : .systemBlue }
|
72
|
-
let barChartData = BarChartData(dataSet: barChartDataSet)
|
73
|
-
return barChartData
|
74
|
-
}
|
75
|
-
|
76
|
-
private func createContentsView(of items: [BarChartModel], barsCountPerPage: Int) -> UIView {
|
77
|
-
let itemsPerPage = stride(from: 0, to: items.count, by: barsCountPerPage).map { Array(items[$0 ..< min($0 + barsCountPerPage, items.count)]) }
|
78
|
-
let contentsView = UIView(frame: CGRect(x: 0, y: 0, width: scrollView.frame.width * CGFloat(itemsPerPage.count), height: scrollView.frame.height))
|
79
|
-
for (i, items) in itemsPerPage.enumerated() {
|
80
|
-
let barChartView = createBarChartView(of: items)
|
81
|
-
let percent = CGFloat(items.count) / CGFloat(itemsPerPage[0].count)
|
82
|
-
barChartView.frame = CGRect(x: scrollView.frame.width * CGFloat(i), y: 0, width: scrollView.frame.width * percent, height:scrollView.frame.height)
|
83
|
-
contentsView.addSubview(barChartView)
|
84
|
-
}
|
85
|
-
return contentsView
|
86
|
-
}
|
87
|
-
}
|
88
|
-
|
89
|
-
//extension ChartViewController: ChartViewDelegate {
|
90
|
-
// func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
|
91
|
-
// let axisFormatter = chartView.xAxis.valueFormatter!
|
92
|
-
// let label = axisFormatter.stringForValue(entry.x, axis: nil)
|
93
|
-
// print(label, entry.y)
|
94
|
-
// }
|
95
|
-
//}
|
96
|
-
|
97
|
-
class ValueFormatter: IValueFormatter {
|
98
|
-
let items: [BarChartModel]
|
99
|
-
init(of items: [BarChartModel]) {
|
100
|
-
self.items = items
|
101
|
-
}
|
102
|
-
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
|
103
|
-
return "(Int(value))"
|
104
|
-
}
|
105
|
-
}
|
106
|
-
|
107
|
-
class XAxisFormatter: IAxisValueFormatter {
|
108
|
-
let items: [BarChartModel]
|
109
|
-
init(of items: [BarChartModel]) {
|
110
|
-
self.items = items
|
111
|
-
}
|
112
|
-
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
|
113
|
-
let index = Int(value)
|
114
|
-
return self.items[index].name
|
115
|
-
}
|
116
|
-
}
|
117
|
-
コード
|
118
|
-
```
|
1
書き忘れ
title
CHANGED
File without changes
|
body
CHANGED
@@ -19,7 +19,6 @@
|
|
19
19
|
@IBOutlet weak var scrollView: UIScrollView!
|
20
20
|
|
21
21
|
lazy var maxVal: Int = barItems.map({ $0.0 }).max()!
|
22
|
-
var foodData: FoodData!
|
23
22
|
|
24
23
|
let barItems = [
|
25
24
|
(7, "太郎"), (1, "次郎"), (2, "三郎"), (6, "四郎"), (3, "五郎"),
|
@@ -86,5 +85,34 @@
|
|
86
85
|
return contentsView
|
87
86
|
}
|
88
87
|
}
|
88
|
+
|
89
|
+
//extension ChartViewController: ChartViewDelegate {
|
90
|
+
// func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
|
91
|
+
// let axisFormatter = chartView.xAxis.valueFormatter!
|
92
|
+
// let label = axisFormatter.stringForValue(entry.x, axis: nil)
|
93
|
+
// print(label, entry.y)
|
94
|
+
// }
|
95
|
+
//}
|
96
|
+
|
97
|
+
class ValueFormatter: IValueFormatter {
|
98
|
+
let items: [BarChartModel]
|
99
|
+
init(of items: [BarChartModel]) {
|
100
|
+
self.items = items
|
101
|
+
}
|
102
|
+
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
|
103
|
+
return "(Int(value))"
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
class XAxisFormatter: IAxisValueFormatter {
|
108
|
+
let items: [BarChartModel]
|
109
|
+
init(of items: [BarChartModel]) {
|
110
|
+
self.items = items
|
111
|
+
}
|
112
|
+
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
|
113
|
+
let index = Int(value)
|
114
|
+
return self.items[index].name
|
115
|
+
}
|
116
|
+
}
|
89
117
|
コード
|
90
118
|
```
|