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 PointsUsing Swift to set values for XYPieChart
I need to set different values for my XYPieChart using Swift, but the entire XYPieChart.h file is written in Objective-C. I need to set values for number of slices, values for each slice, colors and text for each slice but I don't believe I know how to call them and set values for them correctly. I want my pie chart to display results for a poll. I've already created the IBOutlets for displaying the polls, but I'm not sure how to call these functions to set values for each of them:
@class XYPieChart;
@protocol XYPieChartDataSource <NSObject>
@required
- (NSUInteger)numberOfSlicesInPieChart:(XYPieChart *)pieChart;
- (CGFloat)pieChart:(XYPieChart *)pieChart valueForSliceAtIndex:(NSUInteger)index;
@optional
- (UIColor *)pieChart:(XYPieChart *)pieChart colorForSliceAtIndex:(NSUInteger)index;
- (NSString *)pieChart:(XYPieChart *)pieChart textForSliceAtIndex:(NSUInteger)index;
@end
So far, this is all I could figure out:
@IBOutlet var chartDisplay1: [UIImageView]!
func pieChart(pieChart:XYPieChart!,index:Int!) ->CGFloat
{
let value:NSNumber = self.values[index] as NSNumber
return value.doubleValue
}
The values I'm trying to set for each slice in this particular graph are my variables called votes and votes2. For this poll, those are the only two slices the pie chart needs. How do I set the different variables for numberOfSlicesInPieChart and valueForSliceAtIndex, and the rest?