2

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!
}
Y Y
  • 25
  • 5

3 Answers3

3

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)
DevB2F
  • 4,674
  • 4
  • 36
  • 60
  • Thank you. That does solve it. The document is very misleading for this function then: "Moves the left side of the current viewport to the specified x-value. This also refreshes the chart by calling setNeedsDisplay()." That description make it sound like it should just move the left end of the viewport to the given value, which should not depend on if it can fit the number of points or not. – Y Y Dec 20 '17 at 04:12
0

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

4b0
  • 21,981
  • 30
  • 95
  • 142
DzungPV
  • 1,561
  • 3
  • 18
  • 24
0

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!
ack
  • 289
  • 2
  • 12