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
2323var isDataEmpty = false
2424
25- /// description for each column
26- var labels = [ String] ( )
27-
2825/// current number of chart
2926var currentChart : Int = 0
3027
@@ -45,26 +42,52 @@ open class HamsChartBase:UIControl {
4542/// default is frame.height - chartFooterHeight - 10
4643var 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
4956open var filledStyle : HamsBackgoundStyle ?
57+
5058open var offsets : ChartOffset !
59+
5160open var pageIndicatorTintColor : UIColor ?
61+
5262open var labelStyle : HamsLabelStyle !
63+
64+ open var labelsColor : UIColor = . white
65+
5366open var chartHeaderHeight : CGFloat !
67+
5468open var chartFooterHeight : CGFloat ! {
5569didSet {
5670base = frame. height - chartFooterHeight - 10
5771}
5872}
5973
74+ /// change title label in header
6075open var title : String = " Title "
76+
77+ /// change title color
6178open var titleColor : UIColor = . white
62- open var labelsColor : UIColor = . white
6379
80+ /// variables can be changed in configureForChart
6481func configure( ) {
6582removeLabel ( )
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+
6891title = " Title "
6992titleColor = . white
7093offsets = ChartOffset ( top: 0 , bottom: 0 , column: 30 , horizon: 50 )
@@ -75,8 +98,9 @@ open class HamsChartBase:UIControl {
7598pageControl. widthAnchor. constraint ( equalToConstant: chartFooterHeight+ 5 ) . isActive = true
7699}
77100
101+ /// set title in header
78102func 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 ) )
80104titleLabel. text = title
81105titleLabel. font = UIFont . systemFont ( ofSize: 24 , weight: UIFontWeightSemibold)
82106titleLabel. adjustsFontSizeToFitWidth = true
@@ -86,13 +110,17 @@ open class HamsChartBase:UIControl {
86110self . addSubview ( titleLabel)
87111}
88112
89-
113+ /// the subclass must override this variable, default is 0
90114open var numberOfCharts : Int { return 0 }
91-
115+
116+ /// the subclass must override this function, default return 0
92117open func numberOfValues( in chart: Int ) -> Int { return 0 }
93118
119+ /// reload function for redraw chart and reload data
94120open func reloadData( ) { }
95121
122+ /// get number of charts and values from controller
123+ /// reload data
96124func update( ) {
97125pageControl. numberOfPages = numberOfCharts
98126chartValues = [ ]
@@ -109,6 +137,7 @@ open class HamsChartBase:UIControl {
109137}
110138}
111139
140+ /// setup conponent when chart is created
112141func setup( ) {
113142
114143self . backgroundColor = . clear
@@ -119,7 +148,6 @@ open class HamsChartBase:UIControl {
119148pageControl. centerXAnchor. constraint ( equalTo: self . centerXAnchor) . isActive = true
120149pageControl. bottomAnchor. constraint ( equalTo: self . bottomAnchor) . isActive = true
121150
122-
123151self . swipeGestureLeft. direction = . left
124152self . swipeGestureRight. direction = . right
125153
@@ -131,10 +159,11 @@ open class HamsChartBase:UIControl {
131159self . addGestureRecognizer ( self . swipeGestureLeft)
132160self . addGestureRecognizer ( self . swipeGestureRight)
133161
134-
135162configure ( )
136163}
137164
165+ /// remove label by lable tag
166+ /// only call it when you use addSubview after reloadData()
138167func removeLabel( tag: Int ) {
139168for sub in self . subviews {
140169if 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()
146177func removeLabel( ) {
147178for subview in self . subviews {
148179if 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
155186func capture( ) -> UIImageView {
156187let renderer = UIGraphicsImageRenderer ( size: self . bounds. size)
157188let image = renderer. image { ctx in
0 commit comments