RKPieChart icon indicating copy to clipboard operation
RKPieChart copied to clipboard

Single values not showing Swift 3

Open pan23 opened this issue 7 years ago • 4 comments

I have installed RKPieChart and it works fine for 2 and 3 values with PieChart. but only one value is 100 and other two are 0, piechart not showing.

pan23 avatar Dec 27 '17 12:12 pan23

I will be looking for it today and let you know 👍

ridvank avatar Dec 28 '17 06:12 ridvank

I had the same problem, you just need to add a condition in the RKPieChartView class in the function calculateAngles(), "if item.endAngle! > 2 * π && items.count! = 1" , I leave the complete code of the function.

 private func calculateAngles()  {

    totalRatio = items.map({ $0.ratio }).reduce(0, { $0 + $1 })

    for (index, item) in items.enumerated() {
        item.startAngle = index == 0 ? 3 * π / 2 : items[index - 1].endAngle

        if items.count == 1 {
            totalRatio = 100
        }

        item.endAngle = item.startAngle! + (360 * item.ratio / totalRatio).degreesToRadians

        if item.endAngle! > 2 * π && items.count != 1 {
            item.endAngle = item.endAngle! - 2 * π
        }
    }
}

I hope help you

RodrigoLara avatar Jan 29 '18 05:01 RodrigoLara

My way, not sure about this:

    private func calculateAngles() {
        totalRatio = items.map({ $0.ratio }).reduce(0, { $0 + $1 })
        for (index, item) in items.enumerated() {
            item.startAngle = index == 0 ? 3 * π / 2 : items[index - 1].endAngle
            if items.count == 1 {
                totalRatio = 100
            }
            var diff = (360 * item.ratio / totalRatio).degreesToRadians
            let pi2 = π * 2
            while diff > pi2 {
                diff -= pi2
            }
            item.endAngle = item.startAngle! + diff
        }
    }

soleilpqd avatar Feb 07 '18 10:02 soleilpqd

If you have only one item, and you want It's occupy whole graph (100%), like this:

private func calculateAngles() {
        totalRatio = items.map({ $0.ratio }).reduce(0, { $0 + $1 })
        for (index, item) in items.enumerated() {
            item.startAngle = index == 0 ? 3 * π / 2 : items[index - 1].endAngle
            if items.count == 1 {
                items[0].ratio = 99.999999 // if ratio <= 100 then crashed. so just input 99.9999
                totalRatio = 100
            }
            item.endAngle = item.startAngle! + (360 * item.ratio / totalRatio).degreesToRadians
            if item.endAngle! > 2 * π {
                item.endAngle = item.endAngle! - 2 * π
            }
        }
    }

But this solution is just a fake. And this is very easy way.

usinuniverse avatar Jul 05 '18 09:07 usinuniverse