Skip to content

Instantly share code, notes, and snippets.

@jleach
Created Apr 19, 2016
Embed
What would you like to do?
Create a bar chart with iOS Charts.
internal func bodyStateBarChartFromDate(fromDate: NSDate = NSDate(timeIntervalSinceReferenceDate: NSTimeInterval(0)), toDate: NSDate = NSDate(), unit: NSCalendarUnit = .Day) -> BarChartView {
print ("start = \(fromDate), end = \(toDate)")
let chartView = BarChartView()
// Build the chart view data
let data = buildHeartRateDataFromDate(fromDate, toDate: toDate, unit: unit)
if data.count == 0 {
return chartView
}
let chartData: [BarChartDataEntry] = data.map {
// HAX:(jl) There is a problem where if the bar height is 0 then any bars with
// non-zero height float about 10px above the x-axis. This work around solves
// the problem putting in a transparent bar with a height of 10 for zero values.
// Ref. https://github.com/danielgindi/Charts/issues/947
BarChartDataEntry(value: Double($0.averageHeartRate), xIndex: $0.index)
}
var barColors: [UIColor] = []
for d in data {
// if d.averageHeartRate == 0 {
// // HAX:(jl) See HAX comment above.
// barColors += [UIColor.clearColor()]
// continue
// }
switch d.predominantState {
case .Recovery:
barColors += [UIColor.rockyRecoveryColor()]
case .Exercise:
barColors += [UIColor.rockyExerciseColor()]
case .Stress:
barColors += [UIColor.rockyExertionColor()]
default:
()
}
}
// Chart Look and Feel
let xVals: [String] = formatXAxesTimeValues(fromDate.timeUnitsToDate(toDate, unit: .Hour))
// All three arrays need to have the same number of entries
assert(xVals.count == data.count)
assert(xVals.count == barColors.count)
// Create a data set
let barChartDataSet: BarChartDataSet = BarChartDataSet(yVals: chartData, label: "")
barChartDataSet.drawValuesEnabled = false
barChartDataSet.colors = barColors
var barChartDataSets: [BarChartDataSet] = []
barChartDataSets.append(barChartDataSet)
// Config Look and Feel
chartView.descriptionText = ""
chartView.drawGridBackgroundEnabled = false
chartView.drawBordersEnabled = false
chartView.dragEnabled = false
chartView.gridBackgroundColor = UIColor.rockyChartLegendColor()
chartView.pinchZoomEnabled = false
chartView.drawBordersEnabled = false
chartView.scaleXEnabled = true
chartView.scaleYEnabled = false
chartView.highlightPerTapEnabled = false
let legend = chartView.legend
legend.enabled = false
// let xAxis = chartView.xAxis
// xAxis.drawAxisLineEnabled = true
// xAxis.drawLabelsEnabled = true
// xAxis.drawGridLinesEnabled = false
// xAxis.drawLimitLinesBehindDataEnabled = false
// xAxis.labelPosition = .Bottom
// xAxis.labelTextColor = UIColor.rockyChartLegendColor()
// xAxis.avoidFirstLastClippingEnabled = true
let xAxis: ChartXAxis = chartView.xAxis
xAxis.drawAxisLineEnabled = true
xAxis.drawLabelsEnabled = true
xAxis.drawGridLinesEnabled = false
xAxis.labelTextColor = UIColor.rockyChartLegendColor()
xAxis.drawLimitLinesBehindDataEnabled = false
xAxis.labelPosition = .Bottom
let leftAxis = chartView.leftAxis
leftAxis.enabled = false
// let leftAxis = chartView.leftAxis
// leftAxis.drawAxisLineEnabled = false
// leftAxis.drawLabelsEnabled = false
// leftAxis.drawGridLinesEnabled = false
let rightAxis = chartView.rightAxis
rightAxis.drawAxisLineEnabled = false
rightAxis.drawLabelsEnabled = true
rightAxis.drawGridLinesEnabled = false
rightAxis.drawTopYLabelEntryEnabled = true
rightAxis.startAtZeroEnabled = true
rightAxis.labelCount = 6
// rightAxis.axisMaxValue = 120
rightAxis.axisMinValue = 0
rightAxis.valueFormatter = NSNumberFormatter()
rightAxis.valueFormatter!.maximumFractionDigits = 0
rightAxis.labelTextColor = UIColor.rockyChartLegendColor()
// Assign the chart data last or you need to notificy the chart
// the data has changed !!!
chartView.data = BarChartData(xVals: xVals, dataSets: barChartDataSets)
chartView.notifyDataSetChanged()
return chartView
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment