Skip to content
Ivan Schütz edited this page Apr 27, 2017 · 11 revisions

The main component to create content in SwiftChart are layers. Layers conform to the protocol ChartLayer. It's probably helpful to think about layers like view controllers - they manage the model data and the presentation. The chart itself is only a lightweight container that iterates through the layers, calling methods like chartInitialized on them, when it's ready to display data, or broadcasting zooming and panning events, which each layer can decide to handle individually (more details about transform handling in TODO).

Everything that is displayed in a chart is part of a layer. The axes as well as the different content types are implemented using layers. Here is a quick diagram of the current layer hierarchy :

Rendering, z-order

Layers have a lot of independence - they decide when and how to render their data. They can add subviews to the chart, or CALayers, or render with core graphics in the chart's view, or in their own added subviews... the possibilities are endless (not literally of course!). This flexibility of course enables a potential "mess" where it's difficult to predict the z-ordering of the views added by each individual layer, but it's a small price to pay and for the usual combinations of layers this doesn't cause problems. Just keep in mind that layers that work with subviews will always be displayed in front of layers that use core graphics (unless the later use core graphics on an own added subview), independently of where they are in the chart layers array, and that similarly, views that are added after a delay to the chart will appear in front of views that were added previously, independently of which layers they belong to (unless the delayed views are added to a subview which was added at initialization by the layer they belong to, in which case the z order remains "correct"). In most cases you don't have to worry about this, though, if you just use the layers provided by SwiftCharts and standard layer combinations.

Custom layers

You of course can implement your own layers if you need a special kind of chart or high-level functionality not currently supported by SwiftCharts. An example of functionality where it would make sense to create a new layer would be a crosshair for the scatter chart. This requires special logic, and possibly state (e.g. a quadtree to optimize the search behaviour), which involves all the chart points. Things where you don't need a new layer, on the other side, is for example to customize the appearance or interactivity of individual chart points. In most of these cases you only have to use ChartPointsViewsLayer with a custom view generator or customize / extend one of the existing layers. The exact layer class you have to extend depends on your requirements. Looking at the existing layers is probably a good guide to figure this out.