0

我正在尝试NSDateTimeAxis在 ShinobiChart 中设置我的范围,但是出了点问题,我的代码如下所示:

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
4

1 回答 1

0

你可以尝试设置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()
}
于 2015-03-20T12:27:03.510 回答