Display Calculated Data in SwiftUI Charts ...

I am trying to discover how to display my application’s calculated Solar Information values in a chart.

  1. My application identifies a selected location in MapKit.
  2. The application identifies the location’s longitude, latitude, and current time of day.
  3. The application calculates the selected location’s NOAA [SOLAR ELEVATION], and the [SOLAR AZIMUTH] for the time of day.
  4. The application calculates the data, then stores the calculated values as a [Plist] file within my application’s Document Directory.

For the moment, complete with repeated scouring of the Internet, I am not sure how to properly convert, transfer, or create a Structure, required by the chart to display the calculated values. I would like to create the chart once the calculations are complete, but I introduced a Plist to store the calculations for future use, too.

The calculated values coincide with the NOAA Solar Calculations, complete to the displayed [h : m : s], whereas I also designed the application to create the [Array of Dictionary Objects] to store the calculated values for each subsequent six minute interval, until the end of the selected location’s day. The calculated values are properly appended to the [Array of Dictionary Objects] after each completed calculation, with data transfer constants. There are 240 calculations per day from [00:06:00 to 23:54:00], presented as a [STRING], complete with the [Elevation] presented as a [DOUBLE].

For example :: The application generates the following [Calculated Array of Dictionary Objects], then recreates, and appends a new Plist in the Document Directory.

mySolarElevationDataArrayOfDictionaries :: [(theRequiredTimeOfDay: "00:06:00", theCalculatedElevation: -62.60301082991259), (theRequiredTimeOfDay: "00:12:00", theCalculatedElevation: -62.94818095051292), (theRequiredTimeOfDay: "00:18:00", theCalculatedElevation: -63.245198186807215), (theRequiredTimeOfDay: "00:24:00", theCalculatedElevation: -63.49236786176319), (theRequiredTimeOfDay: "00:30:00", theCalculatedElevation: -63.688223890934175), (theRequiredTimeOfDay: "00:36:00", theCalculatedElevation: -63.831564163806945), (theRequiredTimeOfDay: "00:42:00", theCalculatedElevation: -63.921486675739004), (theRequiredTimeOfDay: "00:48:00", theCalculatedElevation: -63.95741610687708), to the end of the data :: ===> (theRequiredTimeOfDay: "23:54:00", theCalculatedElevation: -60.69355458181633)]

The application presents the initial data as follows ::

Then presents a compass view to illustrate the results ::

I modified the Chart’s [MOCK DATA] from the calculated values to test the Chart’s display in a [SwiftUI Hosting Controller].

For example :: The following Chart Mock Data in a [HourlySunElevation_MockChartData.swift] file is called by the application’s [Content View].

import Foundation

struct Value {

    let theRequiredTimeOfDay: String
    let theCalculatedElevation: Double
    
    static func theSunElevationMockData() -> [Value] {
        
        return [Value(theRequiredTimeOfDay: "00:06:00", theCalculatedElevation: -62.60301082991259), Value(theRequiredTimeOfDay: "00:12:00", theCalculatedElevation: -62.94818095051292), Value(theRequiredTimeOfDay: "00:18:00", theCalculatedElevation: -63.245198186807215), Value(theRequiredTimeOfDay: "00:24:00", theCalculatedElevation: -63.49236786176319), Value(theRequiredTimeOfDay: "00:30:00", theCalculatedElevation: -63.688223890934175), Value(theRequiredTimeOfDay: "00:36:00", theCalculatedElevation: -63.831564163806945), Value(theRequiredTimeOfDay: "00:42:00", theCalculatedElevation: -63.921486675739004), Value(theRequiredTimeOfDay: "00:48:00", theCalculatedElevation: -63.95741610687708), to the end of the data :: ===> Value(theRequiredTimeOfDay: "23:54:00", theCalculatedElevation: -60.69355458181633)]

The Chart illustrates the Mock Data as follows ::

I also created a Struct within the [MySunElevationChart_ViewController] to try to append the calculated data, using the same logic with the Plist data transfer constants, as employed by the [Array of Dictionary Objects] ::

struct ChartSolarElevationValues {
    
    var theRequiredTimeOfDay: String
    var theCalculatedElevation: Double
    
    //  Structs have an implicit [init]. This is here for reference.
    init(theRequiredTimeOfDay: String, theCalculatedElevation: Double) {
        
        self.theRequiredTimeOfDay = theRequiredTimeOfDay
        self.theCalculatedElevation = theCalculatedElevation
        //mySolarElevationChartData.append(self)
    
    }   //  End of [init(theRequiredTimeOfDay: String, theCalculatedElevation: Double)]

}   //  End of [struct ChartSolarElevationValues]

Unfortunately, the result did not append each subsequent calculation, but continued to create the same calculation as a new distinct object ::

NOTE :: I only called three calculations with the Struct test.


//  NOTE :: To prevent an [ERROR] at [var mySolarElevationChartData = [ChartSolarElevationValues]] since it has an init.
        //  Therefore you must add () at the end of [var mySolarElevationChartData = [ChartSolarElevationValues]]
        let theData = [ChartSolarElevationValues]()
        
        //print("theData :: \(theData)\n")
        
        let someData = ChartSolarElevationValues(theRequiredTimeOfDay: TheTimeForDaySunElevation.theTheTimeForDaySunElevation, theCalculatedElevation:VerifyCityLocationSearchRequestCorrectedSolarElevation.theVerifyCityLocationSearchRequestCorrectedSolarElevation)
        
        var theData_New = theData
        
        theData_New.append(someData)
        
        print("theData_New :: \(theData_New)\n")

	  //	Prints :: theData_New :: [My_Map.ChartSolarElevationValues(theRequiredTimeOfDay: "00:06:00", theCalculatedElevation: -61.11000735370401)]]

		//	Prints :: [theData_New :: [My_Map.ChartSolarElevationValues(theRequiredTimeOfDay: "00:12:00", theCalculatedElevation: -61.315092082911875)]]

		//	Prints :: [theData_New :: [My_Map.ChartSolarElevationValues(theRequiredTimeOfDay: "00:18:00", theCalculatedElevation: -61.47403413313205)]]

So, I am misintepreting the required coding structure to properly append the Elevation Chart, and the Azimuth Chart with the calculated data.

I know something is amiss, but for the moment, I do not know how to address this issue.

Your suggestions would be welcome ... :]

jim_k

Display Calculated Data in SwiftUI Charts ...
 
 
Q