Line graph using ios charts swift 3

Bhagyalaxmi Poojary picture Bhagyalaxmi Poojary · Jan 10, 2017 · Viewed 9.2k times · Source

I am using ios chart to implement Line graph in Swift 3 . As in earlier versions of Swift, we had constructor to bind x values to Line graph, but there doesn't seems any such in Swift 3?

Kindly help if anyone has some inputs.

Answer

Sri picture Sri · Jan 10, 2017

Please try this to get your XAxis Values.

import UIKit
import Charts

class LineChartViewController : UIViewController {
    @IBOutlet weak var lineChartView: LineChartView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let populationData :[Int : Double] = [
            1990 : 123456.0,
            2000 : 233456.0,
            2010 : 343456.0
        ]

        let ySeries = populationData.map { x, y in
            return ChartDataEntry(x: Double(x), y: y)
        }

        let data = LineChartData()
        let dataset = LineChartDataSet(values: ySeries, label: "Hello")
        dataset.colors = [NSUIColor.red]
        data.addDataSet(dataset)

        self.lineChartView.data = data

        self.lineChartView.gridBackgroundColor = NSUIColor.white
        self.lineChartView.xAxis.drawGridLinesEnabled = false;
        self.lineChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
        self.lineChartView.chartDescription?.text = "LineChartView Example"
    }

    override open func viewWillAppear(_ animated: Bool) {
        self.lineChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
    }
}

Thanks Sriram