Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jleach
Created April 19, 2016 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jleach/ef831910e5dd16a785361533df73113e to your computer and use it in GitHub Desktop.
Save jleach/ef831910e5dd16a785361533df73113e to your computer and use it in GitHub Desktop.
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