-
-
Notifications
You must be signed in to change notification settings - Fork 6k
/
AnotherBarChartViewController.swift
100 lines (79 loc) · 2.95 KB
/
AnotherBarChartViewController.swift
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
//
// AnotherBarChartViewController.swift
// ChartsDemo-iOS
//
// Created by Jacob Christie on 2017-07-09.
// Copyright © 2017 jc. All rights reserved.
//
import UIKit
import Charts
class AnotherBarChartViewController: DemoBaseViewController {
@IBOutlet var chartView: BarChartView!
@IBOutlet var sliderX: UISlider!
@IBOutlet var sliderY: UISlider!
@IBOutlet var sliderTextX: UITextField!
@IBOutlet var sliderTextY: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = "Another Bar Chart"
self.options = [.toggleValues,
.toggleHighlight,
.animateX,
.animateY,
.animateXY,
.saveToGallery,
.togglePinchZoom,
.toggleData,
.toggleBarBorders]
chartView.delegate = self
chartView.chartDescription?.enabled = false
chartView.maxVisibleCount = 60
chartView.pinchZoomEnabled = false
chartView.drawBarShadowEnabled = false
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
chartView.legend.enabled = false
sliderX.value = 10
sliderY.value = 100
self.slidersValueChanged(nil)
}
override func updateChartData() {
if self.shouldHideData {
chartView.data = nil
return
}
self.setDataCount(Int(sliderX.value) + 1, range: Double(sliderY.value))
}
func setDataCount(_ count: Int, range: Double) {
let yVals = (0..<count).map { (i) -> BarChartDataEntry in
let mult = range + 1
let val = Double(arc4random_uniform(UInt32(mult))) + mult/3
return BarChartDataEntry(x: Double(i), y: val)
}
var set1: BarChartDataSet! = nil
if let set = chartView.data?.dataSets.first as? BarChartDataSet {
set1 = set
set1?.values = yVals
chartView.data?.notifyDataChanged()
chartView.notifyDataSetChanged()
} else {
set1 = BarChartDataSet(values: yVals, label: "Data Set")
set1.colors = ChartColorTemplates.vordiplom()
set1.drawValuesEnabled = false
let data = BarChartData(dataSet: set1)
chartView.data = data
chartView.fitBars = true
}
chartView.setNeedsDisplay()
}
override func optionTapped(_ option: Option) {
super.handleOption(option, forChartView: chartView)
}
// MARK: - Actions
@IBAction func slidersValueChanged(_ sender: Any?) {
sliderTextX.text = "\(Int(sliderX.value))"
sliderTextY.text = "\(Int(sliderY.value))"
self.updateChartData()
}
}