Skip to content

Commit

Permalink
Bar Chart
Browse files Browse the repository at this point in the history
  • Loading branch information
hardyrz committed Jul 12, 2017
1 parent 0c1d84e commit 9068c21
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
46 changes: 46 additions & 0 deletions iExpense/DashboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Charts
class DashboardViewController: UIViewController {

@IBOutlet weak var pieView: PieChartView!
@IBOutlet weak var barView: BarChartView!

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -20,6 +21,7 @@ class DashboardViewController: UIViewController {

override func viewDidAppear(_ animated: Bool) {
updateChartWithData()
updateBarChartWithData()
}

func updateChartWithData() {
Expand All @@ -37,6 +39,7 @@ class DashboardViewController: UIViewController {
pieChartDataSet.colors = [UIColor.magenta, UIColor.orange, UIColor.black, UIColor.brown, UIColor.red, UIColor.blue, UIColor.green, UIColor.gray]

self.pieView.data = PieChartData(dataSet: pieChartDataSet)
self.pieView.chartDescription?.text = "All time expenses"
}

func getExpenseCountsByCategory(category : CategoryDTO) -> Int {
Expand All @@ -63,4 +66,47 @@ class DashboardViewController: UIViewController {
fatalError(error.localizedDescription)
}
}

// Bar Chart
func updateBarChartWithData() {
var dataEntries: [BarChartDataEntry] = []
let expenses = getExpensesFromDatabase()
var months = [0,0,0,
0,0,0,
0,0,0,
0,0,0]
var monthsStrings = ["Jan","Feb","Mar",
"Apr","May","Jun",
"Jul","Aug","Sep",
"Oct","Nov","Dec"]

let calendar = Calendar.current

for i in 0..<expenses.count {
let month = calendar.component(.month, from: expenses[i].date!)
months[month] += expenses[i].value
}

for j in 0..<months.count {
let dataEntry = BarChartDataEntry(x: Double(j), y: Double(months[j]))
dataEntries.append(dataEntry)
}



let chartDataSet = BarChartDataSet(values: dataEntries, label: "2017")
let chartData = BarChartData(dataSet: chartDataSet)
barView.data = chartData
barView.noDataText = "You need to provide data for the chart."
}


func getExpensesFromDatabase() -> Results<ExpenseDTO> {
do {
let realm = try Realm()
return realm.objects(ExpenseDTO.self)
} catch let error as NSError {
fatalError(error.localizedDescription)
}
}
}
1 change: 1 addition & 0 deletions iExpense/NewExpenseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class NewExpenseViewController: UIViewController, UIPickerViewDelegate, UIPicker
myExpense.desc = self.descriptionInput.text!
myExpense.category = self.selectedCategory
myExpense.account = self.selectedAccount
myExpense.date = self.datePicker.date

try! realm.write {
realm.add(myExpense)
Expand Down

0 comments on commit 9068c21

Please sign in to comment.