2

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.

Bhagyalaxmi Poojary
  • 1,213
  • 1
  • 12
  • 17

4 Answers4

5

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

Sri
  • 1,317
  • 9
  • 13
3

Please care to read the migration notes in Release Notes

  • All dataset constructors have changed - they do not take an array of x-indices anymore.
  • All entry constructors have changed - they take in an X and a Y.

Basically, the x values are now taken from the entries.

If you want to change which values are displayed on the x-axis, you can change xAxis.axisMinimum and xAxis.axisMaximum and format them using xAxis.valueFormatter.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
1

Swift 3:

Example with barChart :

import UIKit
import Charts

class StatistiqueSafetyCheckViewController: UIViewController {

    @IBOutlet weak var lineChartView: LineChartView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let ys1 = Array(1..<10).map { x in return sin(Double(x) / 2.0 / 3.141 * 1.5) }
        let ys2 = Array(1..<10).map { x in return cos(Double(x) / 2.0 / 3.141) }

        let yse1 = ys1.enumerated().map { x, y in return LineChartDataEntry(x: Double(x), y: y) }
        let yse2 = ys2.enumerated().map { x, y in return LineChartDataEntry(x: Double(x), y: y) }

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

        let ds2 = LineChartDataSet(values: yse2, label: "World")
        ds2.colors = [NSUIColor.blue]
        data.addDataSet(ds2)

        self.lineChartView.data = data
        self.lineChartView.gridBackgroundColor = NSUIColor.white
        self.lineChartView.chartDescription?.text = "Linechart Demo"
    }

    override open func viewWillAppear(_ animated: Bool) {
        self.lineChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
    }
}
Insou
  • 1,303
  • 1
  • 13
  • 17
0

To change also circle ticket color:

 ds1.circleColors = [NSUIColor.blue]
 ds2.circleColors = [NSUIColor.red]
Alessandro Mattiuzzi
  • 2,309
  • 2
  • 18
  • 24