ICMainViewController.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // ICMainViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/9/8.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import ObjectMapper
  13. import SwiftyJSON
  14. import Charts
  15. import CocoaLumberjack
  16. import O2OA_Auth_SDK
  17. class ICMainViewController: UIViewController {
  18. var pieEntrys:[AttendanceStatusType:AttendanceTotalEntry] = [:]
  19. @IBOutlet weak var chartView: PieChartView!
  20. var button:UIButton?
  21. var window:UIWindow?
  22. override func viewWillAppear(_ animated: Bool) {
  23. self.window?.isHidden = false
  24. }
  25. override func viewWillDisappear(_ animated: Bool) {
  26. self.window?.isHidden = true
  27. }
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. self.createButton()
  31. self.initChartView()
  32. self.loadICTotalData()
  33. }
  34. func loadICTotalData(){
  35. let filter = AttendanceDetailWrapInFilter()
  36. let date = Date()
  37. let result = calcTimeComponetFromDate(date: date)
  38. filter.cycleYear = result.year
  39. filter.cycleMonth = result.month
  40. filter.order = "asc"
  41. filter.q_empName = O2AuthSDK.shared.myInfo()?.distinguishedName
  42. self.loadDataFromFilter(filter: filter)
  43. }
  44. func loadDataFromFilter(filter:AttendanceDetailWrapInFilter){
  45. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(icContext.icContextKey, query: icContext.detailMonthPieChartQuery, parameter: nil)
  46. DDLogDebug(url!)
  47. DDLogDebug(filter.toJSONString()!)
  48. Alamofire.request(url!, method: .put, parameters: filter.toJSON(), encoding:JSONEncoding.default, headers: nil).responseJSON { (response) in
  49. switch response.result {
  50. case .success(let val):
  51. let type = JSON(val)["type"]
  52. if type == "success"{
  53. let dataJSON = JSON(val)["data"]
  54. self.setChartViewData(json: dataJSON)
  55. }else{
  56. DDLogError(JSON(val).description)
  57. }
  58. case .failure(let err):
  59. DDLogError(err.localizedDescription)
  60. }
  61. }
  62. //self.setChartViewData(json: nil)
  63. }
  64. func setChartViewData(json:JSON?){
  65. //AttendanceDetailDataArray
  66. if let detailJSON = json {
  67. let detailArray = Mapper<AttendanceDetailData>().mapArray(JSONString: detailJSON.description)
  68. //生成Dictonary
  69. if detailArray != nil {
  70. for d in detailArray! {
  71. let t = calcAttendanceStatus(attendance: d)
  72. if self.pieEntrys.has(t.statusType) {
  73. let entry:AttendanceTotalEntry = self.pieEntrys[t.statusType]!
  74. entry.incCount()
  75. self.pieEntrys[t.statusType] = entry
  76. }else{
  77. let entry = AttendanceTotalEntry(label: t.statusType.rawValue, type: t.statusType)
  78. entry.incCount()
  79. self.pieEntrys[t.statusType] = entry
  80. }
  81. }
  82. var entrys:[PieChartDataEntry] = []
  83. for d in self.pieEntrys.enumerated() {
  84. let entry = d.element.value
  85. let cEntry = PieChartDataEntry(value: Double(entry.count), label: entry.label, data: entry)
  86. entrys.append(cEntry)
  87. }
  88. let pieDataSet = PieChartDataSet(values: entrys, label: "")
  89. //pieDataSet颜色
  90. var colors:[NSUIColor] = []
  91. //colors.append(contentsOf: ChartColorTemplates.vordiplom())
  92. // colors.append(contentsOf: ChartColorTemplates.joyful())
  93. colors.append(contentsOf: ChartColorTemplates.colorful())
  94. // colors.append(contentsOf: ChartColorTemplates.liberty())
  95. // colors.append(contentsOf: ChartColorTemplates.pastel())
  96. //pieDataSet.entryLabelFont = UIFont(name: "PingFangSC-Regular", size: 15.0)!
  97. //pieDataSet.entryLabelColor = RGB(18, g: 18, b: 18)
  98. pieDataSet.colors = colors
  99. let pieData = PieChartData(dataSets: [pieDataSet])
  100. //pieData数据格式
  101. let pFormatter = NumberFormatter()
  102. pFormatter.numberStyle = NumberFormatter.Style.percent
  103. pFormatter.maximumFractionDigits = 1
  104. pFormatter.multiplier = 1.0
  105. pFormatter.percentSymbol = " %"
  106. pieData.setValueFormatter(DefaultValueFormatter.init(formatter: pFormatter))
  107. //
  108. pieData.setValueFont(UIFont(name: "PingFangSC-Regular", size: 12.0)!)
  109. pieData.setValueTextColor(UIColor.white)
  110. //设置数据
  111. self.chartView.data = pieData
  112. //通知数据表更新
  113. self.chartView.notifyDataSetChanged()
  114. }
  115. }
  116. }
  117. override func didReceiveMemoryWarning() {
  118. super.didReceiveMemoryWarning()
  119. // Dispose of any resources that can be recreated.
  120. }
  121. func initChartView(){
  122. chartView.delegate = self
  123. chartView.usePercentValuesEnabled = true
  124. chartView.drawSlicesUnderHoleEnabled = true
  125. chartView.holeRadiusPercent = 0.58
  126. chartView.transparentCircleRadiusPercent = 0.61
  127. chartView.chartDescription?.enabled = false
  128. chartView.setExtraOffsets(left: 5.0, top: 10.0, right: 5.0, bottom: 5.0)
  129. //无数据文本样式
  130. chartView.noDataText = "您这个月还没有考勤记录"
  131. chartView.noDataFont = UIFont(name: "PingFangSC-Regular", size: 20.0)!
  132. chartView.noDataTextColor = RGB(108, g: 108, b: 108)
  133. //中心文本样式
  134. chartView.drawCenterTextEnabled = true
  135. let paragraphStyle:NSMutableParagraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
  136. paragraphStyle.lineBreakMode = NSLineBreakMode.byTruncatingTail
  137. paragraphStyle.alignment = NSTextAlignment.center
  138. let centerText:NSMutableAttributedString = NSMutableAttributedString(string: "考勤月报")
  139. centerText.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "PingFangSC-Regular", size: 15.0)!, range: NSMakeRange(0, centerText.length))
  140. centerText.addAttribute(NSAttributedString.Key.foregroundColor, value: RGB(18,g:18,b:18), range: NSMakeRange(0, centerText.length))
  141. chartView.centerAttributedText = centerText
  142. chartView.drawHoleEnabled = true
  143. chartView.rotationAngle = 0.0
  144. chartView.rotationEnabled = true
  145. chartView.highlightPerTapEnabled = true
  146. //图例样式
  147. let l:Legend = chartView.legend
  148. l.horizontalAlignment = Legend.HorizontalAlignment.left
  149. l.verticalAlignment = Legend.VerticalAlignment.bottom
  150. l.orientation = Legend.Orientation.horizontal
  151. // l.horizontalAlignment =
  152. // l.verticalAlignment = ChartLegendVerticalAlignmentTop
  153. // l.orientation = ChartLegendOrientationVertical
  154. l.drawInside = false
  155. l.xEntrySpace = 7.0
  156. l.yEntrySpace = 0.0
  157. l.yOffset = 20
  158. l.font = UIFont(name: "PingFangSC-Regular", size: 12.0)!
  159. l.textColor = RGB(18, g: 18, b: 18)
  160. //entry文本样式
  161. }
  162. func createButton(){
  163. let width = SCREEN_WIDTH
  164. let height = SCREEN_HEIGHT
  165. let frame = CGRect(x: 0, y: 0, w: 60, h: 60)
  166. button = UIButton(frame: frame)
  167. button?.setImage(UIImage(named:"icon_fab_list"), for: .normal)
  168. button?.addTarget(self, action: #selector(self.showDetail(sender:)), for: .touchUpInside)
  169. window = UIWindow.init(frame: CGRect(x:width - 80 , y: height - 80, w: 60, h: 60))
  170. window?.windowLevel = UIWindow.Level.alert + 1
  171. window?.backgroundColor = RGB(46, g: 204, b: 113)
  172. window?.layer.cornerRadius = 30
  173. window?.layer.masksToBounds = true
  174. window?.addSubview(button!)
  175. window?.makeKeyAndVisible()
  176. }
  177. @objc func showDetail(sender:UIButton){
  178. self.performSegue(withIdentifier: "showICDetailSegue", sender: nil)
  179. }
  180. @IBAction func showCheckAppeal(_ sender: UIBarButtonItem) {
  181. self.performSegue(withIdentifier: "showCheckAppealSegue", sender: nil)
  182. }
  183. @IBAction func backToSuper(_ sender: UIBarButtonItem) {
  184. let backType = AppConfigSettings.shared.appBackType
  185. if backType == 1 {
  186. self.performSegue(withIdentifier: "backToMain", sender: nil)
  187. }else if backType == 2 {
  188. self.performSegue(withIdentifier: "backToApps", sender: nil)
  189. }
  190. }
  191. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  192. let date = Date()
  193. let result = calcTimeComponetFromDate(date: date)
  194. if segue.identifier == "showICDetailSegue" {
  195. let destVC = segue.destination as! ICDetailViewController
  196. destVC.currentTime = result
  197. }else if segue.identifier == "" {
  198. }
  199. }
  200. }
  201. extension ICMainViewController:ChartViewDelegate{
  202. func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
  203. }
  204. func chartValueNothingSelected(_ chartView: ChartViewBase) {
  205. }
  206. func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat) {
  207. }
  208. func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat) {
  209. }
  210. }