1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// RadarChartViewController.swift
// ChartsDemo-iOS
//
// Created by Jacob Christie on 2017-07-09.
// Copyright © 2017 jc. All rights reserved.
//
import UIKit
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(dataSets: [set1, set2])
data.setValueFont(.systemFont(ofSize: 8, weight: .light))
data.setDrawValues(false)
data.setValueTextColor(.white)
chartView.data = data
}
override func optionTapped(_ option: Option) {
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 set in chartView.data!.dataSets as! [RadarChartDataSet] {
set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()
case .toggleHighlightCircle:
for set in chartView.data!.dataSets as! [RadarChartDataSet] {
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: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return activities[Int(value) % activities.count]
}
}