为什么 moveViewToX() 不会将我的显示移动到条形图中的最后一个值? - Swift

Why moveViewToX() won't move my display to the last value in my bar chart? - Swift

我是 Swift 的新手,我正在尝试创建条形图。我设法创建了一个图表,但出于某种原因 moveViewToX() 无法按预期工作 - 它不会滚动到最后一个 x 值。这是我创建图表的相关函数:

func createBarView() {

       // Set basic parameters

        barChartView.xAxis.labelTextColor = .black
        barChartView.leftAxis.labelTextColor = .black
        barChartView.leftAxis.axisMinimum = 0
        barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
        barChartView.xAxis.labelRotationAngle = 0
        barChartView.xAxis.granularity = 1
        barChartView.xAxis.labelFont = UIFont.systemFont(ofSize: 20.0, weight: .regular)
        barChartView.xAxis.drawGridLinesEnabled = false
        barChartView.rightAxis.drawGridLinesEnabled = false
        barChartView.leftAxis.drawGridLinesEnabled = false
        barChartView.rightAxis.enabled = false
        barChartView.leftAxis.enabled = false
        barChartView.xAxis.labelPosition = .bottom
        barChartView.clipsToBounds = true
        barChartView.layer.cornerRadius = 10
        barChartView.legend.enabled = false
        barChartView.animate(yAxisDuration: 1.3, easingOption: .easeInOutQuart)
        barChartView.scaleYEnabled = false
        barChartView.scaleXEnabled = false
        barChartView.pinchZoomEnabled = false
        barChartView.doubleTapToZoomEnabled = false

//Get X (days) and Y data (calories)

        let count = realm.objects(WorkoutData.self).count
        for i in 0 ..< count {
            let graphDates = realm.objects(WorkoutData.self)[i].date
            allDates.append(graphDates) 
            let calendarDate = Calendar.current.dateComponents([.day, .year, .month], from: graphDates)
            graphDays.append(String(calendarDate.day!)) // Get days array for xAxis

            
            let graphCalories = realm.objects(WorkoutData.self)[i].calories
            visosKalorijos.append(graphCalories) // Get calories array for yAxis
        }

//Supply data for BarChartDataEntry

        var entries = [BarChartDataEntry]()
        for i in 0 ..< count { // dataPoints
            let dataEntry = BarChartDataEntry(x: Double(i), y: Double(visosKalorijos[i]))

        entries.append(dataEntry) //// Get x:y data
        }

//Set labels of xAxis (to show days of the month)

        barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: graphDays)

// Create dataset

        let set = BarChartDataSet(entries: entries, label: "-")

        set.colors = ChartColorTemplates.solid()
        set.valueFont = UIFont.systemFont(ofSize: 16)
        let data = BarChartData(dataSet: set)
        data.barWidth = Double(0.4)
        barChartView.data = data

// Set visibility

        barChartView.setVisibleXRangeMaximum(7)
        barChartView.moveViewToX(Double(count)) 

}

When function is executed I can see only up to 17th day, but there are more days on the xAxis if I scroll.

After view is loaded I need to scroll to the end.

最后一部分 - // 设置可见性是问题所在 - 它不会自动滚动到最后一个 x 值。我究竟做错了什么?感谢您的帮助。

过了一段时间我明白了。最后一步 //设置可见性需要延迟0.1s:

barChartView.setVisibleXRangeMaximum(7)

let when = DispatchTime.now() + 0.1
DispatchQueue.main.asyncAfter(deadline: when) {
    self.barChartView.moveViewToX(Double(self.count))
}

希望这对以后的人有所帮助:)