我正在尝试在ShinobiChart中设置NSDateTimeAxis
的范围,但是出了点问题,我的代码如下所示:
func segmentedControlTapped(sc:UISegmentedControl){
let toDateString:String = "2015-01-05"
let dateFormatter = NSDateFormatter()
dateFormatter.formatterBehavior = NSDateFormatterBehavior.Behavior10_4
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
let toDate = dateFormatter.dateFromString(toDateString)
switch (sc.selectedSegmentIndex){
case 0:
println("10 years")
let fromDateString:String = "2005-01-03"
let fromDate = dateFormatter.dateFromString(fromDateString)
self.xAxis.setRangeWithMinimum(fromDate, andMaximum: toDate)
case 1:
println("5 years")
let fromDate:String = "2010-01-05"
let d = dateFormatter.dateFromString(fromDate)
self.xAxis.setRangeWithMinimum(fromDate, andMaximum: toDate)
case 2:
println("3 years")
let fromDate:String = "2012-01-05"
let d = dateFormatter.dateFromString(fromDate)
self.xAxis.setRangeWithMinimum(fromDate, andMaximum: toDate)
case 3:
println("1 year")
let fromDate:String = "2014-01-05"
let d = dateFormatter.dateFromString(fromDate)
self.xAxis.setRangeWithMinimum(fromDate, andMaximum: toDate)
default:
println("something went terribly wrong")
}
}
我知道我使用的日期存在于我的 xAxis 上,但它仍然不允许我更改范围。正确的方法是什么?
任何建议将不胜感激。
编辑:
我也尝试过这种方式,但它给了我错误说:
无法分配给此表达式的结果。
xAxis 不是使用 let
声明的,所以这对我来说仍然是一个谜。
case 0:
println("10 years")
let fromDateString:String = "2005-01-03"
let fromDate = dateFormatter.dateFromString(fromDateString)
var dateRange = SChartDateRange(dateMinimum: fromDate, andDateMaximum: toDate)
let range = SChartDateRange(dateMinimum: fromDate, andDateMaximum: toDate)
self.xAxis.axisRange.minimum = range.minimum
self.xAxis.axisRange.maximum = range.maximum
您可以尝试设置轴的defaultRange
吗?然后,您需要重新加载并重新绘制图表。
例如,像这样:
@IBAction func segmentedControlValueChanges(sender: UISegmentedControl) {
var interval: NSTimeInterval?
switch (sender.selectedSegmentIndex) {
case 0: interval = 60 * 60 * 24
case 1: interval = 60 * 60 * 24 * 7
default: println("Error")
}
let endDate = startDate.dateByAddingTimeInterval(interval!)
chart.xAxis.defaultRange = SChartDateRange(dateMinimum: startDate, andDateMaximum: endDate)
chart.reloadData()
chart.redrawChart()
}