Last active
August 3, 2020 11:12
-
-
Save ceduliocezar/2bb6f82eec34fb959f41db8fe79d61f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// ChartLibTest | |
// | |
// Created by Cedulio Cezar on 03/11/16. | |
// Copyright © 2016 Cedulio Cezar. All rights reserved. | |
// | |
import UIKit | |
import Charts | |
class ViewController: UIViewController { | |
@IBOutlet weak var chart: HorizontalBarChartView! | |
var months: [String]! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
chart.drawBarShadowEnabled = false | |
chart.drawValueAboveBarEnabled = true | |
chart.maxVisibleCount = 60 | |
let xAxis = chart.xAxis | |
xAxis.labelPosition = .bottom | |
xAxis.drawAxisLineEnabled = true | |
xAxis.drawGridLinesEnabled = false | |
xAxis.granularity = 10.0 | |
let leftAxis = chart.leftAxis; | |
leftAxis.drawAxisLineEnabled = true; | |
leftAxis.drawGridLinesEnabled = true; | |
leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES | |
let rightAxis = chart.rightAxis | |
rightAxis.enabled = true; | |
rightAxis.drawAxisLineEnabled = true; | |
rightAxis.drawGridLinesEnabled = false; | |
rightAxis.axisMinimum = 0.0; // this replaces startAtZero = YES | |
let l = chart.legend | |
l.enabled = false | |
chart.fitBars = true; | |
setDataCount(count: 12, range: 50) | |
} | |
func setDataCount(count: Int, range: Double){ | |
let barWidth = 9.0 | |
let spaceForBar = 10.0; | |
var yVals = [BarChartDataEntry]() | |
for i in 0..<count{ | |
let mult = (range + 1) | |
let val = (Double)(arc4random_uniform(UInt32(mult))) | |
yVals.append(BarChartDataEntry(x: Double(i) * spaceForBar, y: val)) | |
} | |
var set1 : BarChartDataSet! | |
if let count = chart.data?.dataSetCount, count > 0{ | |
set1 = chart.data?.dataSets[0] as! BarChartDataSet | |
set1.values = yVals | |
chart.data?.notifyDataChanged() | |
chart.notifyDataSetChanged() | |
}else{ | |
set1 = BarChartDataSet(values: yVals, label: "DataSet") | |
var dataSets = [BarChartDataSet]() | |
dataSets.append(set1) | |
let data = BarChartData(dataSets: dataSets) | |
data.barWidth = barWidth; | |
chart.data = data | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment