Quick Start Guide > Tutorials (Swift3) > Tutorial 03 - Adding Series to a Chart
Tutorial 03 - Adding Series to a Chart

Adding Series to a SciChartSurface

Source code for this tutorial can be found at our Github Repository under v2.x > Tutorials

Declaring a RenderableSeries

To draw a Line and Scatter series on the chart, we need to define two RenderableSeries, and two DataSeries to hold the data.

First, lets declare the data series and renderable series:

Create Data for the Charts
Copy Code
   var lineDataSeries: SCIXyDataSeries!
   var scatterDataSeries: SCIXyDataSeries!
    
   var lineRenderableSeries: SCIFastLineRenderableSeries!
   var scatterRenderableSeries: SCIXyScatterRenderableSeries!

In the following step, let's add two methods for initializing these properties:

Create Data for the Charts
Copy Code
    func createDataSeries(){
        // Init line data series
        lineDataSeries = SCIXyDataSeries(xType: .double, yType: .double)
        for i in 0..<500{
            lineDataSeries.appendX(SCIGeneric(i), y: SCIGeneric(sin(Double(i))*0.01))
        }
       
        // Init scatter data series
        scatterDataSeries = SCIXyDataSeries(xType: .double, yType: .double)
        for i in 0..<500{
            scatterDataSeries.appendX(SCIGeneric(i), y: SCIGeneric(cos(Double(i))*0.01))
        }
    }
   
    func createRenderableSeries(){
        lineRenderableSeries = SCIFastLineRenderableSeries()
        lineRenderableSeries.dataSeries = lineDataSeries
       
        scatterRenderableSeries = SCIXyScatterRenderableSeries()
        scatterRenderableSeries.dataSeries = scatterDataSeries
       
        sciChartSurface?.renderableSeries.add(lineRenderableSeries)
        sciChartSurface?.renderableSeries.add(scatterRenderableSeries)
    }   
NOTE: In the above code, you will see the use of the structure SCIGeneric to pass values to the DataSeries. This is a structure which allows us to maintain extremely high performance and avoid unboxing and boxing of memory, while providing a generic DataSeries to you as an API. For more info about this type, see SCIGenericType.

Now, to show the renderable series, we need to call these two methods. Let's call them in viewDidLoad method, where we created SCIChartSurface object:

Create Data for the Charts
Copy Code
         
...
    override func viewDidLoad() {
        super.viewDidLoad()
       
        // Do any additional setup after loading the view, typically from a nib.
       
        sciChartSurface = SCIChartSurface(frame: self.view.bounds)
        sciChartSurface?.translatesAutoresizingMaskIntoConstraints = true
       
        self.view.addSubview(sciChartSurface!)
       
        sciChartSurface?.xAxes.add(SCINumericAxis())
        sciChartSurface?.yAxes.add(SCINumericAxis())
       
        createDataSeries()
        createRenderableSeries()
    } 
...

 

Congratulations! You now have added some series to a chart! 

See Also