问题
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!
}
回答1:
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)
来源:https://stackoverflow.com/questions/47878844/ios-charts-moveviewtox-does-not-seem-to-work