Add Series (lineSeries) to a chart in Swift


#1

Hi

Just starting with Shinobi

I am looking on information on how to add several series to a chart in swift.
When I have one series the graph is displayed. It works but with additional series I have issues => no graph displayed. I could only find the following example with adding series to a chart.

Is this the only way to add ?

  • (id)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;
    }

Do I have to use SChartDataAdapter?
Is there an example with SChartDataAdapter in swift?

In the documentation, I found the following:
You can add a series to your chart by simply creating an array of either SChartDataPoints or NSNumber objects that contain your data values along with a series object that must be a subclass of SChartSeries (e.g. SChartLineSeries). Use the addSeries:dataArray: to accomplish this.

SChartLineSeries does seem to have “addSeries:dataArray:” function. Correct?

The issue I am facing is the following
I set the value X and Y values for the different arrays var EconProData = [SChartDataPoint] in ViewController.swift

fun loadChartData()

var EconProData = [SChartDataPoint]

            let ApmsDataPoint = SChartDataPoint()
            ApmsDataPoint.xValue = dateTime
            ApmsDataPoint.yValue = yValue
            
            var row1 = [SChartDataPoint]()
            row1.append(ApmsDataPoint)
            
            EconProData.append(row1)
            
            let ApmsDataPoint1 = SChartDataPoint()
            ApmsDataPoint1.xValue = dateTime
            ApmsDataPoint1.yValue = yValue1
            
            var row2 = [SChartDataPoint]()
            row2.append(ApmsDataPoint1)
            
            EconProData.append(row2)

            
            let ApmsDataPoint2 = SChartDataPoint()
            ApmsDataPoint2.xValue = dateTime
            ApmsDataPoint2.yValue = yValue2
            
            var row3 = [SChartDataPoint]()
            row3.append(ApmsDataPoint2)
            
            EconProData.append(row3)

        let xAxis = SChartDiscontinuousDateTimeAxis()
        xAxis.title = "Date"
        enablePanningAndZoomingOnAxis(axis: xAxis)
    
        chart.xAxis = xAxis
        
        // Y Axis
        let yAxis = SChartNumberAxis()
        yAxis.title = "EconPro Read Data BLE Amps"
        enablePanningAndZoomingOnAxis(axis: yAxis)

        chart.yAxis = yAxis

        self.chart.datasource = self


#2

Hi Corinne,

We recommend using the SChartDataSource method numberOFSeriesAtIndex to add multiple series to your chart.

Then you can use the seriesIndex parameter in the seriesAtIndex:, numberOfDataPointsForSeriesAtIndex: & dataPointAtIndex:forSeriesAtIndex: methods to add data to the correct series.

More information on these data source methods can be found here.

Let me know if you have any questions.

Kind regards,
Andrew Polkinghorn.