-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarView.swift
231 lines (177 loc) · 8.19 KB
/
BarView.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// BarView.swift
// PennyPlus
//
// Created by Balagurubaran Kalingarayan on 1/29/18.
// Copyright © 2018 Balagurubaran Kalingarayan. All rights reserved.
//
import Foundation
import UIKit
import Charts
class BarView {
var revenue = [BarChartDataEntry]()
var earnings = [BarChartDataEntry]()
func loadBarCharRevenue_earning(barChart:BarChartView,revenue_earnigData:[Financial]){
if(revenue_earnigData.count <= 0){
let chartData = BarChartData(dataSets: [])
barChart.data = chartData
return
}
var minValue:Double = 0.0
var maxValue:Double = 0.0
let sales = revenue_earnigData
let minYearSale = revenue_earnigData.max(by: { (a, b) -> Bool in
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = appDateFormat //Your New Date format as per requirement change it own
guard let aYear = Int((dateFormatter.date(from: a.year)?.getYear())!) else {
return false
}
guard let bYear = Int((dateFormatter.date(from: b.year)?.getYear())!) else {
return false
}
return aYear > bYear
})
let maxYearSale = revenue_earnigData.max(by: { (a, b) -> Bool in
guard let aYear = Int(getDate(date: a.year).getYear()) else {
return false
}
guard let bYear = Int(getDate(date: b.year).getYear()) else {
return false
}
return aYear < bYear
})
let minYear:Int = Int(getDate(date: minYearSale!.year).getYear())!
let maxYear:Int = Int(getDate(date: maxYearSale!.year).getYear())!
let revenue = sales.map { (sale) -> BarChartDataEntry in
let year:Double = Double(getDate(date: sale.year).getYear())!
return BarChartDataEntry(x:year, y: Double(sale.revenue!)!)
}
let earnings = sales.map { (sale) -> BarChartDataEntry in
let year:Double = Double(getDate(date: sale.year).getYear())!
return BarChartDataEntry(x:year, y: Double(sale.earnings!)!)
}
let revenueBarColor = revenue.map { (entry) -> NSUIColor in
return entry.y >= 0 ? UIColor.stockEmc2Green() : UIColor.stockEmc2Red()
}
let earningsBarColor = earnings.map { (entry) -> NSUIColor in
return entry.y >= 0 ? UIColor.stockEmc2TealBlue() : UIColor.stockEmc2Red()
}
let orderEarnings = sales.sorted { (a, b) -> Bool in
return (a.earnings! > b.earnings!)
}
let orderRevenue = sales.sorted { (a, b) -> Bool in
return (a.revenue! > b.revenue!)
}
if let value = orderRevenue[0].revenue {
maxValue = Double(value)!
if let value = orderEarnings[0].earnings {
minValue = Double(value)!
}
}
if(orderEarnings.count > 1){
if let value = orderEarnings[orderEarnings.count - 1].earnings {
minValue = Double(value)!
}
}
if (minValue > 0){
minValue = 0;
}
//maxValue = orderSales
//minValue = revenueMin?.revenue < earningMin ? revenueMin : earningMin
barCharSettings(barChart:barChart, maxValue: maxValue + maxValue/2, minValue: minValue * 3 ,minYear:minYear,maxYear:maxYear)
let chartDataSetRevenue = BarChartDataSet(values: revenue, label: "Revenue")
chartDataSetRevenue.colors = revenueBarColor
let chartDataSetEarning = BarChartDataSet(values: earnings, label: "Earning")
chartDataSetEarning.colors = earningsBarColor
// let lineDataSet = LineChartDataSet(values: earnings, label: "Earning")
// lineDataSet.colors = [.blue]
//
// let lineData = LineChartData()
// lineData.addDataSet(lineDataSet)
let chartData = BarChartData(dataSets: [chartDataSetRevenue,chartDataSetEarning])
chartData.setValueFormatter(LargeValueFormatter())
chartData.barWidth = 0.25
// // Set bar chart data to previously created data
chartData.groupBars(fromX: Double(minYear), groupSpace: 0.20, barSpace: 0.05)
barChart.data = chartData
}
private func getDate(date:String)->Date{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = appDateFormat //Your New Date format as per requirement change it own
let date = dateFormatter.date(from: date)
return date!
}
private func barCharSettings(barChart:BarChartView,maxValue:Double,minValue:Double,minYear:Int,maxYear:Int){
barChart.chartDescription?.text = ""
barChart.xAxis.labelPosition = .bottom
barChart.xAxis.axisMinimum = Double(minYear)
barChart.xAxis.axisMaximum = Double(maxYear)
barChart.xAxis.granularity = 1.0
barChart.fitBars = true
barChart.leftAxis.axisMinimum = minValue
barChart.leftAxis.axisMaximum = maxValue
barChart.leftAxis.valueFormatter = LargeValueFormatter()
barChart.scaleYEnabled = false
barChart.scaleXEnabled = false
barChart.pinchZoomEnabled = false
barChart.doubleTapToZoomEnabled = false
barChart.drawBarShadowEnabled = false
barChart.chartDescription?.enabled = false
barChart.highlighter = nil
barChart.rightAxis.enabled = false
barChart.xAxis.drawGridLinesEnabled = false
barChart.animate(yAxisDuration: 1.5)
barChart.layer.cornerRadius = 10.0;
// let l = barChart.legend
// l.horizontalAlignment = .right
// l.verticalAlignment = .top
// l.orientation = .vertical
// l.drawInside = true
// l.font = .systemFont(ofSize: 8, weight: .light)
// l.yOffset = 10
// l.xOffset = 10
// l.yEntrySpace = 0
}
func loadVolumeToBarCharView(chartView:BarChartView,volume:[TradeVolme]){
if(volume.count <= 0){
return
}
let lastValue = volume.last
var index = 0
let revenue = volume.map { (tradeV) -> BarChartDataEntry in
//let Timestamp = price.timeStamp
index = index + 1
return BarChartDataEntry(x:Double(index), y: Double(tradeV.Volume!))
}
let chartDataSetRevenue = BarChartDataSet(values: revenue, label: "Day's trade volume")
chartDataSetRevenue.colors = [UIColor.stockEmc2Green()]
let data = BarChartData(dataSet: chartDataSetRevenue)
//data.barWidth = 0.25
chartView.data = data
chartView.fitBars = true
chartView.chartDescription?.text = ""
chartView.xAxis.labelPosition = .bottom
//chartView.xAxis.axisMaximum = Double((lastValue?.Index)!)
//chartView.xAxis.axisMinimum = Double((volume.first?.Index)!)
let XlabelText:[String] = volume.compactMap { (each) -> String in
return each.xValue ?? ""
}
chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: XlabelText)
chartView.xAxis.granularity = 1.0
chartView.fitBars = true
//chartView.leftAxis.axisMinimum = minValue
//chartView.leftAxis.axisMaximum = maxValue
chartView.scaleYEnabled = false
chartView.scaleXEnabled = false
chartView.pinchZoomEnabled = false
chartView.doubleTapToZoomEnabled = false
chartView.drawBarShadowEnabled = false
chartView.chartDescription?.enabled = false
chartView.highlighter = nil
chartView.rightAxis.enabled = false
chartView.xAxis.drawGridLinesEnabled = false
chartView.animate(yAxisDuration: 1.5)
chartView.layer.cornerRadius = 10.0;
chartView.setNeedsDisplay()
}
}