在Swift中为XYPieChart设置值

时间:2014-11-10 04:04:01

标签: ios objective-c swift

我需要使用Swift为我的XYPieChart设置不同的值,但整个XYPieChart.h文件是用Objective-C编写的。我需要设置切片数量,每个切片的值,每个切片的颜色和文本的值,但我不相信我知道如何调用它们并正确设置它们的值。我希望我的饼图显示投票结果。我已经创建了用于显示民意调查的IBOutlets,但我不确定如何调用这些函数为每个函数设置值:

@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

到目前为止,这是我能想到的全部内容:

 @IBOutlet var chartDisplay1: [UIImageView]!
 func pieChart(pieChart:XYPieChart!,index:Int!) ->CGFloat
    {
        let value:NSNumber = self.values[index] as NSNumber
        return value.doubleValue
    }

我试图为这个特定图中的每个切片设置的值是我的变量,称为votes和votes2。对于此轮询,这些是饼图所需的唯一两个切片。如何为numberOfSlicesInPieChart和valueForSliceAtIndex设置不同的变量,其余的变量是什么?

1 个答案:

答案 0 :(得分:2)

首先,您必须确保有一个桥接标题,以便您可以访问目标C代码,请访问此网站:

How to call Objective-C code from Swift

然后,在视图控制器中,您希望生成饼图,您必须确保包含必要的方法以符合XYPieChartDataSource(至少,XYPieChartDelegate具有其他可选方法),这些方法显示在快到这里:

func pieChart(pieChart: XYPieChart!, valueForSliceAtIndex index: UInt) -> CGFloat {

    return CGFloat(self.slices[Int(index)] as NSNumber)

}

func numberOfSlicesInPieChart(pieChart: XYPieChart!) -> UInt {

    return UInt(self.slices.count)
}


func pieChart(pieChart: XYPieChart!, colorForSliceAtIndex index: UInt) -> UIColor! {
    return self.sliceColors[Int(index)] as UIColor
}

并相应地修改ViewController标头:

类YourViewController:UIViewController,XYPieChartDelegate,XYPieChartDataSource {

最好在viewDidLoad上添加这些实例变量:

// Pie Chart Properties
    var slices:NSMutableArray = NSMutableArray(capacity: 10)
    var sliceColors:NSArray!
    var myPieChart:XYPieChart!
    var indexOfSlices:UInt!
    var numSlices:UInt = 0
    var valueAtSlice:Int = 0

之后,你已经为饼图设置了你想要的值,我在initializePieChart()方法中有这个:

let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: UIScreen.mainScreen().applicationFrame.size)
let midPointx = UIScreen.mainScreen().applicationFrame.width/2
var midPointy:CGFloat = 0
var rad:CGFloat = 0
let point = CGPoint(x: midPointx, y: midPointy)

然后,您可以将pieChart初始化为:

    let aPieChart:XYPieChart = XYPieChart(frame: rect, center: point, radius: rad)
    self.myPieChart = aPieChart

最后,设置dataSource,修改饼图的属性,如下所示:

   myPieChart.dataSource = self
    myPieChart.delegate = self

 //Various settings
        myPieChart.setPieBackgroundColor(UIColor(red: 0.251, green: 0.251, blue: 0.251, alpha: 1.0))
        myPieChart.startPieAngle = CGFloat(M_PI_2)
        myPieChart.animationSpeed = 2.0

    // Various settings
    myPieChart.showPercentage = true
    myPieChart.pieCenter = CGPointMake(midPointx, midPointy)
    myPieChart.userInteractionEnabled = false
    myPieChart.labelShadowColor = UIColor.blackColor()

    //For getting data into the slices
    self.sliceColors = [UIColor(red: 104/255, green: 1, blue: 67/255, alpha: 1),
        UIColor(red: 23/255, green: 247/255, blue: 252/255, alpha: 1),
        UIColor(red: 1, green: 0.231, blue: 0.188, alpha: 1)]

    for i in 0...2 {
        //This is just initializing the slices, you'll have to insert the data elsewhere using self.slices.replaceObjectAtIndex(0, withObject: dataVariable)

        var one:NSNumber = 0
        self.slices.addObject(one)
    }

然后最后,不要忘记重新加载数据并添加到子视图

self.pieChartView.setTranslatesAutoresizingMaskIntoConstraints(false)
        myPieChart.reloadData()
        self.pieChartView.addSubview(myPieChart)

应该这样做!