ああああああああああああああああああああああああああああああ
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
修正した方がよいと思うところを以下に3つ挙げます。
(1) barChartView
は非表示のため、存在していても問題ないと思いますが、念のため削除しておいた方が安全だと思います。
diff
1 override func viewDidLoad() { 2 super.viewDidLoad() 3- let barChartView = createBarChartView() 4- self.view.addSubview(barChartView) 5- barChartView.translatesAutoresizingMaskIntoConstraints = false 6- barChartView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 80).isActive = true 7- barChartView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -90).isActive = true 8- barChartView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true 9- barChartView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true 10- barChartView.isHidden = true 11 scrollView.frame = CGRect(x: 0,y: 0,width: scrollView.superview!.frame.width,height: scrollView.superview!.frame.height) 12 let contentsView = createContentsView(of: barItems.map({ BarChartModel(value: $0.0, name: $0.1 ) }),barsCountPerPage: 5) 13 scrollView.addSubview(contentsView) 14 scrollView.contentSize = contentsView.frame.size 15 scrollView.isPagingEnabled = true 16 }
(2) createContentsView(of:barsCountPerPage:)
の中で、ページごとに表示するバーの情報を配列 itemsPerPage
にまとめています。forループではページ単位の処理をしているため、forループ内で呼び出す createBarChartView()
に引数を追加し、ページ内で表示するバーについての情報だけを渡すようにします。
diff
1 private func createContentsView(of items: [BarChartModel], barsCountPerPage: Int) -> UIView { 2 let itemsPerPage = stride(from: 0, to: items.count, by: barsCountPerPage).map { Array(items[$0 ..< min($0 + barsCountPerPage, items.count)]) } 3 let contentsView = UIView(frame: CGRect(x: 0, y: 0, width: scrollView.frame.width * CGFloat(itemsPerPage.count), height: scrollView.frame.height)) 4 for (i, items) in itemsPerPage.enumerated() { 5- let barChartView = createBarChartView() 6+ let barChartView = createBarChartView(of: items) 7 let percent = CGFloat(items.count) / CGFloat(itemsPerPage[0].count) 8 barChartView.frame = CGRect(x: scrollView.frame.width * CGFloat(i), y: 0, width: scrollView.frame.width * percent, height:scrollView.frame.height) 9 contentsView.addSubview(barChartView) 10 } 11 return contentsView 12 }
(3) createBarChartView()
の定義も修正します。従来は barItems
プロパティを使っていたために、常に全件のデータを含む BarChartView
オブジェクトを作成して返していましたが、この修正により、引数で受け取ったバー情報だけを含む BarChartView
オブジェクトを返すようになります。
diff
1- func createBarChartView() -> BarChartView { 2+ func createBarChartView(of items: [BarChartModel]) -> BarChartView { 3 let barChartView = BarChartView() 4 barChartView.delegate = self 5 barChartView.animate(yAxisDuration: 1) 6- barChartView.data = createBarChartData(of: barItems.map({BarChartModel(value: $0.0, name: $0.1)})) 7+ barChartView.data = createBarChartData(of: items.map({BarChartModel(value: $0.value, name: $0.name)})) 8 barChartView.xAxis.labelCount = barItems.count 9 barChartView.xAxis.labelPosition = .bottom 10 barChartView.xAxis.drawGridLinesEnabled = false 11 barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: barItems.map({ $0.1 })) 12 barChartView.leftAxis.enabled = false 13 barChartView.rightAxis.enabled = false 14 barChartView.legend.enabled = false 15 barChartView.pinchZoomEnabled = false 16 barChartView.doubleTapToZoomEnabled = false 17 barChartView.leftAxis.axisMaximum = Double(maxVal) + 1 18 return barChartView 19 }
投稿2021/11/07 07:19
総合スコア177
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/11/07 08:29 編集
2021/11/07 08:39
2021/11/07 08:40