kalpa kalpa - 5 months ago 39
Swift Question

3D PieChart using Core Plot in swift

I am trying to design a 3D PieChart using core plot.
So that I have the following code

@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.
}


But I am getting a white color pie chart.
here is the image of my output.
https://drive.google.com/open?id=0B9YZfopxzOv5UG9hQmRjZGVQdzQ
I updated my data source and delegate method.
Yes without overly I am getting this output -
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)])"
}


This is my Prepareforsegue Method where I am assigning the pieChartValueArrayToShow and 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)")
}

Answer

I fixed it by myself. I did a stupid mistake. Instead of this

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

I Wrote

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