123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- //
- // LineChartTimeViewController.swift
- // ChartsDemo-iOS
- //
- // Created by Jacob Christie on 2017-07-09.
- // Copyright © 2017 jc. All rights reserved.
- //
- #if canImport(UIKit)
- import UIKit
- #endif
- import Charts
- class LineChartTimeViewController: DemoBaseViewController {
- @IBOutlet var chartView: LineChartView!
- @IBOutlet var sliderX: UISlider!
- @IBOutlet var sliderTextX: UITextField!
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- // Do any additional setup after loading the view.
- self.title = "Time Line Chart"
- self.options = [.toggleValues,
- .toggleFilled,
- .toggleCircles,
- .toggleCubic,
- .toggleHorizontalCubic,
- .toggleStepped,
- .toggleHighlight,
- .animateX,
- .animateY,
- .animateXY,
- .saveToGallery,
- .togglePinchZoom,
- .toggleAutoScaleMinMax,
- .toggleData]
-
- chartView.delegate = self
-
- chartView.chartDescription?.enabled = false
-
- chartView.dragEnabled = true
- chartView.setScaleEnabled(true)
- chartView.pinchZoomEnabled = false
- chartView.highlightPerDragEnabled = true
-
- chartView.backgroundColor = .white
-
- chartView.legend.enabled = false
-
- let xAxis = chartView.xAxis
- xAxis.labelPosition = .topInside
- xAxis.labelFont = .systemFont(ofSize: 10, weight: .light)
- xAxis.labelTextColor = UIColor(red: 255/255, green: 192/255, blue: 56/255, alpha: 1)
- xAxis.drawAxisLineEnabled = false
- xAxis.drawGridLinesEnabled = true
- xAxis.centerAxisLabelsEnabled = true
- xAxis.granularity = 3600
- xAxis.valueFormatter = DateValueFormatter()
-
- let leftAxis = chartView.leftAxis
- leftAxis.labelPosition = .insideChart
- leftAxis.labelFont = .systemFont(ofSize: 12, weight: .light)
- leftAxis.drawGridLinesEnabled = true
- leftAxis.granularityEnabled = true
- leftAxis.axisMinimum = 0
- leftAxis.axisMaximum = 170
- leftAxis.yOffset = -9
- leftAxis.labelTextColor = UIColor(red: 255/255, green: 192/255, blue: 56/255, alpha: 1)
-
- chartView.rightAxis.enabled = false
-
- chartView.legend.form = .line
-
- sliderX.value = 100
- slidersValueChanged(nil)
-
- chartView.animate(xAxisDuration: 2.5)
- }
-
- override func updateChartData() {
- if self.shouldHideData {
- chartView.data = nil
- return
- }
-
- self.setDataCount(Int(sliderX.value), range: 30)
- }
-
- func setDataCount(_ count: Int, range: UInt32) {
- let now = Date().timeIntervalSince1970
- let hourSeconds: TimeInterval = 3600
-
- let from = now - (Double(count) / 2) * hourSeconds
- let to = now + (Double(count) / 2) * hourSeconds
-
- let values = stride(from: from, to: to, by: hourSeconds).map { (x) -> ChartDataEntry in
- let y = arc4random_uniform(range) + 50
- return ChartDataEntry(x: x, y: Double(y))
- }
-
- let set1 = LineChartDataSet(entries: values, label: "DataSet 1")
- set1.axisDependency = .left
- set1.setColor(UIColor(red: 51/255, green: 181/255, blue: 229/255, alpha: 1))
- set1.lineWidth = 1.5
- set1.drawCirclesEnabled = false
- set1.drawValuesEnabled = false
- set1.fillAlpha = 0.26
- set1.fillColor = UIColor(red: 51/255, green: 181/255, blue: 229/255, alpha: 1)
- set1.highlightColor = UIColor(red: 244/255, green: 117/255, blue: 117/255, alpha: 1)
- set1.drawCircleHoleEnabled = false
-
- let data = LineChartData(dataSet: set1)
- data.setValueTextColor(.white)
- data.setValueFont(.systemFont(ofSize: 9, weight: .light))
-
- chartView.data = data
- }
-
- override func optionTapped(_ option: Option) {
- switch option {
- case .toggleFilled:
- for set in chartView.data!.dataSets as! [LineChartDataSet] {
- set.drawFilledEnabled = !set.drawFilledEnabled
- }
- chartView.setNeedsDisplay()
-
- case .toggleCircles:
- for set in chartView.data!.dataSets as! [LineChartDataSet] {
- set.drawCirclesEnabled = !set.drawCirclesEnabled
- }
- chartView.setNeedsDisplay()
-
- case .toggleCubic:
- for set in chartView.data!.dataSets as! [LineChartDataSet] {
- set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier
- }
- chartView.setNeedsDisplay()
-
- case .toggleStepped:
- for set in chartView.data!.dataSets as! [LineChartDataSet] {
- set.mode = (set.mode == .stepped) ? .linear : .stepped
- }
- chartView.setNeedsDisplay()
-
- case .toggleHorizontalCubic:
- for set in chartView.data!.dataSets as! [LineChartDataSet] {
- set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier
- }
- chartView.setNeedsDisplay()
-
- default:
- super.handleOption(option, forChartView: chartView)
- }
- }
-
- @IBAction func slidersValueChanged(_ sender: Any?) {
- sliderTextX.text = "\(Int(sliderX.value))"
- self.updateChartData()
- }
- }
|