123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- //
- // CubicLineChartViewController.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
- private class CubicLineSampleFillFormatter: IFillFormatter {
- func getFillLinePosition(dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat {
- return -10
- }
- }
- class CubicLineChartViewController: DemoBaseViewController {
- @IBOutlet var chartView: LineChartView!
- @IBOutlet var sliderX: UISlider!
- @IBOutlet var sliderY: UISlider!
- @IBOutlet var sliderTextX: UITextField!
- @IBOutlet var sliderTextY: UITextField!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- self.title = "Cubic Line Chart"
-
- self.options = [.toggleValues,
- .toggleFilled,
- .toggleCircles,
- .toggleCubic,
- .toggleHorizontalCubic,
- .toggleStepped,
- .toggleHighlight,
- .animateX,
- .animateY,
- .animateXY,
- .saveToGallery,
- .togglePinchZoom,
- .toggleAutoScaleMinMax,
- .toggleData]
- chartView.delegate = self
- chartView.setViewPortOffsets(left: 0, top: 20, right: 0, bottom: 0)
- chartView.backgroundColor = UIColor(red: 104/255, green: 241/255, blue: 175/255, alpha: 1)
-
- chartView.dragEnabled = true
- chartView.setScaleEnabled(true)
- chartView.pinchZoomEnabled = false
- chartView.maxHighlightDistance = 300
-
- chartView.xAxis.enabled = false
-
- let yAxis = chartView.leftAxis
- yAxis.labelFont = UIFont(name: "HelveticaNeue-Light", size:12)!
- yAxis.setLabelCount(6, force: false)
- yAxis.labelTextColor = .white
- yAxis.labelPosition = .insideChart
- yAxis.axisLineColor = .white
-
- chartView.rightAxis.enabled = false
- chartView.legend.enabled = false
-
- sliderX.value = 45
- sliderY.value = 100
- self.slidersValueChanged(nil)
-
- chartView.animate(xAxisDuration: 2, yAxisDuration: 2)
- }
- override func updateChartData() {
- if self.shouldHideData {
- chartView.data = nil
- return
- }
-
- self.setDataCount(Int(sliderX.value + 1), range: UInt32(sliderY.value))
- }
-
- func setDataCount(_ count: Int, range: UInt32) {
- let yVals1 = (0..<count).map { (i) -> ChartDataEntry in
- let mult = range + 1
- let val = Double(arc4random_uniform(mult) + 20)
- return ChartDataEntry(x: Double(i), y: val)
- }
- let set1 = LineChartDataSet(entries: yVals1, label: "DataSet 1")
- set1.mode = .cubicBezier
- set1.drawCirclesEnabled = false
- set1.lineWidth = 1.8
- set1.circleRadius = 4
- set1.setCircleColor(.white)
- set1.highlightColor = UIColor(red: 244/255, green: 117/255, blue: 117/255, alpha: 1)
- set1.fillColor = .white
- set1.fillAlpha = 1
- set1.drawHorizontalHighlightIndicatorEnabled = false
- set1.fillFormatter = CubicLineSampleFillFormatter()
-
- let data = LineChartData(dataSet: set1)
- data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 9)!)
- data.setDrawValues(false)
-
- 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)
- }
- }
-
- // MARK: - Actions
- @IBAction func slidersValueChanged(_ sender: Any?) {
- sliderTextX.text = "\(Int(sliderX.value))"
- sliderTextY.text = "\(Int(sliderY.value))"
-
- self.updateChartData()
- }
- }
|