From 8b04d61a15ed06d693ad13ad1643aec684c3ae19 Mon Sep 17 00:00:00 2001 From: fharmony Date: Fri, 30 Sep 2016 17:08:34 +0800 Subject: [PATCH] adapt swift 3 --- Graphs.xcodeproj/project.pbxproj | 2 ++ Graphs/BarGraphView.swift | 26 ++++++++------- Graphs/Graph.swift | 40 +++++++++++------------ Graphs/GraphView.swift | 28 ++++++++-------- Graphs/GraphsExtensions.swift | 52 +++++++++++++++-------------- Graphs/LineGraphView.swift | 51 ++++++++++++++++------------- Graphs/MultiBarGraphView.swift | 4 +++ Graphs/PieGraphView.swift | 56 ++++++++++++++++++-------------- 8 files changed, 143 insertions(+), 116 deletions(-) diff --git a/Graphs.xcodeproj/project.pbxproj b/Graphs.xcodeproj/project.pbxproj index 38c7245..a8caf93 100644 --- a/Graphs.xcodeproj/project.pbxproj +++ b/Graphs.xcodeproj/project.pbxproj @@ -317,6 +317,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -358,6 +359,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.3; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; diff --git a/Graphs/BarGraphView.swift b/Graphs/BarGraphView.swift index a083b6b..062acfd 100644 --- a/Graphs/BarGraphView.swift +++ b/Graphs/BarGraphView.swift @@ -42,11 +42,11 @@ public struct BarGraphViewConfig { ) { self.barColor = (barColor ?? DefaultColorType.Bar.color()).matColor() self.textColor = textColor ?? DefaultColorType.BarText.color() - self.textFont = textFont ?? UIFont.systemFontOfSize(10.0) + self.textFont = textFont ?? UIFont.systemFont(ofSize: 10.0) self.barWidthScale = barWidthScale ?? 0.8 self.zeroLineVisible = zeroLineVisible ?? true self.textVisible = textVisible ?? true - self.contentInsets = contentInsets ?? UIEdgeInsetsZero + self.contentInsets = contentInsets ?? UIEdgeInsets.zero } } @@ -63,9 +63,13 @@ internal class BarGraphView: UIView { self.graph = graph super.init(frame: frame) - self.backgroundColor = UIColor.clearColor() + self.backgroundColor = UIColor.clear self.setNeedsDisplay() } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } func setBarGraphViewConfig(config: BarGraphViewConfig?) { @@ -82,8 +86,8 @@ internal class BarGraphView: UIView { ) } - override func drawRect(rect: CGRect) { - super.drawRect(rect) + override func draw(_ rect: CGRect) { + super.draw(rect) guard let graph = self.graph else { return } @@ -97,7 +101,7 @@ internal class BarGraphView: UIView { let zero = rect.size.height / CGFloat((max - min).floatValue()) * CGFloat(min.floatValue()) - graph.units.enumerate().forEach({ (index, u) in + graph.units.enumerated().forEach({ (index, u) in switch self.config.barColor { case let .Mat(color): color.setFill() @@ -129,14 +133,14 @@ internal class BarGraphView: UIView { ) path.fill() - if let str = self.graph?.graphTextDisplay()(unit: u, totalValue: total) { + if let str = self.graph?.graphTextDisplay()(u, total) { - let attrStr = NSAttributedString.graphAttributedString(str, color: self.config.textColor, font: self.config.textFont) + let attrStr = NSAttributedString.graphAttributedString(string: str, color: self.config.textColor, font: self.config.textFont) let size = attrStr.size() - attrStr.drawInRect( - CGRect( + attrStr.draw( + in: CGRect( origin: CGPoint( x: sectionWidth * CGFloat(index) + rect.origin.x, y: u.value >= U(0) @@ -152,4 +156,4 @@ internal class BarGraphView: UIView { } }) } -} \ No newline at end of file +} diff --git a/Graphs/Graph.swift b/Graphs/Graph.swift index a43cee0..da10003 100644 --- a/Graphs/Graph.swift +++ b/Graphs/Graph.swift @@ -17,15 +17,15 @@ public enum GraphType { public class Graph { - public typealias GraphTextDisplayHandler = (unit: GraphUnit, totalValue: U) -> String? + public typealias GraphTextDisplayHandler = (_ unit: GraphUnit, _ totalValue: U) -> String? let kind: GraphKind - + init(barGraph: BarGraph) { self.kind = GraphKind.Bar(barGraph) } - + init(lineGraph: LineGraph) { self.kind = GraphKind.Line(lineGraph) } @@ -37,7 +37,7 @@ public class Graph { public extension Graph { - public convenience init(type: GraphType, data: [S], min minOrNil: U? = nil, max maxOrNil: U? = nil, textDisplayHandler: GraphTextDisplayHandler? = nil) { + public convenience init(type: GraphType, data: [S], min minOrNil: U? = nil, max maxOrNil: U? = nil, textDisplayHandler: GraphTextDisplayHandler? = nil) where S.GraphDataKey == T, S.GraphDataValue == U { let range = {() -> GraphRange? in if let min = minOrNil, let max = maxOrNil { @@ -49,11 +49,11 @@ public extension Graph { self.init(type: type, data: data, range: range(), textDisplayHandler: textDisplayHandler) } - public convenience init(type: GraphType, data: [S], range rangeOrNil: GraphRange? = nil, textDisplayHandler: GraphTextDisplayHandler? = nil) { + public convenience init(type: GraphType, data: [S], range rangeOrNil: GraphRange? = nil, textDisplayHandler: GraphTextDisplayHandler? = nil) where S.GraphDataKey == T, S.GraphDataValue == U { let r = {() -> GraphRange in if let r = rangeOrNil { return r } - let sorted = data.sort{ $0.value < $1.value } + let sorted = data.sorted{ $0.value < $1.value } return GraphRange( min: sorted.first?.value ?? U(0), max: sorted.last?.value ?? U(0) @@ -103,7 +103,7 @@ public extension Graph { let r = {() -> GraphRange in if let r = rangeOrNil { return r } - let sorted = array.sort{ $0 < $1 } + let sorted = array.sorted{ $0 < $1 } return GraphRange( min: sorted.first ?? U(0), max: sorted.last ?? U(0) @@ -153,7 +153,7 @@ public extension Graph { public convenience init(type: GraphType, dictionary: [T: U], range rangeOrNil: GraphRange? = nil, textDisplayHandler: GraphTextDisplayHandler? = nil) { - let sorted = dictionary.sort{ $0.1 < $1.1 } + let sorted = dictionary.sorted{ $0.1 < $1.1 } let r = {() -> GraphRange in if let r = rangeOrNil { return r } @@ -261,7 +261,7 @@ internal struct BarGraph: GraphBase { if let f = textDisplayHandler { return f } - return { (unit, total) -> String? in String(unit.value) } + return { (unit, total) -> String? in String(describing: unit.value) } } } @@ -279,7 +279,7 @@ internal struct MultiBarGraph: GraphBase { if let f = textDisplayHandler { return f } - return { (unit, total) -> String? in String(unit.value) } + return { (unit, total) -> String? in String(describing: unit.value) } } } @@ -312,7 +312,7 @@ internal struct LineGraph: GraphBase { if let f = textDisplayHandler { return f } - return { (unit, total) -> String? in String(unit.value) } + return { (unit, total) -> String? in String(describing: unit.value) } } } @@ -344,7 +344,7 @@ internal struct PieGraph: GraphBase { } return { (unit, total) -> String? in let f = unit.value.floatValue() / total.floatValue() - return String(unit.value) + " : " + String(format: "%.0f%%", f * 100.0) + return String(describing: unit.value) + " : " + String(format: "%.0f%%", f * 100.0) } } } @@ -400,9 +400,9 @@ public struct GraphTextAttributes { textColor: UIColor?, textAlign: NSTextAlignment? ) { - self.font = font ?? UIFont.systemFontOfSize(10.0) - self.textColor = textColor ?? UIColor.grayColor() - self.textAlign = textAlign ?? .Center + self.font = font ?? UIFont.systemFont(ofSize: 10.0) + self.textColor = textColor ?? UIColor.gray + self.textAlign = textAlign ?? .center } } @@ -411,11 +411,11 @@ public struct GraphTextAttributes { public protocol NumericType: Equatable, Comparable { - func +(lhs: Self, rhs: Self) -> Self - func -(lhs: Self, rhs: Self) -> Self - func *(lhs: Self, rhs: Self) -> Self - func /(lhs: Self, rhs: Self) -> Self - func %(lhs: Self, rhs: Self) -> Self + static func +(lhs: Self, rhs: Self) -> Self + static func -(lhs: Self, rhs: Self) -> Self + static func *(lhs: Self, rhs: Self) -> Self + static func /(lhs: Self, rhs: Self) -> Self + static func %(lhs: Self, rhs: Self) -> Self init() init(_ v: Int) } diff --git a/Graphs/GraphView.swift b/Graphs/GraphView.swift index 3a8cacf..a87a75d 100644 --- a/Graphs/GraphView.swift +++ b/Graphs/GraphView.swift @@ -16,21 +16,21 @@ public class GraphView: UIView { } } - private var barGraphConfig: BarGraphViewConfig? - private var lineGraphConfig: LineGraphViewConfig? - private var pieGraphConfig: PieGraphViewConfig? + var barGraphConfig: BarGraphViewConfig? + var lineGraphConfig: LineGraphViewConfig? + var pieGraphConfig: PieGraphViewConfig? public init(frame: CGRect, graph: Graph? = nil) { self.graph = graph super.init(frame: frame) - self.backgroundColor = UIColor.clearColor() + self.backgroundColor = UIColor.clear self.reloadData() } required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) - self.backgroundColor = UIColor.clearColor() + self.backgroundColor = UIColor.clear self.reloadData() } @@ -43,27 +43,27 @@ public class GraphView: UIView { switch graph.kind { case .Bar(let g): - if let view = g.view(self.bounds) { + if let view = g.view(frame: self.bounds) { if let c = barGraphConfig { - view.setBarGraphViewConfig(c) + view.setBarGraphViewConfig(config: c) } self.addSubview(view) } case .Line(let g): - if let view = g.view(self.bounds) { + if let view = g.view(frame: self.bounds) { if let c = lineGraphConfig { - view.setLineGraphViewConfig(c) + view.setLineGraphViewConfig(config: c) } self.addSubview(view) } case .Pie(let g): - if let view = g.view(self.bounds) { + if let view = g.view(frame: self.bounds) { if let c = pieGraphConfig { - view.setPieGraphViewConfig(c) + view.setPieGraphViewConfig(config: c) } self.addSubview(view) } @@ -84,7 +84,7 @@ extension GraphView { self.barGraphConfig = configuration() self.subviews.forEach { (v) in if let barGraphView = v as? BarGraphView { - barGraphView.setBarGraphViewConfig(barGraphConfig) + barGraphView.setBarGraphViewConfig(config: barGraphConfig) } } return self @@ -94,7 +94,7 @@ extension GraphView { self.lineGraphConfig = configuration() self.subviews.forEach { (v) in if let lineGraphView = v as? LineGraphView { - lineGraphView.setLineGraphViewConfig(lineGraphConfig) + lineGraphView.setLineGraphViewConfig(config: lineGraphConfig) } } return self @@ -104,7 +104,7 @@ extension GraphView { self.pieGraphConfig = configuration() self.subviews.forEach { (v) in if let pieGraphView = v as? PieGraphView { - pieGraphView.setPieGraphViewConfig(pieGraphConfig) + pieGraphView.setPieGraphViewConfig(config: pieGraphConfig) } } return self diff --git a/Graphs/GraphsExtensions.swift b/Graphs/GraphsExtensions.swift index 58ab391..2c3730e 100644 --- a/Graphs/GraphsExtensions.swift +++ b/Graphs/GraphsExtensions.swift @@ -26,15 +26,15 @@ public protocol GraphData { SequenceType -> 'Graph' object */ -extension SequenceType where Generator.Element: GraphData { +extension Sequence where Iterator.Element: GraphData { - typealias GraphDataKey = Generator.Element.GraphDataKey - typealias GraphDataValue = Generator.Element.GraphDataValue + typealias GraphDataKey = Iterator.Element.GraphDataKey + typealias GraphDataValue = Iterator.Element.GraphDataValue public func barGraph( range: GraphRange? = nil, textDisplayHandler: Graph.GraphTextDisplayHandler? = nil - ) -> Graph { + ) -> Graph { return Graph(type: .Bar, data: self.map{ $0 }, range: range, textDisplayHandler: textDisplayHandler) } @@ -42,14 +42,14 @@ extension SequenceType where Generator.Element: GraphData { public func lineGraph( range: GraphRange? = nil, textDisplayHandler: Graph.GraphTextDisplayHandler? = nil - ) -> Graph { + ) -> Graph { return Graph(type: .Line, data: self.map{ $0 }, range: range, textDisplayHandler: textDisplayHandler) } public func pieGraph( textDisplayHandler: Graph.GraphTextDisplayHandler? = nil - ) -> Graph { + ) -> Graph { return Graph(type: .Pie, data: self.map{ $0 }, range: nil, textDisplayHandler: textDisplayHandler) } @@ -60,33 +60,33 @@ extension SequenceType where Generator.Element: GraphData { SequenceType -> 'Graph' object */ -extension SequenceType where Generator.Element: NumericType { +extension Sequence where Iterator.Element: NumericType { public func barGraph( - range: GraphRange? = nil, - textDisplayHandler: Graph.GraphTextDisplayHandler? = nil - ) -> Graph { + range: GraphRange? = nil, + textDisplayHandler: Graph.GraphTextDisplayHandler? = nil + ) -> Graph { - return Graph(type: .Bar, array: self.map{ $0 }, range: range, textDisplayHandler: textDisplayHandler) + return Graph(type: .Bar, array: self.map{ $0 }, range: range, textDisplayHandler: textDisplayHandler) } public func lineGraph( - range: GraphRange? = nil, - textDisplayHandler: Graph.GraphTextDisplayHandler? = nil - ) -> Graph { + range: GraphRange? = nil, + textDisplayHandler: Graph.GraphTextDisplayHandler? = nil + ) -> Graph { - return Graph(type: .Line, array: self.map{ $0 }, range: range, textDisplayHandler: textDisplayHandler) + return Graph(type: .Line, array: self.map{ $0 }, range: range, textDisplayHandler: textDisplayHandler) } public func pieGraph( - textDisplayHandler: Graph.GraphTextDisplayHandler? = nil - ) -> Graph { + textDisplayHandler: Graph.GraphTextDisplayHandler? = nil + ) -> Graph { - return Graph(type: .Pie, array: self.map{ $0 }, range: nil, textDisplayHandler: textDisplayHandler) + return Graph(type: .Pie, array: self.map{ $0 }, range: nil, textDisplayHandler: textDisplayHandler) } - + } @@ -94,7 +94,7 @@ extension SequenceType where Generator.Element: NumericType { Dictionary -> 'Graph' object */ -extension CollectionType where Self: DictionaryLiteralConvertible, Self.Key: Hashable, Self.Value: NumericType, Generator.Element == (Self.Key, Self.Value) { +extension Collection where Self: ExpressibleByDictionaryLiteral, Self.Key: Hashable, Self.Value: NumericType, Iterator.Element == (Self.Key, Self.Value) { typealias aKey = Self.Key @@ -178,7 +178,7 @@ enum DefaultColorType { tail.append(arr[i]) } } - return [arr[randomIndex]] + randomArray(tail) + return [arr[randomIndex]] + randomArray(arr: tail) } return Array(0 ..< count).map({ $0 }).map({ UIColor(hue: CGFloat($0) / CGFloat(count), saturation: 0.9, brightness: 0.9, alpha: 1.0) }) @@ -201,7 +201,9 @@ public extension UIColor { let prefixHex = {(str) -> String in for prefix in ["0x", "0X", "#"] { if str.hasPrefix(prefix) { - return str.substringFromIndex(str.startIndex.advancedBy(prefix.characters.count)) + let index = str.index(str.startIndex, offsetBy: prefix.characters.count) + return str.substring(from: index) + } } return str @@ -213,9 +215,9 @@ public extension UIColor { return } - let scanner = NSScanner(string: prefixHex) + let scanner = Scanner(string: prefixHex) var hexInt: UInt64 = 0 - if !scanner.scanHexLongLong(&hexInt) { + if !scanner.scanHexInt64(&hexInt) { self.init(white: 0.0, alpha: 1.0) return } @@ -253,7 +255,7 @@ extension NSAttributedString { class func graphAttributedString(string: String, color: UIColor, font: UIFont) -> NSAttributedString { let paragraph = NSMutableParagraphStyle() - paragraph.alignment = .Center + paragraph.alignment = .center return NSAttributedString(string: string, attributes: [ NSForegroundColorAttributeName:color, diff --git a/Graphs/LineGraphView.swift b/Graphs/LineGraphView.swift index 0bfc6b6..4da924b 100644 --- a/Graphs/LineGraphView.swift +++ b/Graphs/LineGraphView.swift @@ -30,10 +30,10 @@ public struct LineGraphViewConfig { self.lineColor = lineColor ?? DefaultColorType.Line.color() self.lineWidth = lineWidth ?? 3.0 self.textColor = textColor ?? DefaultColorType.LineText.color() - self.textFont = textFont ?? UIFont.systemFontOfSize(10.0) + self.textFont = textFont ?? UIFont.systemFont(ofSize: 10.0) self.dotEnable = true self.dotDiameter = dotDiameter ?? 10.0 - self.contentInsets = contentInsets ?? UIEdgeInsetsZero + self.contentInsets = contentInsets ?? UIEdgeInsets.zero } } @@ -53,62 +53,69 @@ internal class LineGraphView: UIView { self.config = LineGraphViewConfig() super.init(frame: frame) - self.backgroundColor = UIColor.clearColor() + self.backgroundColor = UIColor.clear self.graph = graph } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } func setLineGraphViewConfig(config: LineGraphViewConfig?) { self.config = config ?? LineGraphViewConfig() self.setNeedsDisplay() } - override func drawRect(rect: CGRect) { - super.drawRect(rect) + override func draw(_ rect: CGRect) { + super.draw(rect) guard let lineGraph = self.graph else { return } let rect = self.graphFrame() let total = lineGraph.units.map{ $0.value }.reduce(U(0)){ $0 + $1 } let sectionWidth = rect.width / CGFloat(lineGraph.units.count) - let ps = self.points(lineGraph, rect: rect) + let ps = self.points(graph: lineGraph, rect: rect) let context = UIGraphicsGetCurrentContext() - CGContextSetStrokeColorWithColor(context, config.lineColor.CGColor ?? UIColor.blackColor().CGColor) - CGContextSetLineWidth(context, self.config.lineWidth) + context?.setStrokeColor(config.lineColor.cgColor) + context?.setLineWidth(config.lineWidth) + ps.forEach({point in if point == ps.first { - CGContextMoveToPoint(context, point.x, point.y) + context?.move(to: CGPoint(x: point.x, y: point.y)) + + } else { - CGContextAddLineToPoint(context, point.x, point.y) - CGContextStrokePath(context) - CGContextMoveToPoint(context, point.x, point.y) + context?.addLine(to: CGPoint(x: point.x, y: point.y)) + context?.strokePath() + context?.move(to: CGPoint(x: point.x, y: point.y)) } }) - - CGContextSetLineWidth(context, 0.0) - CGContextSetFillColorWithColor(context, config.lineColor.CGColor ?? UIColor.blackColor().CGColor) + context?.setLineWidth(0.0) + context?.setFillColor(config.lineColor.cgColor) + if self.config.dotEnable { ps.forEach({point in let r = CGRect(x: point.x - CGFloat(self.config.dotDiameter / 2.0), y: point.y - CGFloat(self.config.dotDiameter / 2.0), width: CGFloat(self.config.dotDiameter), height: CGFloat(self.config.dotDiameter)) - CGContextStrokeEllipseInRect(context, r) - CGContextFillEllipseInRect(context, r) + context?.strokeEllipse(in: r) + context?.fillEllipse(in: r) }) } zip(lineGraph.units, ps).forEach { (u, p) in - guard let str = self.graph?.graphTextDisplay()(unit: u, totalValue: total) else { + guard let str = self.graph?.graphTextDisplay()(u, total) else { return } - let attrStr = NSAttributedString.graphAttributedString(str, color: self.config.textColor, font: self.config.textFont) + let attrStr = NSAttributedString.graphAttributedString(string: str, color: self.config.textColor, font: self.config.textFont) let size = attrStr.size() - attrStr.drawInRect( - CGRect( + attrStr.draw( + in: CGRect( origin: CGPoint( x: p.x - sectionWidth / 2.0, y: u.value >= U(0) @@ -137,7 +144,7 @@ internal class LineGraphView: UIView { let sectionWidth = rect.width / CGFloat(graph.units.count) - return graph.units.enumerate().map { + return graph.units.enumerated().map { CGPoint( x: CGFloat($0) * sectionWidth + (sectionWidth / 2.0) + rect.origin.x, y: rect.size.height - rect.size.height * CGFloat(($1.value - graph.range.min).floatValue() / (graph.range.max - graph.range.min).floatValue()) + rect.origin.y diff --git a/Graphs/MultiBarGraphView.swift b/Graphs/MultiBarGraphView.swift index 975c9a3..aa6fb7b 100644 --- a/Graphs/MultiBarGraphView.swift +++ b/Graphs/MultiBarGraphView.swift @@ -25,6 +25,10 @@ class MultiBarGraphView: UIView { ) self.addSubview(self.scrollView) } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } } struct MultiBarGraphViewConfig { diff --git a/Graphs/PieGraphView.swift b/Graphs/PieGraphView.swift index 5f6b1e1..df6dcc2 100644 --- a/Graphs/PieGraphView.swift +++ b/Graphs/PieGraphView.swift @@ -25,9 +25,9 @@ public struct PieGraphViewConfig { ) { self.pieColors = pieColors self.textColor = textColor ?? DefaultColorType.PieText.color() - self.textFont = textFont ?? UIFont.systemFontOfSize(10.0) + self.textFont = textFont ?? UIFont.systemFont(ofSize: 10.0) self.isDounut = isDounut - self.contentInsets = contentInsets ?? UIEdgeInsetsZero + self.contentInsets = contentInsets ?? UIEdgeInsets.zero } } @@ -36,7 +36,7 @@ internal class PieGraphView: UIView { private var graph: PieGraph? { didSet { - self.config.pieColors = DefaultColorType.pieColors(graph?.units.count ?? 0) + self.config.pieColors = DefaultColorType.pieColors(count: graph?.units.count ?? 0) self.setNeedsDisplay() } } @@ -44,33 +44,40 @@ internal class PieGraphView: UIView { init(frame: CGRect, graph: PieGraph?) { - self.config = PieGraphViewConfig(pieColors: DefaultColorType.pieColors(graph?.units.count ?? 0)) + self.config = PieGraphViewConfig(pieColors: DefaultColorType.pieColors(count: graph?.units.count ?? 0)) super.init(frame: frame) - self.backgroundColor = UIColor.clearColor() + self.backgroundColor = UIColor.clear self.graph = graph } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } func setPieGraphViewConfig(config: PieGraphViewConfig?) { self.config = config ?? PieGraphViewConfig() self.setNeedsDisplay() } - override func drawRect(rect: CGRect) { - super.drawRect(rect) + override func draw(_ rect: CGRect) { + super.draw(rect) guard let graph = self.graph else { return } func convert(s: S, arr: [S], f: (S) -> S) -> [S] { switch arr.match { - case let .Some(h, t): return [f(h) + s] + convert(h + s, arr:t, f: f) - case .None: return [] + case let .some(h, t): + let d:S = f(h) + s + return [d] + convert(s: h + s, arr:t, f: f) + case .none: + return [] } } - let colors = self.config.pieColors ?? DefaultColorType.pieColors(graph.units.count) + let colors = self.config.pieColors ?? DefaultColorType.pieColors(count: graph.units.count) let values = graph.units.map({ max($0.value, U(0)) }) - let total = values.reduce(U(0), combine: { $0 + $1 }) + let total = values.reduce(U(0), { $0 + $1 }) let percentages = values.map({ Double($0.floatValue() / total.floatValue()) }) let rect = self.graphFrame() @@ -80,7 +87,7 @@ internal class PieGraphView: UIView { let y = rect.size.height / 2.0 + rect.origin.y let radius = min(rect.width, rect.height) / 2.0 - let centers = convert(0.0, arr: percentages) { $0 / 2.0 }.map { (c) -> CGPoint in + let centers = convert(s: 0.0, arr: percentages) { $0 / 2.0 }.map { (c) -> CGPoint in let angle = M_PI * 2.0 * c - M_PI / 2.0 return CGPoint( x: Double(x) + cos(angle) * Double(radius * 3.0 / 4.0), @@ -90,40 +97,41 @@ internal class PieGraphView: UIView { var startAngle = -M_PI / 2.0 - percentages.enumerate().forEach { (index, f) in + percentages.enumerated().forEach { (index, f) in let endAngle = startAngle + M_PI * 2.0 * f - CGContextMoveToPoint(context, x, y); - CGContextAddArc(context, x, y, radius, CGFloat(startAngle), CGFloat(endAngle), 0); + context?.move(to: CGPoint(x: x, y: y)) + context?.addArc(center: CGPoint(x: x, y: y), radius: radius, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: false) + if self.config.isDounut { - CGContextAddArc(context, x, y, radius/2, CGFloat(endAngle), CGFloat(startAngle), 1) + context?.addArc(center: CGPoint(x: x, y: y), radius: radius * 0.5, startAngle: CGFloat(endAngle), endAngle: CGFloat(startAngle), clockwise: true) } + context?.setFillColor(colors[index].cgColor) + context?.closePath() + context?.fillPath() - CGContextSetFillColor(context, CGColorGetComponents( colors[index].CGColor )) - CGContextClosePath(context); - CGContextFillPath(context); startAngle = endAngle } zip(graph.units, centers).forEach { (u, center) in - guard let str = self.graph?.graphTextDisplay()(unit: u, totalValue: total) else { + guard let str = self.graph?.graphTextDisplay()(u, total) else { return } let paragraph = NSMutableParagraphStyle() - paragraph.alignment = .Center + paragraph.alignment = .center let attrStr = NSAttributedString(string: str, attributes: [ NSForegroundColorAttributeName:self.config.textColor, - NSFontAttributeName: UIFont.systemFontOfSize(10.0), + NSFontAttributeName: UIFont.systemFont(ofSize: 10.0), NSParagraphStyleAttributeName: paragraph ]) let size = attrStr.size() - attrStr.drawInRect( - CGRect( + attrStr.draw( + in: CGRect( origin: CGPoint( x: center.x - size.width / 2.0, y: center.y - size.height / 2.0