Swift: Disable spoke in specific datapoints Donut Chart


#1

I have a litle problem with spoke in donut chart. I want hide the spoke(spoke label) if dataPoint.value = 0 (value of data is 0). I try this but its doesnt work :

    func sChart(_ chart: ShinobiChart, seriesAt index: Int) -> SChartSeries {
    ......
    /*
    if (chart == donutOccupiedRoom) {
        if index == 0 {
            if paying == 0 || comp == 0 || hUse == 0 {
                chart.style().spokeStyle.showSpokes = false
                chart.selectedStyle().spokeStyle.showSpokes = false
            }
        }
    }
*/
    
    return chart
}

I bind the spokes label in this method :

func sChart(_ chart: ShinobiChart, alter label: UILabel, for datapoint: SChartRadialDataPoint, atSlice index: Int, in series: SChartRadialSeries) {
    if (chart == donutArrival) {
        switch index {
        case 0:
            label.text = "\(datapoint.name!): \(datapoint.value!)"
            break
        case 1:
            label.text = "\(datapoint.name!): \(datapoint.value!)"
            break
        default:
            break
        }
.............
...........
    }
}

Any suggest and answer will help for me. Thanks in Advance


#2

Hi MrX,

Try something like this in your alter label method:

func sChart(_ chart: ShinobiChart, alter label: UILabel, for datapoint: SChartRadialDataPoint, atSlice index: Int, in series: SChartRadialSeries) {
    if (datapoint.value == 0) {
        let donutSeries = series as! SChartDonutSeries
        let currentStyle = series.selectedDataPoints.contains(datapoint) ? donutSeries.selectedStyle().spokeStyle : donutSeries.style().spokeStyle
        let newStyle = currentStyle.copy() as! SChartSpokeStyle
        newStyle.showSpokes = false
        donutSeries.setSpokeStyle(newStyle, forSpokeAtSliceIndex: index)
        label.isHidden = true
    } else {
        label.text = "\(datapoint.name!): \(datapoint.value!)"
    }
}

If the value is 0, we get the series’ current spoke style for our datapoint (depending on whether or not it is selected), take a copy of it (so we don’t alter the style for all points in the series), hide the spokes on our copied style, and set that spoke style for this specific point. We also hide the label.

I hope that helps!

Alison


#3

Hi aclarke,

I solved yesterday with this code :

if datapoint.value == 0 {
        label.isHidden = true
        let spokeStyle = SChartSpokeStyle()
        spokeStyle.showSpokes = false
        (series as! SChartDonutSeries).setSpokeStyle(spokeStyle, forSpokeAtSliceIndex: index)
    } else {
        label.text = "\(datapoint.name!): \(datapoint.value!)"
    }

I try your code too and its work, thanks in advance

Best Regard,
MrX