SwiftUI Gestures Logo

Appendix A: The DataProvider Struct

This entry is part 4 of 4 in the series SwiftUI Charts Gestures

The real heart of the demonstration app, is something that we didn’t cover at all, in the tutorial. That’s the Data Model for the app, which is in the DataProvider.swift structural type.

It’s a fairly basic type, that consumes some CSV (Comma-Separated-Values) data, and formats it into a configuration that can drive the chart.

The Data

The data, itself, is immutable, but the instance has a bit of state. In particular, it has the ability to select one row of data (which is translated into a bar, in the chart display), and we can also modify the “window” of X-axis values (which are dates. Each bar is a single day, within a range of dates, so we can modify that range).

The Support Stuff

The Model also supplies some “supporting” stuff, like bar section colors, chart axis values, and a chart legend.

This allows the actual chart code to be quite small.

If this were a shipping app, we’d likely set up a protocol (so we can feed the chart different data), localize the data, and probably add accessibility stuff.