Y Y
Y Y

Reputation: 25

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!
}

Upvotes: 2

Views: 1586

Answers (3)

ack
ack

Reputation: 289

For me - the order was important, otherwise the moveViewToX would not have any effect. This order seemed to ensure moveViewToX worked in shifting the graph the the correct location (I am using timeSince1970 for my x values).

    lineChartView.xAxis.axisMinimum = startGraphEpoch
    lineChartView.xAxis.axisMaximum = endGraphEpoch
    lineChartView.setVisibleXRange(minXRange: 3600*24, maxXRange: 3600*24)
    lineChartView.data = data
    lineChartView.moveViewToX(startTodayEpoch) //works!

Upvotes: 0

DzungPV
DzungPV

Reputation: 1891

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

Upvotes: 0

DevB2F
DevB2F

Reputation: 5095

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)

Upvotes: 3

Related Questions