Skip to content

Commit 0228e17

Browse files
author
Howard Wang
committed
add bar chart
1 parent b2b12f2 commit 0228e17

15 files changed

+170
-74
lines changed

HamsterUIKit/ChartOffset.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//
88

99
import Foundation
10+
11+
/// it will change the display of chart
1012
public struct ChartOffset {
1113
let top: CGFloat
1214
let bottom: CGFloat

HamsterUIKit/ChartsCore.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ open class ChartsCore {
5555
}
5656

5757
var unitWidth: CGFloat {
58-
return columnWidth + gapBetweenColumns
58+
return columnWidth + gapWidth
5959
}
6060

6161
var contentMidWidth: CGFloat {
@@ -66,9 +66,7 @@ open class ChartsCore {
6666

6767
switch alignment {
6868
case .justified:
69-
let spacer = (chartWidth - horizonMargin * 2 - columnWidth * 2) /
70-
CGFloat((values.count - 1))
71-
var x:CGFloat = CGFloat(column) * spacer
69+
var x:CGFloat = CGFloat(column) * gapWidth
7270
x += horizonMargin + columnWidth/2
7371
return x
7472
case .left:
@@ -101,6 +99,10 @@ open class ChartsCore {
10199
set {
102100
gapBetweenColumns = newValue
103101
} get {
102+
if alignment == .justified {
103+
return (chartWidth - horizonMargin * 2 - columnWidth * 2) /
104+
CGFloat((values.count - 1))
105+
}
104106
return gapBetweenColumns
105107
}
106108
}

HamsterUIKit/HamsBackgoundStyle.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import Foundation
10+
1011
public enum HamsBackgoundStyle {
1112
case plain(UIColor) // single color
1213
case gradient(top: UIColor,bottom: UIColor) // from top to bottom

HamsterUIKit/HamsBarChart.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22
// GradientedBarChat.swift
33
// Hamster
44
//
5-
// Created by Drake on 2/12/17.
6-
// Copyright © 2017 Jiongzhi Wang. All rights reserved.
5+
// Created by Howard on 2/12/17.
6+
// Copyright © 2017 Howard Wang. All rights reserved.
77
//
88

99
import UIKit
1010

11+
/// the protocol represnts the data model object
1112
public protocol HamsBarChartDataSource{
13+
/// implement this method to customize rectangular bars
1214
func barChart(_ barChart: HamsBarChart, barForChart indexPath: HamsIndexPath) -> HamsBarChartRect
15+
16+
/// set number of charts
1317
func numberOfCharts(in barChart: HamsBarChart) -> Int
18+
19+
/// set number of values in one chart
1420
func barChart(_ barChart: HamsBarChart, numberOfValuesInChart chart: Int) -> Int
1521
}
1622

17-
@objc public protocol HamsBarChartDelegate {
23+
/// this protocol represents the display and behaviour of the charts
24+
@objc
25+
public protocol HamsBarChartDelegate {
26+
/// should use this method to configue each chart
1827
@objc optional func barChart(_ barChart: HamsBarChart, configureForCharts chart: Int)
1928
}
2029

@@ -32,6 +41,7 @@ open class HamsBarChart: HamsChartBase {
3241
return count
3342
}
3443

44+
/// this will reload data and display
3545
open override func reloadData() {
3646
if dataSource != nil {
3747
self.configure()

HamsterUIKit/HamsBarChartRect.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// HamsBarChartRect.swift
33
// HamsterUIKit
44
//
5-
// Created by Drake on 4/25/17.
5+
// Created by Howard on 4/25/17.
66
// Copyright © 2017 Howard Wang. All rights reserved.
77
//
88

HamsterUIKit/HamsChartBase.swift

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// HamsChartBase.swift
33
// HamsterUIKit
44
//
5-
// Created by Drake on 4/28/17.
5+
// Created by Howard on 4/28/17.
66
// Copyright © 2017 Howard Wang. All rights reserved.
77
//
88

@@ -22,9 +22,6 @@ open class HamsChartBase:UIControl {
2222
/// default is false
2323
var isDataEmpty = false
2424

25-
/// description for each column
26-
var labels = [String]()
27-
2825
/// current number of chart
2926
var currentChart: Int = 0
3027

@@ -45,26 +42,52 @@ open class HamsChartBase:UIControl {
4542
/// default is frame.height - chartFooterHeight - 10
4643
var base: CGFloat = 0
4744

48-
///
45+
/// description for each column
46+
var labels = [String]()
47+
48+
/// label (week, costom label) height
49+
var labelHeight: CGFloat = 10
50+
51+
/// padding between chartHeaderHeight and content
52+
var paddingHeight: CGFloat = 20
53+
54+
/// in curve chart fill graph
55+
/// in barchart fill background
4956
open var filledStyle: HamsBackgoundStyle?
57+
5058
open var offsets: ChartOffset!
59+
5160
open var pageIndicatorTintColor:UIColor?
61+
5262
open var labelStyle: HamsLabelStyle!
63+
64+
open var labelsColor: UIColor = .white
65+
5366
open var chartHeaderHeight: CGFloat!
67+
5468
open var chartFooterHeight: CGFloat! {
5569
didSet {
5670
base = frame.height - chartFooterHeight - 10
5771
}
5872
}
5973

74+
/// change title label in header
6075
open var title:String = "Title"
76+
77+
/// change title color
6178
open var titleColor:UIColor = .white
62-
open var labelsColor: UIColor = .white
6379

80+
/// variables can be changed in configureForChart
6481
func configure(){
6582
removeLabel()
66-
chartHeaderHeight = frame.height/5 + 20
67-
chartFooterHeight = frame.height/5
83+
if frame.height/5 > 40.0 {
84+
chartFooterHeight = 60
85+
chartHeaderHeight = 60
86+
} else {
87+
chartHeaderHeight = frame.height/5 + 30
88+
chartFooterHeight = frame.height/5
89+
}
90+
6891
title = "Title"
6992
titleColor = .white
7093
offsets = ChartOffset(top: 0, bottom: 0, column: 30, horizon: 50)
@@ -75,8 +98,9 @@ open class HamsChartBase:UIControl {
7598
pageControl.widthAnchor.constraint(equalToConstant: chartFooterHeight+5).isActive = true
7699
}
77100

101+
/// set title in header
78102
func setTitle() {
79-
let titleLabel = UILabel(frame: CGRect( x: 0, y: chartHeaderHeight-39, width: frame.width, height: 29))
103+
let titleLabel = UILabel(frame: CGRect( x: 0, y: chartHeaderHeight-49, width: frame.width, height: 29))
80104
titleLabel.text = title
81105
titleLabel.font = UIFont.systemFont(ofSize: 24, weight: UIFontWeightSemibold)
82106
titleLabel.adjustsFontSizeToFitWidth = true
@@ -86,13 +110,17 @@ open class HamsChartBase:UIControl {
86110
self.addSubview(titleLabel)
87111
}
88112

89-
113+
/// the subclass must override this variable, default is 0
90114
open var numberOfCharts: Int { return 0 }
91-
115+
116+
/// the subclass must override this function, default return 0
92117
open func numberOfValues(in chart: Int) -> Int { return 0 }
93118

119+
/// reload function for redraw chart and reload data
94120
open func reloadData() { }
95121

122+
/// get number of charts and values from controller
123+
/// reload data
96124
func update() {
97125
pageControl.numberOfPages = numberOfCharts
98126
chartValues = []
@@ -109,6 +137,7 @@ open class HamsChartBase:UIControl {
109137
}
110138
}
111139

140+
/// setup conponent when chart is created
112141
func setup() {
113142

114143
self.backgroundColor = .clear
@@ -119,7 +148,6 @@ open class HamsChartBase:UIControl {
119148
pageControl.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
120149
pageControl.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
121150

122-
123151
self.swipeGestureLeft.direction = .left
124152
self.swipeGestureRight.direction = .right
125153

@@ -131,10 +159,11 @@ open class HamsChartBase:UIControl {
131159
self.addGestureRecognizer(self.swipeGestureLeft)
132160
self.addGestureRecognizer(self.swipeGestureRight)
133161

134-
135162
configure()
136163
}
137164

165+
/// remove label by lable tag
166+
/// only call it when you use addSubview after reloadData()
138167
func removeLabel(tag: Int) {
139168
for sub in self.subviews {
140169
if sub.tag == tag {
@@ -143,6 +172,8 @@ open class HamsChartBase:UIControl {
143172
}
144173
}
145174

175+
/// remove all labels
176+
/// only call it when you use addSubview after reloadData()
146177
func removeLabel() {
147178
for subview in self.subviews {
148179
if subview is UILabel {
@@ -151,7 +182,7 @@ open class HamsChartBase:UIControl {
151182
}
152183
}
153184

154-
/// capture a screenshot by
185+
/// capture a screenshot for swipe transition between two views
155186
func capture() -> UIImageView {
156187
let renderer = UIGraphicsImageRenderer(size: self.bounds.size)
157188
let image = renderer.image { ctx in

0 commit comments

Comments
 (0)