ShinobiCharts中的设定范围

时间:2015-03-10 21:51:34

标签: charts shinobi

我正在尝试在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

1 个答案:

答案 0 :(得分:0)

您可以尝试设置轴的defaultRange吗?然后,您需要重新加载并重绘图表。

e.g。像这样的东西:

@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()
}