简体   繁体   中英

iOS Charts moveViewToX does not seem to work

I am trying to move the current displayed data range. Reading the documents, it seems that I should be able to use moveViewToX function to move the left side of the view to the given x value.

I made a test code that has 1 View with a button. The graph is displayed correctly when the view loads in simulator. When the button is clicked, it calls moveViewToX . However, nothing happens when I click the button (the print text is printed to console when the button is clicked, the graph view remains the same).

import Charts
import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //Setup Test Chart
    self.lineChartView.data = LineChartData(dataSet: lineChartDataSet())

}


fileprivate func lineChartDataSet() -> LineChartDataSet {
    var dataEntries = [ChartDataEntry]()
    dataEntries.append(ChartDataEntry(x: 1, y: 2))
    dataEntries.append(ChartDataEntry(x: 3, y: 5))
    dataEntries.append(ChartDataEntry(x: 5, y: 10))
    dataEntries.append(ChartDataEntry(x: 6, y: 2))
    dataEntries.append(ChartDataEntry(x: 10, y: 15))
    dataEntries.append(ChartDataEntry(x: 12, y: 7))
    dataEntries.append(ChartDataEntry(x: 20, y: 10))
    dataEntries.append(ChartDataEntry(x: 30, y: 15))

    return LineChartDataSet(values: dataEntries, label: "Test Data")
}

@IBAction func buttonClick(_ sender: UIButton) {
    print("Button clicked")
    self.lineChartView.moveViewToX(5)
}
@IBOutlet weak var lineChartView: LineChartView!
}

Make sure the graph has enough values to make it scrollable. If the graph has 8 values and they are all shown on the screen without the need to scroll to the side to see values, it means moveViewToX won´t do anything, since it can´t scroll. You can choose the range for visible points on the graph, and if you set the range as 3 and then scroll to 5, it will work.

self.lineChart.setVisibleXRange(minXRange: 3.0, maxXRange: 3.0)
self.lineChart.moveViewToX(5)

I faced the same problem on the iOS. The moveViewToAnimated worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM