1

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

ClockWise
  • 1,509
  • 15
  • 32

1 Answers1

2
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
}
daniel.gindi
  • 3,457
  • 1
  • 30
  • 36
  • I haven't tested this code, but it looks I'll take your word for it since you are the developer of the ios-charts. I made my own solution 20 minutes ago which works fine, but is not at all connected to the ios-charts. I just get the centerPoint of my ChartPieView and calculate a point depending on the width, height, and the angle amount which each pie occupies in the PieChart. So I simply use Pythagoras theorem and use the angle provided by PieChartView.drawAngles. This looks a lot cleaner though, thank you! – ClockWise Jun 11 '15 at 13:03
  • @ClockWise I've updated it a bit, as you really do not need to recalculate the angles - but still need to do the cos/sin to take the x/y – daniel.gindi Jun 11 '15 at 15:50
  • @ClockWise, how did you get the centerPoint of the ChartPieView? Neither pieChart.centerCircleBox, nor regular old pieChart.center is marking the true center of the pie chart. – Aditya Garg Dec 09 '16 at 09:02
  • but this isn't working when pie chart is scrolling ... this images should scroll with the slices. how can I achieve this ? – Maryam Fekri Feb 11 '17 at 06:17
  • updated Question and answer with new version of iso-chart ( v 3.0 ) is here -> http://stackoverflow.com/q/42113512/2739657 – Maryam Fekri Feb 12 '17 at 10:32