3D PieChart在swift中使用Core Plot

时间:2016-06-17 10:45:01

标签: swift core-plot pie-chart

我正在尝试使用核心图设计3D PieChart。 所以我有以下代码

    @IBOutlet weak var pieChartDataLabel: UILabel!
@IBOutlet weak var graphView: CPTGraphHostingView!
var pieItem:Int = 0
var pieChartValueArrayToShow = [AnyObject]()
override func viewDidLoad() {
    super.viewDidLoad()

    let graph = CPTXYGraph(frame: CGRectZero)
    graph.title = "PieChart"
    graph.paddingLeft = 0
    graph.paddingTop = 0
    graph.paddingRight = 0
    graph.paddingBottom = 0
    graph.backgroundColor = UIColor.brownColor().CGColor

    let overlayGradient = CPTGradient()
    overlayGradient.gradientType = CPTGradientType.Radial
    overlayGradient.addColorStop(CPTColor.blackColor().colorWithAlphaComponent(0.0), atPosition: 0.9)
    overlayGradient.addColorStop(CPTColor.blackColor().colorWithAlphaComponent(0.4), atPosition: 1.0)

    let pie = CPTPieChart()
    //pie.borderLineStyle = linestyle
    pie.dataSource = self
    pie.delegate = self
    pie.pieRadius = (self.view.frame.size.width * 0.7)/2
    pie.overlayFill = CPTFill(gradient: overlayGradient)
    graph.addPlot(pie)
    self.graphView.hostedGraph = graph;
    // Do any additional setup after loading the view.
}

但我得到一张白色饼图。 这是我输出的图像。 https://drive.google.com/open?id=0B9YZfopxzOv5UG9hQmRjZGVQdzQ 我更新了我的数据源和委托方法。 是没有过度我得到这个输出 - https://drive.google.com/open?id=0B9YZfopxzOv5Uy14NXlQYkJZY1k

func numberOfRecordsForPlot(plot: CPTPlot) -> UInt {
    return UInt(pieItem)
}

func numberForPlot(plot: CPTPlot, field fieldEnum: UInt, recordIndex idx: UInt) -> AnyObject? {
    return pieChartValueArrayToShow[Int(idx)]
}

func pieChart(plot: CPTPieChart, sliceWasSelectedAtRecordIndex idx: UInt) {
    print("dataLabel was selected At Record Index getting called")
    self.pieChartDataLabel.text = "Your selected value is \(pieChartValueArrayToShow[Int(idx)])"
}

这是我的Prepareforsegue方法,我将分配 pieChartValueArrayToShow pieItem

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "ShowPieChart") {
        let desinationviewController = segue.destinationViewController as! PieChartViewController

        let count = pieChartValueArray.count as Int
        print(count)
        desinationviewController.pieItem = count//(sender?.integerValue)!//pieChartValueArray.count//
        print("count is \(pieChartValueArray.count)")
        desinationviewController.pieChartValueArrayToShow = pieChartValueArray
    }
    print("Prepare segue getting called\(sender)")
}

1 个答案:

答案 0 :(得分:2)

我自己修理了它。我犯了一个愚蠢的错误。 而不是这个

overlayGradient.addColorStop(CPTColor.blackColor().colorWithAlphaComponent(0.0), atPosition: 0.9)
overlayGradient.addColorStop(CPTColor.blackColor().colorWithAlphaComponent(0.4), atPosition: 1.0)

我写了

overlayGradient = overlayGradient.addColorStop(CPTColor.blackColor().colorWithAlphaComponent(0.0), atPosition: 0.8)
    overlayGradient = overlayGradient.addColorStop(CPTColor.blackColor().colorWithAlphaComponent(0.4), atPosition: 1.0)