iOS-Charts: Set UIImageViews in accordance with PieChart's Pie positions?

1.3k views Asked by At

I'm currently using iOS-Charts (https://github.com/danielgindi/ios-charts) in order to present a pie chart. I got it working, and can highlight it and whatnot. But I need to place icons next to these pies that are presented. So I'd like to place an UIImageView outside the pie chart, but in the "middle" of each corresponding pie. Check the image for clarification. Is there a simple way to do this? This also needs to be dynamic, of course, so I can't just place them in the storyboard.

I can get how much of a circle each pie consists of, in degrees or percentage, but I don't know how to use this in order to get positions to place the UIImageView. Any suggestions?

Edit: Forgot the image duh enter image description here

1

There are 1 answers

5
daniel.gindi On BEST ANSWER
let FDEG2RAD = CGFloat(M_PI / 180.0)
var drawAngles = chart.drawAngles
var absoluteAngles = chart.absoluteAngles
var animator = chart.animator
var center = chart.centerCircleBox
var r: CGFloat = YOUR_DRAWING_RADIUS_HERE

for (var i = 0; i < absoluteAngles.count; i++)
{
  var offset =  drawAngles[i] / 2.0
  var x = (r * cos(((rotationAngle + absoluteAngles[i] - offset) * animator.phaseY) * FDEG2RAD) + center.x)
  var y = (r * sin(((rotationAngle + absoluteAngles[i] - offset) * animator.phaseY) * FDEG2RAD) + center.y)

  // Put whatever you want now on the x/y
}