-
Notifications
You must be signed in to change notification settings - Fork 0
/
PieChart.swift
62 lines (52 loc) · 1.8 KB
/
PieChart.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// PieChart.swift
// PreSales-Huddle
//
// Created by Sachin Avhad on 27/07/15.
// Copyright (c) 2015 synerzip. All rights reserved.
//
import UIKit
class PieChart: UIViewController {
@IBOutlet weak var graphView:CPTGraphHostingView!
weak var delegate: Charts?
override func viewDidLoad() {
super.viewDidLoad()
CreatePieChart()
}
@IBAction func Close(sender: UIBarButtonItem) {
dismissViewControllerAnimated(true, completion: nil)
}
func CreatePieChart() {
let graph = CPTXYGraph(frame: self.graphView.bounds)
graph.title = "Pie Chart"
graph.paddingLeft = 0
graph.paddingTop = 0
graph.paddingRight = 0
graph.paddingBottom = 0
// hide the axes
let axes = graph.axisSet as! CPTXYAxisSet
let lineStyle = CPTMutableLineStyle()
lineStyle.lineWidth = 0
axes.xAxis.axisLineStyle = lineStyle
axes.yAxis.axisLineStyle = lineStyle
// add a pie plot
let pie = CPTPieChart(frame: CGRectZero)
pie.dataSource = delegate
pie.pieRadius = (min(self.graphView.bounds.size.height, self.graphView.bounds.size.width) * 0.7)/2
pie.labelOffset = 2
graph.addPlot(pie)
self.graphView.backgroundColor = UIColor.lightGrayColor()
self.graphView.hostedGraph = graph
// configure legend
let theLegend = CPTLegend(graph: self.graphView.hostedGraph)
theLegend.numberOfColumns = 1
theLegend.fill = CPTFill(color:CPTColor.whiteColor())
theLegend.borderLineStyle = CPTLineStyle()
theLegend.cornerRadius = 5.0
let anchor:CPTRectAnchor = .BottomRight
self.graphView.hostedGraph.legendAnchor = anchor
self.graphView.hostedGraph.legend = theLegend
let legendPadding = -(self.view.bounds.size.width / 16)
self.graphView.hostedGraph.legendDisplacement = CGPointMake(legendPadding, 0.0);
}
}