ios - 在 Swift 中为 XYPieChart 设置值

标签 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 以及其他变量设置不同的变量?

最佳答案

首先你必须确保你有一个桥接 header ,这样你才能访问 Objective-C 代码,请参见这个网站:

How to call Objective-C code from Swift

然后,在您希望生成饼图的 View Controller 中,您必须确保包含必要的方法以符合 XYPieChartDataSource(至少,XYPieChartDelegate 具有额外的可选方法),如下所示在这里 swift :

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 header :

类 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)

然后,您可以这样初始化饼图:

    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)
    }

最后,不要忘记重新加载数据并添加到 subview

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

应该可以了!

关于ios - 在 Swift 中为 XYPieChart 设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26836562/

相关文章:

iOS 使用 Fabric 发送推文而不撰写推文

ios - 如何将self.button.currentImage与图像进行比较?

iphone - 如何检测其他应用程序是否正在播放背景音频?

Swift bool 文字(Obj-C @YES @NO 等价物)

ios - 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因 : '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array

ios - 开始使用 iOS 推送通知

ios - 无法使用 afnetworking 注册用户

swift - 使用 Swift 和 SpriteKit 实现高级旋转行为

iphone - 如何在iPhone上播放3gpp文件?

ios - 在主窗口上创建一个新的 UIWindow