123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- //
- // RadarChartViewController.swift
- // ChartsDemo-iOS
- //
- // Created by Jacob Christie on 2017-07-09.
- // Copyright © 2017 jc. All rights reserved.
- //
- #if canImport(UIKit)
- import UIKit
- #endif
- import Charts
- class RadarChartViewController: DemoBaseViewController {
- @IBOutlet var chartView: RadarChartView!
-
- let activities = ["Burger", "Steak", "Salad", "Pasta", "Pizza"]
- var originalBarBgColor: UIColor!
- var originalBarTintColor: UIColor!
- var originalBarStyle: UIBarStyle!
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- self.title = "Radar Chart"
- self.options = [.toggleValues,
- .toggleHighlight,
- .toggleHighlightCircle,
- .toggleXLabels,
- .toggleYLabels,
- .toggleRotate,
- .toggleFilled,
- .animateX,
- .animateY,
- .animateXY,
- .spin,
- .saveToGallery,
- .toggleData]
-
- chartView.delegate = self
-
- chartView.chartDescription.enabled = false
- chartView.webLineWidth = 1
- chartView.innerWebLineWidth = 1
- chartView.webColor = .lightGray
- chartView.innerWebColor = .lightGray
- chartView.webAlpha = 1
-
- let marker = RadarMarkerView.viewFromXib()!
- marker.chartView = chartView
- chartView.marker = marker
-
- let xAxis = chartView.xAxis
- xAxis.labelFont = .systemFont(ofSize: 9, weight: .light)
- xAxis.xOffset = 0
- xAxis.yOffset = 0
- xAxis.valueFormatter = self
- xAxis.labelTextColor = .white
-
- let yAxis = chartView.yAxis
- yAxis.labelFont = .systemFont(ofSize: 9, weight: .light)
- yAxis.labelCount = 5
- yAxis.axisMinimum = 0
- yAxis.axisMaximum = 80
- yAxis.drawLabelsEnabled = false
-
- let l = chartView.legend
- l.horizontalAlignment = .center
- l.verticalAlignment = .top
- l.orientation = .horizontal
- l.drawInside = false
- l.font = .systemFont(ofSize: 10, weight: .light)
- l.xEntrySpace = 7
- l.yEntrySpace = 5
- l.textColor = .white
- // chartView.legend = l
- self.updateChartData()
-
- chartView.animate(xAxisDuration: 1.4, yAxisDuration: 1.4, easingOption: .easeOutBack)
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
-
- UIView.animate(withDuration: 0.15) {
- let navBar = self.navigationController!.navigationBar
- self.originalBarBgColor = navBar.barTintColor
- self.originalBarTintColor = navBar.tintColor
- self.originalBarStyle = navBar.barStyle
- navBar.barTintColor = self.view.backgroundColor
- navBar.tintColor = .white
- navBar.barStyle = .black
- }
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
-
- UIView.animate(withDuration: 0.15) {
- let navBar = self.navigationController!.navigationBar
- navBar.barTintColor = self.originalBarBgColor
- navBar.tintColor = self.originalBarTintColor
- navBar.barStyle = self.originalBarStyle
- }
- }
-
- override func updateChartData() {
- if self.shouldHideData {
- chartView.data = nil
- return
- }
-
- self.setChartData()
- }
-
- func setChartData() {
- let mult: UInt32 = 80
- let min: UInt32 = 20
- let cnt = 5
-
- let block: (Int) -> RadarChartDataEntry = { _ in return RadarChartDataEntry(value: Double(arc4random_uniform(mult) + min))}
- let entries1 = (0..<cnt).map(block)
- let entries2 = (0..<cnt).map(block)
-
- let set1 = RadarChartDataSet(entries: entries1, label: "Last Week")
- set1.setColor(UIColor(red: 103/255, green: 110/255, blue: 129/255, alpha: 1))
- set1.fillColor = UIColor(red: 103/255, green: 110/255, blue: 129/255, alpha: 1)
- set1.drawFilledEnabled = true
- set1.fillAlpha = 0.7
- set1.lineWidth = 2
- set1.drawHighlightCircleEnabled = true
- set1.setDrawHighlightIndicators(false)
-
- let set2 = RadarChartDataSet(entries: entries2, label: "This Week")
- set2.setColor(UIColor(red: 121/255, green: 162/255, blue: 175/255, alpha: 1))
- set2.fillColor = UIColor(red: 121/255, green: 162/255, blue: 175/255, alpha: 1)
- set2.drawFilledEnabled = true
- set2.fillAlpha = 0.7
- set2.lineWidth = 2
- set2.drawHighlightCircleEnabled = true
- set2.setDrawHighlightIndicators(false)
-
- let data: RadarChartData = [set1, set2]
- data.setValueFont(.systemFont(ofSize: 8, weight: .light))
- data.setDrawValues(false)
- data.setValueTextColor(.white)
-
- chartView.data = data
- }
-
- override func optionTapped(_ option: Option) {
- guard let data = chartView.data else { return }
- switch option {
- case .toggleXLabels:
- chartView.xAxis.drawLabelsEnabled = !chartView.xAxis.drawLabelsEnabled
- chartView.data?.notifyDataChanged()
- chartView.notifyDataSetChanged()
- chartView.setNeedsDisplay()
-
- case .toggleYLabels:
- chartView.yAxis.drawLabelsEnabled = !chartView.yAxis.drawLabelsEnabled
- chartView.setNeedsDisplay()
- case .toggleRotate:
- chartView.rotationEnabled = !chartView.rotationEnabled
-
- case .toggleFilled:
- for case let set as RadarChartDataSet in data {
- set.drawFilledEnabled = !set.drawFilledEnabled
- }
-
- chartView.setNeedsDisplay()
-
- case .toggleHighlightCircle:
- for case let set as RadarChartDataSet in data {
- set.drawHighlightCircleEnabled = !set.drawHighlightCircleEnabled
- }
- chartView.setNeedsDisplay()
- case .animateX:
- chartView.animate(xAxisDuration: 1.4)
-
- case .animateY:
- chartView.animate(yAxisDuration: 1.4)
-
- case .animateXY:
- chartView.animate(xAxisDuration: 1.4, yAxisDuration: 1.4)
-
- case .spin:
- chartView.spin(duration: 2, fromAngle: chartView.rotationAngle, toAngle: chartView.rotationAngle + 360, easingOption: .easeInCubic)
-
- default:
- super.handleOption(option, forChartView: chartView)
- }
- }
- }
- extension RadarChartViewController: AxisValueFormatter {
- func stringForValue(_ value: Double, axis: AxisBase?) -> String {
- return activities[Int(value) % activities.count]
- }
- }
|