-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarChartViewExtention.swift
More file actions
55 lines (42 loc) · 1.79 KB
/
BarChartViewExtention.swift
File metadata and controls
55 lines (42 loc) · 1.79 KB
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
//
// BarChartViewExtention.swift
//
// Extension for simplify using of BarChartView class from Charts https://github.com/danielgindi/Charts
// This extention just add new method setBarChartData(:[String]:[Double]:String) wich set string values for xAxis and numeric values for yAxis
//
// Created by Alexander Smetannikov on 31/03/2017.
// Copyright © 2017 AlexSmetannikov. All rights reserved.
//
import Foundation
import Charts
extension BarChartView {
private class BarChartFormatter: NSObject, IAxisValueFormatter {
var labels: [String] = []
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return labels[Int(value)]
}
init(labels: [String]) {
super.init()
self.labels = labels
}
}
/// Set string values for xAxis and numeric values for yAxis.
/// Number of values calculated by yValues.Count
/// - parameter xValues: String values for xAxis
/// - parameter yValues: Numeric values (Double) for yAxix
/// - parameter label: Just label for data
func setBarChartData(xValues: [String], yValues: [Double], label: String) {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<yValues.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: label)
let chartData = BarChartData(dataSet: chartDataSet)
let chartFormatter = BarChartFormatter(labels: xValues)
let xAxis = XAxis()
xAxis.valueFormatter = chartFormatter
self.xAxis.valueFormatter = xAxis.valueFormatter
self.data = chartData
}
}