Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Graphs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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";
Expand Down
26 changes: 15 additions & 11 deletions Graphs/BarGraphView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -63,9 +63,13 @@ internal class BarGraphView<T: Hashable, U: NumericType>: 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?) {

Expand All @@ -82,8 +86,8 @@ internal class BarGraphView<T: Hashable, U: NumericType>: UIView {
)
}

override func drawRect(rect: CGRect) {
super.drawRect(rect)
override func draw(_ rect: CGRect) {
super.draw(rect)

guard let graph = self.graph else { return }

Expand All @@ -97,7 +101,7 @@ internal class BarGraphView<T: Hashable, U: NumericType>: 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()
Expand Down Expand Up @@ -129,14 +133,14 @@ internal class BarGraphView<T: Hashable, U: NumericType>: 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)
Expand All @@ -152,4 +156,4 @@ internal class BarGraphView<T: Hashable, U: NumericType>: UIView {
}
})
}
}
}
40 changes: 20 additions & 20 deletions Graphs/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public enum GraphType {

public class Graph<T: Hashable, U: NumericType> {

public typealias GraphTextDisplayHandler = (unit: GraphUnit<T, U>, totalValue: U) -> String?
public typealias GraphTextDisplayHandler = (_ unit: GraphUnit<T, U>, _ totalValue: U) -> String?

let kind: GraphKind<T, U>


init(barGraph: BarGraph<T, U>) {
self.kind = GraphKind<T, U>.Bar(barGraph)
}

init(lineGraph: LineGraph<T, U>) {
self.kind = GraphKind<T, U>.Line(lineGraph)
}
Expand All @@ -37,7 +37,7 @@ public class Graph<T: Hashable, U: NumericType> {

public extension Graph {

public convenience init<S: GraphData where S.GraphDataKey == T, S.GraphDataValue == U>(type: GraphType, data: [S], min minOrNil: U? = nil, max maxOrNil: U? = nil, textDisplayHandler: GraphTextDisplayHandler? = nil) {
public convenience init<S: GraphData>(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<U>? in
if let min = minOrNil, let max = maxOrNil {
Expand All @@ -49,11 +49,11 @@ public extension Graph {
self.init(type: type, data: data, range: range(), textDisplayHandler: textDisplayHandler)
}

public convenience init<S: GraphData where S.GraphDataKey == T, S.GraphDataValue == U>(type: GraphType, data: [S], range rangeOrNil: GraphRange<U>? = nil, textDisplayHandler: GraphTextDisplayHandler? = nil) {
public convenience init<S: GraphData>(type: GraphType, data: [S], range rangeOrNil: GraphRange<U>? = nil, textDisplayHandler: GraphTextDisplayHandler? = nil) where S.GraphDataKey == T, S.GraphDataValue == U {

let r = {() -> GraphRange<U> 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<U>(
min: sorted.first?.value ?? U(0),
max: sorted.last?.value ?? U(0)
Expand Down Expand Up @@ -103,7 +103,7 @@ public extension Graph {

let r = {() -> GraphRange<U> in
if let r = rangeOrNil { return r }
let sorted = array.sort{ $0 < $1 }
let sorted = array.sorted{ $0 < $1 }
return GraphRange<U>(
min: sorted.first ?? U(0),
max: sorted.last ?? U(0)
Expand Down Expand Up @@ -153,7 +153,7 @@ public extension Graph {

public convenience init(type: GraphType, dictionary: [T: U], range rangeOrNil: GraphRange<U>? = nil, textDisplayHandler: GraphTextDisplayHandler? = nil) {

let sorted = dictionary.sort{ $0.1 < $1.1 }
let sorted = dictionary.sorted{ $0.1 < $1.1 }

let r = {() -> GraphRange<U> in
if let r = rangeOrNil { return r }
Expand Down Expand Up @@ -261,7 +261,7 @@ internal struct BarGraph<T: Hashable, U: NumericType>: GraphBase {
if let f = textDisplayHandler {
return f
}
return { (unit, total) -> String? in String(unit.value) }
return { (unit, total) -> String? in String(describing: unit.value) }
}
}

Expand All @@ -279,7 +279,7 @@ internal struct MultiBarGraph<T: Hashable, U: NumericType>: GraphBase {
if let f = textDisplayHandler {
return f
}
return { (unit, total) -> String? in String(unit.value) }
return { (unit, total) -> String? in String(describing: unit.value) }
}
}

Expand Down Expand Up @@ -312,7 +312,7 @@ internal struct LineGraph<T: Hashable, U: NumericType>: GraphBase {
if let f = textDisplayHandler {
return f
}
return { (unit, total) -> String? in String(unit.value) }
return { (unit, total) -> String? in String(describing: unit.value) }
}
}

Expand Down Expand Up @@ -344,7 +344,7 @@ internal struct PieGraph<T: Hashable, U: NumericType>: 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)
}
}
}
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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)
}
Expand Down
28 changes: 14 additions & 14 deletions Graphs/GraphView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ public class GraphView<T: Hashable, U: NumericType>: 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<T, U>? = 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()
}

Expand All @@ -43,27 +43,27 @@ public class GraphView<T: Hashable, U: NumericType>: 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)
}
Expand All @@ -84,7 +84,7 @@ extension GraphView {
self.barGraphConfig = configuration()
self.subviews.forEach { (v) in
if let barGraphView = v as? BarGraphView<T, U> {
barGraphView.setBarGraphViewConfig(barGraphConfig)
barGraphView.setBarGraphViewConfig(config: barGraphConfig)
}
}
return self
Expand All @@ -94,7 +94,7 @@ extension GraphView {
self.lineGraphConfig = configuration()
self.subviews.forEach { (v) in
if let lineGraphView = v as? LineGraphView<T, U> {
lineGraphView.setLineGraphViewConfig(lineGraphConfig)
lineGraphView.setLineGraphViewConfig(config: lineGraphConfig)
}
}
return self
Expand All @@ -104,7 +104,7 @@ extension GraphView {
self.pieGraphConfig = configuration()
self.subviews.forEach { (v) in
if let pieGraphView = v as? PieGraphView<T, U> {
pieGraphView.setPieGraphViewConfig(pieGraphConfig)
pieGraphView.setPieGraphViewConfig(config: pieGraphConfig)
}
}
return self
Expand Down
Loading