Loading Weather Data

Loading Weather Data

As explained in the main graph view usage document, the basic implementation of setting up a graph view assigns static data on the graph's series items. If using data from the Xweather Weather API, this process requires that you handle loading data before setting the graph's series data.

Setup Weather Series Items

However, you can setup your graph view to automatically handle requesting and setting data from the Xweather Weather API without needing to perform the requests or data setup yourself. Just use the AWFWeatherSeriesItem and AWFWeatherGraphSeries classes instead when configuring your graph. When using this method, you must provide the AWFWeatherEndpoint instance you want to use for a specific series item, which is what will perform the request for that series item's data:

let endpoint = AWFObservationsArchive()
endpoint.options?.place = AWFPlace(city: "seattle", state: "wa", country: "us")
endpoint.options?.fromDateString = "-24hours"

Then setup the series item using AWFWeatherSeriesItem instead of AWFSeriesItem and associate it with the endpoint instance by setting the endpoint property:

let tempItem = AWFWeatherSeriesItem()
tempItem.title = "Temperature"
tempItem.endpoint = endpoint
tempItem.xAxisPropertyName = "periods.#.timestamp"
tempItem.yAxisPropertyName = "periods.#.tempF"

Note that in the above example, we prefix the model value's key path with periods.#. This is because the AWFObservationsArchive endpoint returns an AWFObservationArchive instance whose periods property contains the array of AWFObservation instances for the range. If a property on an AWFWeatherObject is an array but you need to reference a property on each item within that array for your series, you can use a hash (#) symbol before the sub-object's key path. This will tell the graph to use the key path property values for each point in the series.

You can also use the same endpoint instance for multiple series items, which is useful for graphing different values from the same weather model object. The following example reuses the same AWFObservationsArchive instance from above but for the series item plotting wind speed values instead of temperature:

let windItem = AWFWeatherSeriesItem()
windItem.endpoint = endpoint
windItem.xAxisPropertyName = "periods.#.timestamp"
windItem.yAxisPropertyName = "periods.#.windSpeedMPH"

Since you're not providing static data for your graph series, you'll need to tell the graph view to load the required data for each of your series items. So just call reloadData on your graph view when you're ready to load or reload the graph's data: