Skip to content

Instantly share code, notes, and snippets.

@Shahn-Auronas
Created January 27, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shahn-Auronas/aebb2008b398f59b48c42519ca562cc3 to your computer and use it in GitHub Desktop.
Save Shahn-Auronas/aebb2008b398f59b48c42519ca562cc3 to your computer and use it in GitHub Desktop.
A Table View Controller that uses charts and graphs in dynamically expandable cells
//
// StatsTableViewController.swift
//
// Created by Shahn Auronas on 12/16/16.
// Copyright © 2016 Shahn Auronas. All rights reserved.
//
import Foundation
import UIKit
import ASPCircleChart
import SwiftCharts
class StatsTableViewController: UITableViewController {
@IBOutlet weak var circleChart: CircleChart!
@IBOutlet weak var milesTotalCell: MilesTotalCell!
@IBOutlet weak var milesPastWeekCell: MilesPastWeekCell!
@IBOutlet weak var chartView: UIView!
lazy var dataSource = VehicleDataManager()
var selectedIndex: Int = StatType.byVehicle.rawValue
//MARK: View lifecycle
override func viewDidLoad() {
super.viewDidLoad()
configureTableView()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configChartView(firstTime: true)
circleChart.dataSource = dataSource
milesTotalCell.configureVehicleLabelView(vehicles: dataSource.vehicles)
}
//MARK: Helpers
func configChartView(firstTime: Bool) {
for view in chartView.subviews {
view.removeFromSuperview()
}
if selectedIndex == StatType.byDriver.rawValue || firstTime {
let chart = standartBarChart()
milesPastWeekCell.configureMilesViews(chart: chart, vehicles: dataSource.vehicles)
chartView.addSubview(chart.view)
} else {
let chart = multiBarChart()
chartView.addSubview(chart.view)
}
}
private func configureTableView() {
tableView.estimatedRowHeight = Dimension.kEstimatedRowHeight
tableView.rowHeight = UITableViewAutomaticDimension
tableView.sectionFooterHeight = 0.0
}
func reloadData() {
dataSource.updateMilesPerVehicle()
tableView.reloadData()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2,
execute: {
self.circleChart.reloadData()
})
}
}
//MARK: Chart
extension StatsTableViewController {
fileprivate func standartBarChart() -> BarsChart {
let chartConfig = BarsChartConfig(
valsAxisConfig: ChartAxisConfig(from: 0, to: 40, by: 5)
)
return BarsChart(frame: CGRect(x: -50, y: 62, width: 360, height: 136),
chartConfig: chartConfig,
xTitle: "x",
yTitle: "y",
bars: [("A", Double(dataSource.vehicles[0].miles)),
("B", Double(dataSource.vehicles[1].miles)),
("C", Double(dataSource.vehicles[2].miles)),
("D", Double(dataSource.vehicles[3].miles)),
("E", Double(dataSource.vehicles[4].miles)),
("F", Double(dataSource.vehicles[5].miles)),
("G", Double(dataSource.vehicles[6].miles))],
color: UIColor.graphLightPurple(),
barWidth: 20)
}
fileprivate func multiBarChart() -> Chart {
let multiBarChart = MultiBarChart()
return multiBarChart.chart(frame: chartView.frame, horizontal: false)
}
}
//MARK: UITableViewDataSource
extension StatsTableViewController {
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.row {
case selectedIndex:
return 0
case 1:
if selectedIndex == 0 {
return max(milesTotalCell.height(),
super.tableView(tableView, heightForRowAt: indexPath))
} else {
return 0
}
default:
return super.tableView(tableView, heightForRowAt: indexPath)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment