Fetching core data from table view cells for plotting charts

Viewed 45

I am trying to create a health app containing clinical assessment questionnaires for users, to track their symptoms: fatigue, pain and mood.
I am using core data model to save the inputs from the questionnaire into a table view. Now I want to plot those inputs into a graph separately for each symptom. When I tried plotting it only the latest entry from table view can be fetched from the core data and plotted.

How can I get the data from every single table view cell?

Code used for fetching Core Data:

    var lineChart = LineChartView()
override func viewDidLoad() {
    fetchday1()
    super.viewDidLoad()
    lineChart.delegate = self
}
func fetchday1()
    {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "SDA")
        do {
            let results:NSArray = try context.fetch(request) as NSArray
            for result in results {
                let sda = result as! SDA
                SDAList.append(sda)
        
                S1 = sda.value(forKey: "sda1") as! String
                S2 = sda.value(forKey: "sda2") as! String
                S3 = sda.value(forKey: "sda3") as! String
            }
        } catch {
            print("Failed")
        }
    }

Code used for plotting:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    
    lineChart.frame = CGRect(x:0, y:0, width: self.view.frame.size.width, height: self.view.frame.size.width)
    
    lineChart.center = view.center
    view.addSubview(lineChart)
    
    var entries = [ChartDataEntry]()
    
    let Int1 = Int(S3) ?? 0
    let Int2 = Int(S2) ?? 0
    let Int3 = Int(S3) ?? 0
    
    entries.append(ChartDataEntry(x: 1.0, y: Double(Int1)))
    entries.append(ChartDataEntry(x: 2.0, y: Double(Int1)))
    entries.append(ChartDataEntry(x: 3.0, y: Double(Int1)))
}

UI of the table view

.xcdatamodel file

Responses for one table view cell

0 Answers
Related