The charting library "SwiftCharts" can show the "Default" maxValue , midValue and the minValue, but how to hide the midValue "98 " ? please help me, thanks!
I used the demo project on the SwiftCharts:
Code
func initializeChart(cell: MarketPriceCell, index: Int) {
// Initialize data series and labels
var serieData: [Double] = []
var labels: [Double] = []
var labelsAsString: Array<String> = []
let beginInd = 0
let oneFour = stockValuesAll.count / 4
let twoFour = (stockValuesAll.count * 2) / 4
let thrFour = (stockValuesAll.count * 3) / 4
labels = [Double(beginInd), (Double(oneFour)), (Double(twoFour)),(Double(thrFour))]
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
let ddate1 = dateFormatter.string(from: stockValuesAll[beginInd]["date"] as! Date)
let ddate2 = dateFormatter.string(from: stockValuesAll[oneFour]["date"] as! Date)
let ddate3 = dateFormatter.string(from: stockValuesAll[twoFour]["date"] as! Date)
let ddate4 = dateFormatter.string(from: stockValuesAll[thrFour]["date"] as! Date)
labelsAsString = [ddate1,ddate2,ddate3,ddate4]
/// Date formatter to retrieve the month names
for (_, value) in stockValuesAll.enumerated() {
serieData.append(value["close"] as! Double)
}
let series = ChartSeries(serieData)
series.area = true
// Configure chart layout
cell.stkChart.lineWidth = 0.5
cell.stkChart.labelFont = UIFont.systemFont(ofSize: 12)
cell.stkChart.xLabels = labels
cell.stkChart.xLabelsFormatter = { (labelIndex: Int, labelValue: Double) -> String in
return labelsAsString[labelIndex]
}
cell.stkChart.xLabelsTextAlignment = .left
cell.stkChart.minY = serieData.min()! - 5
cell.stkChart.add(series)
Json File:
{
"quotes":
[
{ "date": "2017-07-01", "close": 93.52 },
{ "date": "2017-07-02", "close": 93.48 },
{ "date": "2017-07-03", "close": 94.03 },
{ "date": "2017-07-07", "close": 95.97 }...
The labels for the y-axis are set in the following function:
fileprivate func drawLabelsAndGridOnYAxis() {
let context = UIGraphicsGetCurrentContext()!
context.setStrokeColor(gridColor.cgColor)
context.setLineWidth(0.5)
var labels: [Double]
if yLabels == nil {
labels = [(min.y + max.y) / 2, max.y]
if yLabelsOnRightSide || min.y != 0 {
labels.insert(min.y, at: 0)
}
} else {
labels = yLabels!
}
so you would have to set your own array of yLabels (which contain only min and max) and assign them to your chart.
Another option would be to change the above code and remove the item for the midvalue:
if yLabels == nil {
labels = [max.y]
Related
I'm using the following library to implement charts in my iOS app:
https://github.com/danielgindi/Charts
On the xAxis I would like to have a formatting similar to the one on the screenshot.
I would like to display the name of the month once, followed by days of that month (without month name), and when the months changes, display the name again.
Can someone point to the right direction?
Current code for xAxis configuration, which shows month and day for each entry:
xAxis.drawAxisLineEnabled = true
xAxis.drawGridLinesEnabled = false
xAxis.centerAxisLabelsEnabled = true
xAxis.drawLabelsEnabled = true
xAxis.axisLineColor = R.Color.Chart.Alpha.separator
xAxis.labelTextColor = R.Color.Chart.Alpha.content
xAxis.labelPosition = .bottom
xAxis.labelCount = 5
xAxis.valueFormatter = XAxisValueFormatter()
class XAxisValueFormatter: AxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let date = Date(timeIntervalSince1970: value)
return date.to(DateFormat.monthday)
}
}
You need to update your existing code as below.
Update your data binding for XAxis:
let chartFormatter = LineChartFormatter(labels: xValues)
let xAxis = XAxis()
xAxis.valueFormatter = chartFormatter
self.xAxis.valueFormatter = xAxis.valueFormatter
Update Value Formatter code:
private class LineChartFormatter: NSObject, IAxisValueFormatter {
var labels: [String] = []
let dateFormatter = DateFormatter()
let dateShortFormatter = DateFormatter()
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if let date = dateFormatter.date(from:labels[Int(value)]) {
if value == 0 {
dateShortFormatter.dateFormat = "MMM dd"
return dateShortFormatter.string(from: date)
} else {
let prevDate = dateFormatter.date(from:labels[Int(value - 1)])
dateShortFormatter.dateFormat = "MMM"
if dateShortFormatter.string(from: date) != dateShortFormatter.string(from: prevDate!) {
dateShortFormatter.dateFormat = "MMM dd"
return dateShortFormatter.string(from: date)
} else {
dateShortFormatter.dateFormat = "dd"
return dateShortFormatter.string(from: date)
}
}
} else {
return labels[Int(value)]
}
}
init(labels: [String]) {
super.init()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
self.labels = labels
}}
By Above code change, you will achieve your Date formatting in XAxis.
I am new at Swift the code builds just fine but the greater than (>) dose not work. I'm trying to producing at a number in the "totalCoal" label, but never goes over the "coalPileHolding" Second label. I know that this code can be way better But i am trying to get the basic first. I also know that the timeDiffernt ">" dose not work also so somehow I am missing something. Thank you for your help
#IBOutlet weak var coalPileHoldingLabel: UILabel!
func loadBigCoalPile () {
var coalPileHolding = Int ()
if UserDefaults.standard.object(forKey: "coalPileResearch") == nil {
coalPileHolding = 0 } else {
coalPileHolding = UserDefaults.standard.object(forKey: "coalPileResearch") as! Int}
if coalPileHolding == 1 {
let coalPileHolding = 200
coalPileHoldingLabel.text = String(coalPileHolding) }
if coalPileHolding == 2 {
let coalPileHolding = 300
coalPileHoldingLabel.text = String(coalPileHolding) }
if coalPileHolding == 3 {
let coalPileHolding = 400
coalPileHoldingLabel.text = String(coalPileHolding) }
#objc func buttonIsInAction(){
}
#IBOutlet weak var coalRunButton: UIButton!
#IBAction func coalRunButton(_ sender: Any) {
func getMillisecondsNow() -> Int64{
let currentDate = Date()
return getMillisecondsFromDate(date: currentDate)
}
func getMillisecondsFromDate(date: Date) -> Int64{
var d : Int64 = 0
let interval = date.timeIntervalSince1970
d = Int64(interval * 1000)
return d
}
func getTimeDifferenceFromNowInMilliseconds(time: Int64) -> Int64{
let now = getMillisecondsNow()
let diff: Int64 = now - time
return diff
}
var terminationTime = Int64()
if UserDefaults.standard.object(forKey: "latestTerminationDate") == nil {
terminationTime = getMillisecondsNow()
UserDefaults.standard.set(terminationTime, forKey:"latestTerminationDate")
}
else {
terminationTime = UserDefaults.standard.object(forKey: "latestTerminationDate") as! Int64 }
let timeDiff = getTimeDifferenceFromNowInMilliseconds(time: terminationTime)
let timeDiffernt = Int(timeDiff)
let now = getMillisecondsNow()
UserDefaults.standard.set (now, forKey: "latestTerminationDate")
if timeDiffernt > 86400000 { _ = 86400000}
var methodOfCut = Int ()
var machineryButton = Int ()
var qualityOfWorkers = Int ()
if UserDefaults.standard.object(forKey: "methodOfCut") == nil {
methodOfCut = 0 } else {
methodOfCut = UserDefaults.standard.object(forKey: "methodOfCut") as! Int}
if UserDefaults.standard.object(forKey: "machineryButton") == nil {
machineryButton = 0 } else {
machineryButton = UserDefaults.standard.object(forKey: "machineryButton") as! Int}
if UserDefaults.standard.object(forKey: "qualityOfWorkers") == nil {
qualityOfWorkers = 0 } else {
qualityOfWorkers = UserDefaults.standard.object(forKey: "qualityOfWorkers") as! Int}
let coalMayham = (machineryButton) + (qualityOfWorkers) + (methodOfCut)
let (dailyCoalAccumulate) = ((timeDiffernt) * (coalMayham) + 1) / 10000
var coalPileHolding2 = 0
if let coalPile = Int(coalPileLabel.text!) {
let totalCoal = (dailyCoalAccumulate) + coalPile
coalPileHolding2 = Int(coalPileHoldingLabel.text!) ?? 0
if totalCoal > coalPileHolding2 { coalPileHolding2 = totalCoal }
coalPileLabel.text = String(totalCoal)
UserDefaults.standard.set(totalCoal, forKey:"totalCoal")}
callOutLabel.text = String(dailyCoalAccumulate)}}
That mix of numeric types (Int32, Float, Int) is rather confusing. In general you want to use Int or Double. All other variants should only be used when absolutely necessary, for example if an API requires a different type. So lets assume that dailyCoalAccumulate is Int and switch everything else to Int too:
let coalPileHolding = 0
if let coalPile = Int(coalPileLabel.text!) {
let totalCoal = dailyCoalAccumulate + coalPile
let coalPileHolding = Int((coalPileHoldingLabel.text as! NSString).intValue)
if totalCoal > coalPileHolding {
let coalPileHolding = totalCoal
}
coalPileLabel.text = String(totalCoal)
UserDefaults.standard.set(totalCoal, forKey:"totalCoal")
}
callOutLabel.text = String(dailyCoalAccumulate)
Here the intValue API of NSString returns Int32 but I immediately convert it to a regular Int. But of course there is a better way to do this without having to bridge to the Objective-C NSString. If the string doesn't contain a number intValue simply returns zero. We can produce the same behavior when we use the Int initializer to convert the string and then replace the nil value with zero: Int(coalPileHoldingLabel.text!) ?? 0.
Then we have three different variables named coalPileHolding. Since they are defined in different scopes they can share the same name, but are still different variables. My guess is that you want to actually update the coalPileHolding variable. Otherwise the assignment in the inner if makes no sense - the compiler even warns about that.
So lets change coalPileHolding to var and update its value.
var coalPileHolding = 0
if let coalPile = Int(coalPileLabel.text!) {
let totalCoal = dailyCoalAccumulate + coalPile
coalPileHolding = Int(coalPileHoldingLabel.text!) ?? 0
if totalCoal > coalPileHolding {
coalPileHolding = totalCoal
}
coalPileLabel.text = String(totalCoal)
UserDefaults.standard.set(totalCoal, forKey:"totalCoal")
}
callOutLabel.text = String(dailyCoalAccumulate)
I have found a similar question on here, but the answer did not help me. I think due to my data structure.
I have an array made up of individual arrays, each for its own line in the chart. this is then made up of multiple structs for the plot points of that line.
My issue is that values/lines are correct, but are not aligning correctly with the dates. In the example below. the dates start at 3rd May and end 8th May. Please help
here is my code
struct chartPoint {
let date:String
var total:Double
let exercise:String
}
var sets:[[chartPoint]] = []
func setupLineChart() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var dataSets:[LineChartDataSet] = []
var color:[UIColor] = [UIColor.red,UIColor.blue, UIColor.green,UIColor.red,UIColor.red,UIColor.red,UIColor.red]
for i in sets { //Array of array of structs
let sort = i.sorted { // sort the internal array by date
item1, item2 in
let date1 = dateFormatter.date(from:item1.date)
let date2 = dateFormatter.date(from:item2.date)
return date1!.compare(date2!) == ComparisonResult.orderedAscending
}
var dataEntries: [ChartDataEntry] = []
for stat in 0...(sort.count - 1) {
let date = dateFormatter.date(from:sort[stat].date)
let timeIntervalForDate: TimeInterval = date!.timeIntervalSince1970
let dataEntry = ChartDataEntry(x: Double(timeIntervalForDate), y: sort[stat].total)
dataEntries.append(dataEntry)
if stat == (sort.count - 1){
let chartDataSet = LineChartDataSet(values: dataEntries, label: "\(sort[stat].exercise)")
chartDataSet.setCircleColor(color[stat])
chartDataSet.setColor(color[stat], alpha: 1.0)
chartDataSet.drawValuesEnabled = true
dataSets.append(chartDataSet)
startChart(dataSets: dataSets)
}
}
}
}
func startChart(dataSets:[LineChartDataSet]){
testLineChartView.animate(xAxisDuration: 0.7, yAxisDuration: 0.7)
testLineChartView.dragEnabled = true
testLineChartView.legend.form = .circle
testLineChartView.drawGridBackgroundEnabled = false
let xaxis = testLineChartView.xAxis
xaxis.valueFormatter = axisFormatDelegate
xaxis.labelCount = dataSets.count
xaxis.granularityEnabled = true
xaxis.granularity = 1.0
xaxis.centerAxisLabelsEnabled = true
xaxis.avoidFirstLastClippingEnabled = true
xaxis.drawLimitLinesBehindDataEnabled = true
let rightAxis = testLineChartView.rightAxis
rightAxis.enabled = false
let leftAxis = testLineChartView.leftAxis
leftAxis.drawZeroLineEnabled = true
leftAxis.drawGridLinesEnabled = true
axisFormatDelegate = self
testLineChartView.delegate = self
let chartData = LineChartData(dataSets: dataSets)
testLineChartView.data = chartData
testLineChartView.chartDescription?.text = ""
}
extension ChartViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM"
let date = Date(timeIntervalSince1970: value)
return dateFormatter.string(from: date)
}
}
I am using dates on the x-axis as well in one of my projects and I simply changed each date to a string and passed an array of string values to an IndexAxisValueFormatter.
testLineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xvalues)
If this is not what you are looking for, can you show a sample of what "sets" includes? Then I will be able to run your code.
Here is another thread I started with an issue around a single value not showing. It includes the code that also fixed this.
I think the main point being
xaxis.forceLabelsEnabled = true
iOS Charts - single values not showing Swift
I am having a hard time to migrate library Charts (from Daniel Gindi) from version 2 (Swift 2.3) to 3 (Swift 3).
Basically, I can't have the x labels (dates) aligned correctly with the corresponding plots.
This is what I had before in version 2:
In version 2, I had values for days 7, 8, 10 and 11.
So I was missing a day in the middle, but the labels were correctly alined with the plots.
And here is what I have in version 3:
In version 3, the "labels" in the x axis have now been replaced by double (for dates, it's a timeInterval since 1970), and formatted via a formatter.
So, indeniably, the graph is more "correct" now, since the chart correctly extrapolates the value for the 9th, but I can't find how to put the labels under the corresponding plots.
This is my code for the x axis:
let chartView = LineChartView()
...
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelCount = entries.count
xAxis.drawLabelsEnabled = true
xAxis.drawLimitLinesBehindDataEnabled = true
xAxis.avoidFirstLastClippingEnabled = true
// Set the x values date formatter
let xValuesNumberFormatter = ChartXAxisFormatter()
xValuesNumberFormatter.dateFormatter = dayNumberAndShortNameFormatter // e.g. "wed 26"
xAxis.valueFormatter = xValuesNumberFormatter
Here is the ChartXAxisFormatter class I created:
import Foundation
import Charts
class ChartXAxisFormatter: NSObject {
var dateFormatter: DateFormatter?
}
extension ChartXAxisFormatter: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if let dateFormatter = dateFormatter {
let date = Date(timeIntervalSince1970: value)
return dateFormatter.string(from: date)
}
return ""
}
}
So, the values here are correct, the formatting is correct, the shape of the chart is correct, but the alignment of the labels with the corresponding plots is not good.
Thanks for your help
OK, got it!
You've got to define a reference time Interval (the "0" for the x axis). And then calculate the additional time interval for each x value.
The ChartXAxisFormatter becomes:
import Foundation
import Charts
class ChartXAxisFormatter: NSObject {
fileprivate var dateFormatter: DateFormatter?
fileprivate var referenceTimeInterval: TimeInterval?
convenience init(referenceTimeInterval: TimeInterval, dateFormatter: DateFormatter) {
self.init()
self.referenceTimeInterval = referenceTimeInterval
self.dateFormatter = dateFormatter
}
}
extension ChartXAxisFormatter: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
guard let dateFormatter = dateFormatter,
let referenceTimeInterval = referenceTimeInterval
else {
return ""
}
let date = Date(timeIntervalSince1970: value * 3600 * 24 + referenceTimeInterval)
return dateFormatter.string(from: date)
}
}
And, then, when I create my data entries, it works like so:
// (objects is defined as an array of struct with date and value)
// Define the reference time interval
var referenceTimeInterval: TimeInterval = 0
if let minTimeInterval = (objects.map { $0.date.timeIntervalSince1970 }).min() {
referenceTimeInterval = minTimeInterval
}
// Define chart xValues formatter
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .none
formatter.locale = Locale.current
let xValuesNumberFormatter = ChartXAxisFormatter(referenceTimeInterval: referenceTimeInterval, dateFormatter: formatter)
// Define chart entries
var entries = [ChartDataEntry]()
for object in objects {
let timeInterval = object.date.timeIntervalSince1970
let xValue = (timeInterval - referenceTimeInterval) / (3600 * 24)
let yValue = object.value
let entry = ChartDataEntry(x: xValue, y: yValue)
entries.append(entry)
}
// Pass these entries and the formatter to the Chart ...
The result is much nicer (I removed cubic by the way):
If you exactly know how many labels you need in the x-axis,you can write this code to solve it.For example,If I need seven labels to appear on the x-axis,Then this should be enough.The reason is the chart library is not properly calculating the intervals between the two x-axis points and hence the plot-label mismatch.When we force the library to plot against the given number of labels,The issue seems to be gone.
let xAxis = chart.xAxis
xAxis.centerAxisLabelsEnabled = false
xAxis.setLabelCount(7, force: true) //enter the number of labels here
#IBOutlet weak var tView:UIView!
#IBOutlet weak var lineChartView:LineChartView!{
didSet{
lineChartView.xAxis.labelPosition = .bottom
lineChartView.xAxis.granularityEnabled = true
lineChartView.xAxis.granularity = 1.0
let xAxis = lineChartView.xAxis
// xAxis.axisMinimum = 0.0
// xAxis.granularity = 1.0
// xaAxis.setLabelCount(6, force: true)
}
}
#IBOutlet weak var back: UIButton?
#IBAction func back(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
self.lineChartView.delegate = self
self.lineChartView.chartDescription?.textColor = UIColor.white
let months = ["Jan" , "Feb", "Mar"]
let dollars1 = [1453.0,2352,5431]
setChart(months, values: dollars1)
}
func setChart(_ dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0 ..< dataPoints.count {
dataEntries.append(ChartDataEntry(x: Double(i), y: values[i]))
}
let lineChartDataSet = LineChartDataSet(entries: dataEntries, label: nil)
lineChartDataSet.axisDependency = .left
lineChartDataSet.setColor(UIColor.black)
lineChartDataSet.setCircleColor(UIColor.black) // our circle will be dark red
lineChartDataSet.lineWidth = 1.0
lineChartDataSet.circleRadius = 3.0 // the radius of the node circle
lineChartDataSet.fillAlpha = 1
lineChartDataSet.fillColor = UIColor.black
lineChartDataSet.highlightColor = UIColor.white
lineChartDataSet.drawCircleHoleEnabled = true
var dataSets = [LineChartDataSet]()
dataSets.append(lineChartDataSet)
let lineChartData = LineChartData(dataSets: dataSets)
lineChartView.data = lineChartData
lineChartView.rightAxis.enabled = false
lineChartView.xAxis.drawGridLinesEnabled = false
lineChartView.xAxis.labelPosition = .bottom
lineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
}
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
print(entry)
}
I solved this issue using this answer
https://stackoverflow.com/a/44554613/2087608
I suspected that these offsets come from adjusting X axis value to a specific time of the day in my case
Here is my code
for i in 0..<valuesViewModel.entries.count {
let dataEntry = ChartDataEntry(x: roundDate(date: valuesViewModel.entries[i].date).timeIntervalSince1970, y: valuesViewModel.entries[i].value)
dataEntries.append(dataEntry)
}
func roundDate(date: Date) -> Date {
var comp: DateComponents = Calendar.current.dateComponents([.year, .month, .day], from: date)
comp.timeZone = TimeZone(abbreviation: "UTC")!
let truncated = Calendar.current.date(from: comp)!
return truncated
}
How we can set UIDatePicker values to show only Sunday's date in swift iOS ?
finally, I myself found solution. In didSelectRow Method check if the selected day is sunday??? if yes then ok but if not then reload component to select date of nearest sunday.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
pickerView.reloadComponent(1)
}
let titleLabel = pickerView.viewForRow(row, forComponent: component) as? UILabel
titleLabel?.font = UIFont(name: BCGConstants.Fonts.Name.ProximaNovaBold, size: 27)!
var dayValue = pickerView.selectedRowInComponent(1) + 1
let monthValue = pickerView.selectedRowInComponent(0) + 1
var yearValue = 0
let unitFlags: NSCalendarUnit = [.Day, .Month, .Year, .Weekday]
let currentDateComponents = NSCalendar.currentCalendar().components(unitFlags, fromDate: NSDate())
if monthValue > currentDateComponents.month || (dayValue >= currentDateComponents.day && monthValue == currentDateComponents.month ) {
yearValue = currentDateComponents.year
} else {
yearValue = currentDateComponents.year + 1
}
debugPrint("\(self.isGivenDaySunday(dayValue, selectedMonth: monthValue, selectedYear: yearValue)) day = \(dayValue) month = \(monthValue) )")
let sundayCheck = self.isGivenDaySunday(pickerView.selectedRowInComponent(1) + 1, selectedMonth: pickerView.selectedRowInComponent(0) + 1, selectedYear: yearValue)
if sundayCheck.isSunday {
self.startDateTextField.text = sundayCheck.sundayDate?.fullStyleDateString
self.newBootcamp?.startDate = sundayCheck.sundayDate!
} else {
// titleLabel?.font = UIFont(name: BCGConstants.Fonts.Name.ProximaNovaBold, size: 27)!
// titleLabel?.textColor = UIColor.lightGrayColor()
if dayValue > 15 {
dayValue = pickerView.selectedRowInComponent(1) - (7 - sundayCheck.nextSundayAsWeekDay)
pickerView.selectRow(dayValue, inComponent: 1, animated: true)
} else {
dayValue = pickerView.selectedRowInComponent(1) + sundayCheck.nextSundayAsWeekDay
pickerView.selectRow(dayValue, inComponent: 1, animated: true)
}
var confirmSunday = self.isGivenDaySunday(dayValue + 1, selectedMonth: monthValue, selectedYear: yearValue)
// Added by mohsin : Reason bug : selecting previous day
if confirmSunday.sundayDate?.isLessThanDate(NSDate()) == true {
confirmSunday = self.isGivenDaySunday(dayValue, selectedMonth: monthValue, selectedYear: yearValue + 1)
//TODO: Need to be verify again : If not working fine then you must try to change next commented statement and uncomment it
// dayValue = pickerView.selectedRowInComponent(1) + sundayCheck.nextSundayAsWeekDay
pickerView.selectRow(dayValue - 1, inComponent: 1, animated: true)
}
self.startDateTextField.text = confirmSunday.sundayDate?.fullStyleDateString
self.newBootcamp?.startDate = confirmSunday.sundayDate!
debugPrint(confirmSunday.sundayDate?.fullStyleDateString)
}
}
Method which checks sunday is following one
func isGivenDaySunday(selectedDay: Int, selectedMonth: Int, selectedYear: Int) -> (isSunday: Bool, nextSundayAsWeekDay: Int, sundayDate: NSDate?) {
let unitFlags: NSCalendarUnit = [.Day, .Month, .Year, .Weekday]
let selectedDateComponents = NSDateComponents()
selectedDateComponents.month = selectedMonth
selectedDateComponents.day = selectedDay
selectedDateComponents.year = selectedYear
let selectedDate = NSCalendar(identifier: NSCalendarIdentifierGregorian)?.dateFromComponents(selectedDateComponents)
let newSelectedDateComponent = NSCalendar.currentCalendar().components(unitFlags, fromDate: selectedDate!)
if newSelectedDateComponent.weekday == 1 { // 1 means SUNDAY as per Gregorian
return (true, 0, selectedDate)
} else {
return (false, 8 - newSelectedDateComponent.weekday, nil)
}
}