如何在 iOS 条形图中根据工作日设置数据
How to Setup Data According to weekdays in iOS Bar Chart
我正在做一个计步器项目,我必须在其中实现存储当天步数并根据日期名称将其附加到 ios 条形图数据的功能。基本上,我想在 ios 条形图上表示每个工作日的总步数。如果我得到一些示例代码或任何提示以根据日期名称附加每日步骤,这将非常有帮助。
这是我的代码:-
import UIKit
import Charts
enum WeekDayName: String {
case Sunday = "Sun"
case Monday = "Mon"
case Tuesday = "Tue"
case Wednesday = "Wed"
case Thursday = "Thu"
case Friday = "Fri"
case Saturday = "Sat"
}
class HistoryViewController: UIViewController,ChartViewDelegate {
@IBOutlet weak var barChartView: BarChartView!
var todayName:String!
let week = ["Sun", "Mon", "Tue", "Wed","Thu", "Fri", "Sat"]
// let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
let staticSteps = [200, 400, 600, 300, 1200, 1600, 400]
override func viewDidLoad() {
super.viewDidLoad()
getCurrentDayName()
setup(barLineChartView: barChartView)
}
//MARK: Get Current Day Name
func getCurrentDayName(){
let weekday = Calendar.current.component(.weekday, from: Date())
print(weekday)
switch weekday {
case 1:
todayName = WeekDayName.Sunday.rawValue
case 2:
todayName = WeekDayName.Monday.rawValue
case 3:
todayName = WeekDayName.Tuesday.rawValue
case 4:
todayName = WeekDayName.Wednesday.rawValue
case 5:
todayName = WeekDayName.Thursday.rawValue
case 6:
todayName = WeekDayName.Friday.rawValue
case 7:
todayName = WeekDayName.Saturday.rawValue
default:
print("nothng")
}
print(todayName)
}
func setup(barLineChartView chartView: BarChartView) {
chartView.chartDescription?.enabled = false
chartView.isUserInteractionEnabled = false //Disables the selection for bar charts
chartView.dragEnabled = true
chartView.setScaleEnabled(false) //turn off scaling /kinda pinch zoom type
chartView.pinchZoomEnabled = false //disables pinch zoom
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = .systemFont(ofSize: 10)
//hides grid
xAxis.drawGridLinesEnabled = false
chartView.xAxis.valueFormatter = WeekValueFormatter()
xAxis.labelCount = 7
chartView.animate(yAxisDuration: 3)
chartView.rightAxis.enabled = false
let leftAxisFormatter = NumberFormatter()
leftAxisFormatter.minimumFractionDigits = 0
leftAxisFormatter.maximumFractionDigits = 1
let leftAxis = chartView.leftAxis
leftAxis.labelFont = .systemFont(ofSize: 10)
leftAxis.labelCount = 8
leftAxis.valueFormatter = DefaultAxisValueFormatter(formatter: leftAxisFormatter)
leftAxis.labelPosition = .outsideChart
leftAxis.spaceTop = 0.15
leftAxis.axisMinimum = 0 // FIXME: HUH?? this replaces startAtZero = YES
let l = chartView.legend
l.horizontalAlignment = .left
l.verticalAlignment = .bottom
l.orientation = .horizontal
l.drawInside = false
l.form = .circle
l.formSize = 9
l.font = UIFont(name: "HelveticaNeue-Light", size: 11)!
l.xEntrySpace = 4
//setChart(dataPoints: months, values: unitsSold.map { Double([=11=]) })
setChart(dataPoints: week, values: staticSteps)
}
func setChart(dataPoints: [String], values: [Int]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(values[i]))
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "BeWell Workout Chart")
chartDataSet.colors = ChartColorTemplates.colorful()
let chartData = BarChartData(dataSet: chartDataSet)
barChartView.data = chartData
}
//MARK: Weeks Bottom Label Class for x-Axis labels
public class WeekValueFormatter: NSObject, IAxisValueFormatter {
override init() {
super.init()
}
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let weekday = Calendar.current.component(.weekday, from: Date())
let week = ["Sun", "Mon", "Tue", "Wed","Thu", "Fri", "Sat"]
return week[Int(value)]
}
}
假设
- 您的一周一次只有 1 周,因此条形图中没有日期字符串重复并且
- 星期几 星期中的字符串保持有序(从不随机排序)...
您可以为您的条形图的数据集创建一个新的 Int,它具有过去的工作日和今天的值,未来几天的值为 0,如下所示:
func createNewArray() -> [Int] {
var stepsThisWeek = [Int]()
// use todays day to find the index of where it appears in var week=[String]()
if let todayIndex = week.firstIndex(of: getCurrentDayName()) {
// create indices range of 0 to todayIndex
let daysSoFarIndices = [Int](0...todayIndex)
// create new array of var staticSteps=[Int]() at daysSoFarIndices
stepsThisWeek = daysSoFarIndices.map { staticSteps[[=10=]] }
//create indices from todayIndex +1 to lastIndex of var staticSteps=[Int]()
let daysLaterInWeek = [Int](todayIndex..<staticSteps.endIndex)
//add 0 values for each daysLaterInWeek remaining
for _ in daysLaterInWeek {
stepsThisWeek.append(0)
}
}
return stepsThisWeek
}
然后您可以调用此方法来设置条形图的值,例如:
setChart(dataPoints: week, values: createNewArray())
Today is Monday, so your bar chart will appear like this.
我正在做一个计步器项目,我必须在其中实现存储当天步数并根据日期名称将其附加到 ios 条形图数据的功能。基本上,我想在 ios 条形图上表示每个工作日的总步数。如果我得到一些示例代码或任何提示以根据日期名称附加每日步骤,这将非常有帮助。
这是我的代码:-
import UIKit
import Charts
enum WeekDayName: String {
case Sunday = "Sun"
case Monday = "Mon"
case Tuesday = "Tue"
case Wednesday = "Wed"
case Thursday = "Thu"
case Friday = "Fri"
case Saturday = "Sat"
}
class HistoryViewController: UIViewController,ChartViewDelegate {
@IBOutlet weak var barChartView: BarChartView!
var todayName:String!
let week = ["Sun", "Mon", "Tue", "Wed","Thu", "Fri", "Sat"]
// let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
let staticSteps = [200, 400, 600, 300, 1200, 1600, 400]
override func viewDidLoad() {
super.viewDidLoad()
getCurrentDayName()
setup(barLineChartView: barChartView)
}
//MARK: Get Current Day Name
func getCurrentDayName(){
let weekday = Calendar.current.component(.weekday, from: Date())
print(weekday)
switch weekday {
case 1:
todayName = WeekDayName.Sunday.rawValue
case 2:
todayName = WeekDayName.Monday.rawValue
case 3:
todayName = WeekDayName.Tuesday.rawValue
case 4:
todayName = WeekDayName.Wednesday.rawValue
case 5:
todayName = WeekDayName.Thursday.rawValue
case 6:
todayName = WeekDayName.Friday.rawValue
case 7:
todayName = WeekDayName.Saturday.rawValue
default:
print("nothng")
}
print(todayName)
}
func setup(barLineChartView chartView: BarChartView) {
chartView.chartDescription?.enabled = false
chartView.isUserInteractionEnabled = false //Disables the selection for bar charts
chartView.dragEnabled = true
chartView.setScaleEnabled(false) //turn off scaling /kinda pinch zoom type
chartView.pinchZoomEnabled = false //disables pinch zoom
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = .systemFont(ofSize: 10)
//hides grid
xAxis.drawGridLinesEnabled = false
chartView.xAxis.valueFormatter = WeekValueFormatter()
xAxis.labelCount = 7
chartView.animate(yAxisDuration: 3)
chartView.rightAxis.enabled = false
let leftAxisFormatter = NumberFormatter()
leftAxisFormatter.minimumFractionDigits = 0
leftAxisFormatter.maximumFractionDigits = 1
let leftAxis = chartView.leftAxis
leftAxis.labelFont = .systemFont(ofSize: 10)
leftAxis.labelCount = 8
leftAxis.valueFormatter = DefaultAxisValueFormatter(formatter: leftAxisFormatter)
leftAxis.labelPosition = .outsideChart
leftAxis.spaceTop = 0.15
leftAxis.axisMinimum = 0 // FIXME: HUH?? this replaces startAtZero = YES
let l = chartView.legend
l.horizontalAlignment = .left
l.verticalAlignment = .bottom
l.orientation = .horizontal
l.drawInside = false
l.form = .circle
l.formSize = 9
l.font = UIFont(name: "HelveticaNeue-Light", size: 11)!
l.xEntrySpace = 4
//setChart(dataPoints: months, values: unitsSold.map { Double([=11=]) })
setChart(dataPoints: week, values: staticSteps)
}
func setChart(dataPoints: [String], values: [Int]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(values[i]))
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "BeWell Workout Chart")
chartDataSet.colors = ChartColorTemplates.colorful()
let chartData = BarChartData(dataSet: chartDataSet)
barChartView.data = chartData
}
//MARK: Weeks Bottom Label Class for x-Axis labels
public class WeekValueFormatter: NSObject, IAxisValueFormatter {
override init() {
super.init()
}
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let weekday = Calendar.current.component(.weekday, from: Date())
let week = ["Sun", "Mon", "Tue", "Wed","Thu", "Fri", "Sat"]
return week[Int(value)]
}
}
假设
- 您的一周一次只有 1 周,因此条形图中没有日期字符串重复并且
- 星期几 星期中的字符串保持有序(从不随机排序)...
您可以为您的条形图的数据集创建一个新的 Int,它具有过去的工作日和今天的值,未来几天的值为 0,如下所示:
func createNewArray() -> [Int] {
var stepsThisWeek = [Int]()
// use todays day to find the index of where it appears in var week=[String]()
if let todayIndex = week.firstIndex(of: getCurrentDayName()) {
// create indices range of 0 to todayIndex
let daysSoFarIndices = [Int](0...todayIndex)
// create new array of var staticSteps=[Int]() at daysSoFarIndices
stepsThisWeek = daysSoFarIndices.map { staticSteps[[=10=]] }
//create indices from todayIndex +1 to lastIndex of var staticSteps=[Int]()
let daysLaterInWeek = [Int](todayIndex..<staticSteps.endIndex)
//add 0 values for each daysLaterInWeek remaining
for _ in daysLaterInWeek {
stepsThisWeek.append(0)
}
}
return stepsThisWeek
}
然后您可以调用此方法来设置条形图的值,例如:
setChart(dataPoints: week, values: createNewArray())
Today is Monday, so your bar chart will appear like this.