图表'折线图不能正确渲染折线图

时间:2018-04-12 13:47:18

标签: swift ios-charts

我正在使用图表框架,并且我在折线图中遇到了一些非常奇怪的行为。

当我转到ChartViewContoller并且默认选择包含数据时,图表会正常呈现:

1rm_normal

但如果我在默认选择没有任何数据且然后选择具有数据的项目时转向此视图控制器,它看起来像这样:

1)对此:

1rm_nodatamessage

2)然后选择一个包含数据的项目:

1rm_squished

当我切换到视图控制器时,当然viewDidLoad被调用,只要默认选择在我转到它时有数据,我可以选择另一个有数据或不具有数据的项目图表将继续正确呈现。所以差异似乎出现在viewDidLoad中,但我已经尝试了所有我能想到的但没有解决问题的方法。这是我的viewDidLoad

 override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor(hexString: "232B35")

    self.title = "1RM"

    chartView.delegate = self
    chartView.chartDescription?.enabled = false

    let leftAxis = chartView.leftAxis
    leftAxis.axisMinimum = 190
    leftAxis.labelTextColor = NSUIColor.white

    let xAxis = chartView.xAxis
    xAxis.labelPosition = .bottom
    xAxis.axisMinimum = 0
    xAxis.granularity = 1
    xAxis.axisLineWidth = 5
    xAxis.valueFormatter = self
    xAxis.labelTextColor = NSUIColor.white


    chartView.configureDefaults()
    chartView.rightAxis.enabled = false // this fixed the extra xAxis grid lines

    chartView.backgroundColor = NSUIColor(red: 35/255.0, green: 43/255.0, blue: 53/255.0, alpha: 1.0)

    fetchData()

    chartView.setVisibleXRangeMaximum(7)

    chartView.animate(yAxisDuration: 1.0)
}

这里发生了fetchData()

中发生的事情
func fetchData() {

    chartView.data = nil

    let liftName = UserDefaults.selectedLiftForChart()
    let liftEvents = dataManager.fetchLiftsEventsOfTypeByName(liftName)

    guard liftEvents.count > 0 else {
        chartView.noDataText = "There's no \(liftName) data to display"
        shouldHideData = true

        return }

    // put them into a Dictionary grouped by each unique day
    let groupedEvents = Dictionary(grouping: liftEvents, by: { floor($0.date.timeIntervalSince1970 / 86400) })

    // grab the maximum 1RM from each day
    let dailyMaximums = groupedEvents.map { $1.max(by: { $0.oneRepMax < $1.oneRepMax }) }

    // MARK: - TODO: Fix the silly unwrapping
    sortedLiftEvents = dailyMaximums.sorted(by: { $0?.date.compare(($1?.date)!) == .orderedAscending }) as! [LiftEvent]

    let intervalBetweenDates: TimeInterval = 3600 * 24 // 3600 = 1 hour
    let startDate = (sortedLiftEvents.first?.date)! - intervalBetweenDates
    let lastDate = sortedLiftEvents.last?.date

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MMM d"
    let dates:[Date] = intervalDates(from: startDate, to: lastDate!, with: intervalBetweenDates)

    days = dates.map {dateFormatter.string(from: $0)}

    generateLineData()

}

最后,这是generateLineData方法:

func fetchData() {

    chartView.data = nil

    let liftName = UserDefaults.selectedLiftForChart()
    let liftEvents = dataManager.fetchLiftsEventsOfTypeByName(liftName)

    guard liftEvents.count > 0 else {
        chartView.noDataText = "There's no \(liftName) data to display"
        shouldHideData = true

        return }

    // put them into a Dictionary grouped by each unique day
    let groupedEvents = Dictionary(grouping: liftEvents, by: { floor($0.date.timeIntervalSince1970 / 86400) })

    // grab the maximum 1RM from each day
    let dailyMaximums = groupedEvents.map { $1.max(by: { $0.oneRepMax < $1.oneRepMax }) }

    // MARK: - TODO: Fix the silly unwrapping
    sortedLiftEvents = dailyMaximums.sorted(by: { $0?.date.compare(($1?.date)!) == .orderedAscending }) as! [LiftEvent]

    let intervalBetweenDates: TimeInterval = 3600 * 24 // 3600 = 1 hour
    let startDate = (sortedLiftEvents.first?.date)! - intervalBetweenDates
    let lastDate = sortedLiftEvents.last?.date

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MMM d"
    let dates:[Date] = intervalDates(from: startDate, to: lastDate!, with: intervalBetweenDates)

    days = dates.map {dateFormatter.string(from: $0)}

    generateLineData()

}

我已尝试将chartView.setVisibleXRangeMaximum(7)放入设置图表数据的方法中,并在每次呈现图表时验证chartView.visibleXRange为7,但它并没有产生影响。我还确保在为图表设置数据后设置最大XRange

还有什么我可以尝试的,或者这可能是一个尚未修复的错误?

由于

1 个答案:

答案 0 :(得分:1)

好吧,我终于明白了。我从阅读文档中了解到,在将图表数据传递给图表之后,必须设置一些属性。不完全清楚哪些属性,但通过大量的调试和消除过程,我确定只要数据发生变化就需要重置xAxis属性。

现在,当数据发生变化时,我会调用我的新函数:

func resetxAxis() {
    let xAxis = chartView.xAxis
    xAxis.labelPosition = .bottom
    xAxis.axisMinimum = 0
    xAxis.granularity = 1
    xAxis.axisLineWidth = 5
    xAxis.valueFormatter = self
}

这是我的viewDidLoad方法,因此我使用了上述方法,并且可以在需要时随时调用它。