Search code examples
iosswiftios-charts

iOS Chart errors due to no documentation


I want to include Charts on a section of my side project. I struggle to get their demo working. I do not know Objective C so I've converted their code to Swift. Im using Swift 3:

viewDidLoad():

self.title = "Filled Line Chart"
    self.chartView.delegate = self // after build: Thread 1:EXC_BAD_ACCESS (code=2, address=0x……)
    self.chartView.backgroundColor = UIColor.white
    self.chartView.gridBackgroundColor = UIColor(red: CGFloat(51 / 255.0), green: CGFloat(181 / 255.0), blue: CGFloat(229 / 255.0), alpha: CGFloat(150 / 255.0))
    self.chartView.drawGridBackgroundEnabled = true
    self.chartView.drawBordersEnabled = true
    self.chartView.chartDescription.isEnabled = false
    self.chartView.pinchZoomEnabled = false
    self.chartView.dragEnabled = true
    chartView.scaleEnabled = true
    var l = chartView.legend
    l.isEnabled = false
    var xAxis = chartView.xAxis
    xAxis.isEnabled = false
    var leftAxis = chartView.leftAxis
    leftAxis.axisMaximum = 900.0
    leftAxis.axisMinimum = -250.0
    leftAxis.drawAxisLineEnabled = false

Since I dont know what to do from there, I dragged a line from my view to the code to create an IBOutlet: @IBOutlet var chartView: BarChartView!

In all it looks like this:

import UIKit
import Charts

class DashboardVC: UIViewController, ChartViewDelegate {

  @IBOutlet var chartView: BarChartView!

  override func viewDidLoad() {
    super.viewDidLoad()

self.title = "Filled Line Chart"
        self.chartView.delegate = self
        self.chartView.backgroundColor = UIColor.white
        self.chartView.gridBackgroundColor = UIColor(red: CGFloat(51 / 255.0), green: CGFloat(181 / 255.0), blue: CGFloat(229 / 255.0), alpha: CGFloat(150 / 255.0))
        self.chartView.drawGridBackgroundEnabled = true
        self.chartView.drawBordersEnabled = true
        self.chartView.chartDescription?.isEnabled = false
        self.chartView.pinchZoomEnabled = false
        self.chartView.dragEnabled = true
        self.chartView.scaleXEnabled = true
        var l = chartView.legend
        l.isEnabled = false
        var xAxis = chartView.xAxis
        xAxis.isEnabled = false
        var leftAxis = chartView.leftAxis
        leftAxis.axisMaximum = 900.0
        leftAxis.axisMinimum = -250.0
        leftAxis.drawAxisLineEnabled = false
  }
}

Error I got with above:

enter image description here

The aim was to add a chart in a small container on my view but just to see of this moule works, I select the entire view to create the outlet. I see not documentation. I've looked on youtube for videos, searched for tutorials for this module but nothing. Spent 2hrs looking and so far no good. Would appreciate any little input.

Thanks.

EDIT:

As per comments/answer, Ive changed isEnabled to enable. Only one error, Thread 1:EXC_BAD_ACCESS. The fix brought me to this.

This simple question gets longer as when I made the change but selecting Charts for Module, I get the above error which points to my custom segue:

func goTodashboard() {

        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let dc: DashboardVC = storyboard.instantiateViewController(withIdentifier: "dashboardVC") as! DashboardVC

        self.present(dc, animated: true, completion: nil)

    }

Basically, that code is called in viewDidAppear() from loginViewController. When user logs in, it goes to the dashboard. Is that the correct way to switch view? It works ok but the Charts having an issue with it.


Solution

  • To narrow down the solution from my comments you can find the official documentation for the Charts framework here

    the isEnabled-Methods are getter, the corresponding setter method is enabled

    instead of using a custom segue-function try to use the build-in prepare-method, you can call it programmatically with performSegue(withIdentifier: "Your Identifier", sender: /*login-button*/) after you validated the login as correct

    make sure you set the right class to the UIView that displays your chart (in storyboard select the view and go to the identity inspector > enter 'BarChartView' in the Class property and 'Charts' in the Module property)

    after everything works, you need to provide data to your chart:

    let me give you an example

    let data = Utils().getData()               
    var dataEntries: [ChartDataEntry] = []
    
    for i in 0..<data.count
    {
      let dataEntry = ChartDataEntry(x:Double(data[i].xValue), y:Double(data[i].yValue))
      dataEntries.append(dataEntry)
    }
    
    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Label")  
    let chartData = BarChartData(dataSets: [chartDataSet])            
    self.barChart.data = chartData
    

    everything should be set up and working now