Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Hamza Ansari
30 PointsConnecting function to UIImageView
I'm trying to add an IBAction that causes a graph of data to appear from XYPieChart. I've already created the Outlet Collection, since two buttons can cause the graph to display. This is the function that displays values for each slice in the pie chart:
func pieChart(pieChart:XYPieChart,index:Int) -> CGFloat {
let value:NSNumber = self.values[index] as NSNumber
return value.doubleValue
}
The problem is, I don't know how to display the values for my two variables, which in this case are called votes and votes2. Here is my code so far:
@IBOutlet var pollResults1: [UITextField]!
@IBOutlet var chartDisplay1: [UIImageView]!
@IBAction func addVote1(sender: AnyObject) {
for button in self.buttons {
button.enabled = false
}
var query = PFQuery(className: "VoteCount")
query.getObjectInBackgroundWithId("BiEM17uUYT") {
(voteCount1: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%@", error)
} else {
voteCount1.incrementKey("votes")
voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
}
let votes = voteCount1["votes"] as Int
let votes2 = voteCount1["votes2"] as Int
let percent1 = votes * 100 / (votes + votes2)
let percent2 = votes2 * 100 / (votes + votes2)
self.pollResults1[0].text = "\(percent1)% (\(votes)) \(percent2)% (\(votes2))"
}
}
How do I do that, plus add the rest of the values like the number of pie slices and the colors for them and such? The functions for all the values are here:
func numberOfSlicesInPieChart(pieChart: XYPieChart) -> NSUInteger
func pieChart(pieChart: XYPieChart, valueForSliceAtIndex index: NSUInteger) -> CGFloat
func pieChart(pieChart: XYPieChart, colorForSliceAtIndex index: NSUInteger) -> UIColor
func pieChart(pieChart:XYPieChart, valueForSliceAtIndex index: NSUInteger) -> NSString
1 Answer
M T
11,934 PointsHamza, your 30 treehouse points clearly don't reflect the knowledge you have. Well done.
If I understand correctly, this may be what you're looking for:
http://cocoadocs.org/docsets/SHPieChartView/1.0.3/
If you aren't familiar with cocoapods, please view this video. I think you'll like how easy it can make difficult tasks.
http://teamtreehouse.com/library/build-a-photo-browser-iphone-app/what-is-an-api/caching-photos
Edit: I think you're further along than I thought. Unfortunately, I don't know swift. The number of values and colors can each be arrays. You could grab the number of values by calling "count" on the array. If you're looking to add labels with text values, you could initialize them and add a string of the number value to the textlabel.text attribute.