Swiftのグラフを作成するchartsライブラリでX軸に曜日、Y軸に歩数を表示する棒グラフを作成しました。X軸はIAxisValueFormatter を使ってSunからSatまで表示させる([Sun,Mon,Tue,Wed,Thu,Fri,Sat])ことができたのですが、例えば今日が水曜日なら[Thu,Fri,Sat,Sun,Mon,Tue,Wed]の順番に表示させたいのですが、やり方が分からず困っております。どなたかご教示いただけると大変助かります。
開発環境:Xcode version 12.5 MacOS Big Sur version 11.2.3
ソースコード:
class XAxisValueFormatter: NSObject, IAxisValueFormatter {
let week = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] func stringForValue(_ value: Double, axis: AxisBase?) -> String { return week[Int(value)] }
}
func setUpBarChart(values:[Step]){ self.barChartView.frame = CGRect(x: 20, y: 450, width: self.view.frame.width - 40, height: (self.view.frame.width - 40)/2) barChartView.backgroundColor = .clear barChartView.xAxis.labelPosition = .bottom barChartView.xAxis.labelTextColor = .systemBlue barChartView.xAxis.drawGridLinesEnabled = false barChartView.xAxis.drawAxisLineEnabled = false barChartView.xAxis.labelCount = 7 barChartView.xAxis.valueFormatter = XAxisValueFormatter() barChartView.rightAxis.enabled = false barChartView.leftAxis.enabled = true barChartView.leftAxis.labelTextColor = .systemBlue barChartView.leftAxis.drawAxisLineEnabled = false barChartView.legend.enabled = false barChartView.animate(xAxisDuration: 1) var entry = [BarChartDataEntry]() for i in 0..<values.count { let weekday = Calendar.current.component(.weekday, from: values[i].date) entry.append(BarChartDataEntry(x:Double(weekday-1), y:Double(values[i].count))) weeklyAverage += values[i].count } weeklyAverage = Int(weeklyAverage / 7) aveLine = ChartLimitLine(limit: Double(weeklyAverage)) aveLine.lineColor = .systemBlue aveLine.lineDashLengths = [4] barChartView.leftAxis.addLimitLine(aveLine) weeklyAverage = 0 let dataSet = BarChartDataSet(entries: entry, label:"") dataSet.valueTextColor = .blue barChartView.data = BarChartData(dataSet: dataSet) scrollView.addSubview(barChartView) }
あなたの回答
tips
プレビュー