In order to render your data within the chart you need to supply a ‘datasource’, this is a class that adopts the SChartDatasource protocol methods.


Within ViewController.m adopt the datasource protocol via the class extension, that Xcode generated for you, as follows:

@interface ViewController () <SChartDatasource>

The SChartDatasource protocol has four required methods. We’ll take a look at each of them in turn.

The first method numberOfSeriesInSChart: is used to inform the chart how many series you wish to render. Add the following implementation, with a hard-coded value of two:

- (int)numberOfSeriesInSChart:(ShinobiChart *)chart {
    return 2;

The next method, sChart:seriesAtIndex: is used to supply each series to the chart. The series objects describe the visual appearance of each series, but not their actual data values. Add the following to the view controller in order to return two different line series:

-(SChartSeries *)sChart:(ShinobiChart *)chart seriesAtIndex:(int)index {

    SChartLineSeries *lineSeries = [[SChartLineSeries alloc] init];

    // the first series is a cosine curve, the second is a sine curve
    if (index == 0) {
        lineSeries.title = [NSString stringWithFormat:@"y = cos(x)"];
    } else {
        lineSeries.title = [NSString stringWithFormat:@"y = sin(x)"];

    return lineSeries;

The next method, sChart:numberOfDataPointsForSeriesAtIndex:, is used to specify the number of data points within a specific series. Add the following implementation below:

- (int)sChart:(ShinobiChart *)chart numberOfDataPointsForSeriesAtIndex:(int)seriesIndex {
    return 100;

In this example, both series have 100 points, but they do not have to have the same number.

The last required method required by the protocol is sChart:dataPointAtIndex:forSeriesAtIndex:, which the chart uses to request the value of each individual datapoint for each series. The returned datapoint must adopt the SChartData protocol, which specifies xValue and yValue properties. The charting framework supplies a concrete implementation of this protocol, SChartDataPoint, however, you have the option of implementing this protocol with your own data-objects in order to avoid the need to maintain two copies of your data.

Add the following implementation:

- (id<SChartData>)sChart:(ShinobiChart *)chart dataPointAtIndex:(int)dataIndex forSeriesAtIndex:(int)seriesIndex {

    SChartDataPoint *datapoint = [[SChartDataPoint alloc] init];

    // both functions share the same x-values
    double xValue = dataIndex / 10.0;
    datapoint.xValue = [NSNumber numberWithDouble:xValue];

    // compute the y-value for each series
    if (seriesIndex == 0) {
        datapoint.yValue = [NSNumber numberWithDouble:cosf(xValue)];
    } else {
        datapoint.yValue = [NSNumber numberWithDouble:sinf(xValue)];

    return datapoint;

Now that the datasource is implemented, you can add the following to viewDidLoad:

_chart.datasource = self;

At this point if you build and run, you should see a couple of series:


With the current implementation each individual datapoint is requested via the datasource. If you have a lot of data, and it is already present in memory, you can optionally make use of thesChart:dataPointsForSeriesAtIndex: protocol method. Your implementation of this method should return all the points for a given series, and this method will be used instead ofsChart:dataPointAtIndex:forSeriesAtIndex:.