Swift: SChartColumn Change Legend Label


#1

I have new ViewController with SChartColumn. Below the picture

I search and trying for a couple hour, how to change the Legend’s labels series?

In the sample folder, it use below code :

chart.title = (index == 0 ? "Depting" : " ")

But its not work with my code. Any answer and suggest will help for me. Thanks in Advance .


Multiple columns per Series
Multiple columns per Series
#2

Hi MrX,

To change the text in the legend, you need to ensure you set the title on each series rather than on the chart itself.

The relevant code in the column series sample is:

// Create column series objects for both sets of data and assign relevant title
func sChart(_ chart: ShinobiChart, seriesAt index: Int) -> SChartSeries {
    let columnSeries = SChartColumnSeries()
    columnSeries.title = (index == 0 ? "2016" : "2017")
    return columnSeries
}

I hope that helps!

Alison


#3

I try this but only label with index 0 and 1 which change, label in index 2 and 3 is didnt change.


#4

I’m a little confused about why setting the title on each series only worked for 2 out of your 4 series. Please could you could post a screenshot and your code to help me work out what’s going on?


#5

Sorry aclarke, below my code :

func sChart(_ chart: ShinobiChart, seriesAt index: Int) -> SChartSeries {
    let chart = SChartColumnSeries()
    
    chart.title = (index == 0 ? "1" : "")
//        chart.title = (index == 1 ? "2" : "")
//        chart.title = (index == 2 ? "3" : "")
//        chart.title = (index == 3 ? "4" : "")
    
    
    
    return chart
}

func numberOfSeries(in chart: ShinobiChart) -> Int {
    return 4
}

func sChart(_ chart: ShinobiChart, numberOfDataPointsForSeriesAt seriesIndex: Int) -> Int {
    return 1
}

func sChart(_ chart: ShinobiChart, dataPointAt dataIndex: Int, forSeriesAt seriesIndex: Int) -> SChartData {
    
    var dataPoint = SChartDataPoint()
    
    var xValue: String = " "
    var yValue: Int = 0
    
    var x2Value: String = " "
    var y2Value: Int = 0
    
    if (seriesIndex == 0) {
        
//            if dataIndex == 0 {
//                xValue = "Rooms"
//                yValue = (dataOverview["departed1"] as! Int)
//                
//                dataPoint.xValue = xValue
//                dataPoint.yValue = yValue
//                
//            }
//            else if (dataIndex == 1) {
//                xValue = "Persons"
//                yValue = (dataOverview["departed2"] as! Int)
//                
//                dataPoint.xValue = x2Value
//                dataPoint.yValue = y2Value
//
//            }
        
        xValue = "Departed"
        yValue = (dataOverview["departed1"] as! Int)
        
//            dataPoint.sChartValue(forKey: "Room")
        dataPoint.xValue = xValue
        dataPoint.yValue = yValue
        
    }
    else if (seriesIndex == 1) {
        xValue = "Departing"
        yValue = (dataOverview["departing1"] as! Int)
        
        dataPoint.xValue = xValue
        dataPoint.yValue = yValue
    }
    else if (seriesIndex == 2) {
        xValue = "Arrival"
        yValue = (dataOverview["arrived1"] as! Int)
        
        dataPoint.xValue = xValue
        dataPoint.yValue = yValue
    }
    
    else if(seriesIndex == 3) {
        xValue = "Arriving"
        yValue = (dataOverview["arriving1"] as! Int)
        
        dataPoint.xValue = xValue
        dataPoint.yValue = yValue
    }
    
//        var xValue = "Testing"
//        var yValue = "3"
//        var zValue = 35
//        
//        dataPoint.xValue = xValue
//        dataPoint.yValue = yValue
//        dataPoint.xValue = xValue
//        dataPoint.yValue = zValue
        
    return dataPoint
    
}

#6

Thanks for providing the code. I don’t know if you’re misunderstanding the ternary conditional operator syntax but you simply need to check the value of the series index and provide the appropriate series title. Here’s an example using a switch statement:

func sChart(_ chart: ShinobiChart, seriesAt index: Int) -> SChartSeries {
    let series = SChartColumnSeries()
    
    switch index {
    case 0:
        series.title = "My first series"
    case 1:
        series.title = "My second series"
    case 2:
        series.title = "My third series"
    case 3:
        series.title = "My fourth series"
    default: ()
    }
    
    return series
}

Does that help?


#7

hi aclarke,

How really i’m fool. I use if else condition now its work. Thanks aclarke