coca pod Chart not appearing (Swift4) - ios

My chart is not displaying any bars using this bar graph. I have successfully imported the charts cocoa pod. There are currently no run time errors. The only thing that is being displayed in the graph is the description label.
import UIKit
import Charts
class ViewController: UIViewController {
#IBOutlet var lineChartVIew: BarChartView!
var days: [String]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
days = ["Monday","Tuesday","life"]
let task = [1.0,4.0,3.0]
setChart(dataPoints: days, values: task)
}
func setChart(dataPoints : [String], values : [Double]){
lineChartVIew.noDataText = "Nothining to display"
var dataEntries : [BarChartDataEntry] = []
var counter = 0.0
for i in 0..<dataPoints.count {
counter += 1
let dataEntery = BarChartDataEntry(x: values[i], y: counter)
dataEntries.append(dataEntery)
}
let ChartDataSet = BarChartDataSet(values: dataEntries, label: "Time")
let chartData = BarChartData()
lineChartVIew.data = chartData
ChartDataSet.colors = ChartColorTemplates.colorful()
lineChartVIew.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
}}

Try this one it's is Working (Swift 4 Code).
import UIKit
import Charts
class RootViewController: UIViewController {
#IBOutlet weak var lineChartView: BarChartView!
var days: [String]!
override func viewDidLoad() {
super.viewDidLoad()
days = ["Monday","Tuesday","Wednesday","Thursday"]
let task = [1.0,4.0,3.0,5.0]
setChart(dataPoints: days, values: task)
}
func setChart(dataPoints : [String], values : [Double]){
lineChartView.noDataText = "Nothining to display"
var dataEntries : [BarChartDataEntry] = []
var counter = 0.0
for i in 0..<dataPoints.count {
counter += 1
let dataEntery = BarChartDataEntry(x: counter, y:values[i], data: days as AnyObject)
dataEntries.append(dataEntery)
}
let ChartDataSet = BarChartDataSet(values: dataEntries, label: "Time")
let chartData = BarChartData()
chartData.addDataSet(ChartDataSet)
lineChartView.data = chartData
ChartDataSet.colors = ChartColorTemplates.joyful()
lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
}
}
Output is :
Modify Graph Hide and Show Axis and Labels
lineChartView.leftAxis.drawLabelsEnabled = false // Hide Left Axis Label
lineChartView.rightAxis.drawLabelsEnabled = false // Hide Right Axis Label
lineChartView.xAxis.drawLabelsEnabled = false // Hide Top Axis Label
lineChartView.leftAxis.enabled = false // Hide Left Axis Lines
lineChartView.rightAxis.enabled = false // Hide Right Axis Lines
lineChartView.xAxis.enabled = false // Hide Right Axis Lines
lineChartView.legend.enabled = false //Hide Legend of Chart
lineChartView.chartDescription?.text = "" // Hide or Change Chart Description text

Related

iOS Charts(Daniel Gindi) - Two LineChartDataSet with different color failed

I am trying to build a graph with 2 LineChartDataSet. At the first time, I build with one and then on every selected value, I want to do some different color to the right data set yet it seems like the last set sort of run over the settings and do it the opposite:
class GraphTableViewCell: UITableViewCell {
#IBOutlet weak var yieldLabel: UILabel!
#IBOutlet weak var yieldPercentLabel: UILabel!
#IBOutlet weak var lineChart: LineChartView!
#IBOutlet weak var graphButtonView: AssetGraphButtonView!
#IBOutlet weak var endDateLabel: UILabel!
#IBOutlet weak var startDateLabel: UILabel!
var selectionView: AssetGraphSelectionView!
var viewModel: GraphViewModelType!
var set: LineChartDataSet!
var set1: LineChartDataSet!
var marker = BalloonMarker(color: .red,
font: UIFont.systemFont(ofSize: 15),
textColor: .white,
insets: UIEdgeInsets(top: 5, left: 5, bottom: 10, right: 3))
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override func layoutSubviews() {
super.layoutSubviews()
}
func config(with viewModel: GraphViewModelType) {
self.viewModel = viewModel
yieldLabel.attributedText = viewModel.titleAttributeText
yieldPercentLabel.attributedText = viewModel.yielAttributeText
startDateLabel.attributedText = viewModel.startDateAttributeText
endDateLabel.attributedText = viewModel.endDateAttributeText
graphButtonView.confgiureCell(with: self.viewModel.btnData)
setUpChart()
}
func setUpChart() {
lineChart.delegate = self
lineChart.noDataText = "No Data Available"
lineChart.rightAxis.enabled = false
lineChart.leftAxis.enabled = false
lineChart.xAxis.enabled = false
lineChart.legend.enabled = false
lineChart.xAxis.drawGridLinesEnabled = false
lineChart.drawMarkers = true
lineChart.doubleTapToZoomEnabled = false
lineChart.pinchZoomEnabled = false
lineChart.scaleXEnabled = false
lineChart.scaleYEnabled = false
marker.chartView = lineChart
marker.minimumSize = CGSize(width: 28, height: 20)
lineChart.marker = marker
let dataSets = viewModel.getLineChartDataSet()
let data = LineChartData(dataSets: dataSets)
data.setValueFont(.systemFont(ofSize: 7, weight: .light))
lineChart.data = data
}
extension GraphTableViewCell: ChartViewDelegate {
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
let transform = lineChart.getTransformer(forAxis: .left)
let point = transform.pixelForValues(x: highlight.x, y: highlight.y)
print("point : x = \(point.x) y = \(point.y)")
let color = self.lineChart.colorOfPoint(point: point)
marker.color = color
let dataSets = viewModel.chartValueSelected(entry: entry)
lineChart.data = dataSets
}
}
This is the viewModel:
final class GraphTableViewCellViewModel: GraphViewModelType {
var startDateAttributeText = NSMutableAttributedString()
var endDateAttributeText = NSMutableAttributedString()
var titleAttributeText = NSMutableAttributedString(string: "", attributes: [NSAttributedString.Key.font : UIFont(name: "Orion-Bold", size: 14)!, NSAttributedString.Key.foregroundColor : UIColor.black])
var yielAttributeText = NSMutableAttributedString(string: "\(String(format: "%.2f%%", abs(14)))", attributes: [NSAttributedString.Key.font : UIFont(name: "Orion-Bold", size: 14)!, NSAttributedString.Key.foregroundColor : UIColor.green])
let disposeBag = DisposeBag()
var btnData: [AssetGraphButtonViewViewModel]
var data: TwrGraph
var dataSet = [LineChartDataSet]()
var set: LineChartDataSet!
var set1: LineChartDataSet!
init(with data: TwrGraph) {
btnData = AssetGraphViewModel(security: nil).assetGraphData
self.data = data
startDateAttributeText = NSMutableAttributedString(string: data.startDate, attributes: [NSAttributedString.Key.font : UIFont(name: "Orion-Regular", size: 12)!, NSAttributedString.Key.foregroundColor : ColorName.warmGreyTwo])
endDateAttributeText = NSMutableAttributedString(string: data.endDate, attributes: [NSAttributedString.Key.font : UIFont(name: "Orion-Regular", size: 12)!, NSAttributedString.Key.foregroundColor : ColorName.warmGreyTwo])
}
func getChartDataPoints(dataPoints: [String], values: [Double]) -> [ChartDataEntry] {
var dataEntries: [ChartDataEntry] = []
for count in (0..<dataPoints.count) {
dataEntries.append(ChartDataEntry.init(x: Double(count), y: values[count]))
}
return dataEntries
}
func getLineChartDataSet() -> [LineChartDataSet] {
let dataWeeklyDate = self.data.weeklyGraph.map { $0.date }
let dataWeeklyYield = self.data.weeklyGraph.map { $0.yield }
let dataPoints = getChartDataPoints(dataPoints: dataWeeklyDate, values: dataWeeklyYield)
set = LineChartDataSet(entries: dataPoints, label:"")
setup(set)
return [set]
}
func setup(_ dataSet: LineChartDataSet) {
dataSet.drawHorizontalHighlightIndicatorEnabled = false
dataSet.drawVerticalHighlightIndicatorEnabled = true
dataSet.isDrawLineWithGradientEnabled = true
dataSet.fillAlpha = 0.15
dataSet.lineWidth = 2
dataSet.circleRadius = 0
dataSet.drawCircleHoleEnabled = false
dataSet.drawCirclesEnabled = false
dataSet.drawValuesEnabled = false
dataSet.highlightColor = .blue
let rightColor = [ChartColorTemplates.colorFromString("#FA3A7A"), ChartColorTemplates.colorFromString("#C257B1"),
ChartColorTemplates.colorFromString("#8B73E8")]
dataSet.colors = rightColor
dataSet.gradientPositions = [0, 40, 100]
let gradientColors = [ChartColorTemplates.colorFromString("#FC4684").cgColor,
ChartColorTemplates.colorFromString("#D8D8D8").cgColor]
let colorLocations:[CGFloat] = [1.0, 0.0]
if let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: colorLocations) {
dataSet.fill = LinearGradientFill(gradient: gradient, angle: 90.0)
}
dataSet.drawFilledEnabled = true
}
func updateSetAfterTouch(_ dataSet: LineChartDataSet) {
dataSet.drawHorizontalHighlightIndicatorEnabled = false
dataSet.drawVerticalHighlightIndicatorEnabled = true
// dataSet.isDrawLineWithGradientEnabled = true
dataSet.fillAlpha = 0.15
dataSet.lineWidth = 2
dataSet.circleRadius = 0
dataSet.drawCircleHoleEnabled = false
dataSet.drawCirclesEnabled = false
dataSet.drawValuesEnabled = false
// dataSet.highlightColor = .blue
dataSet.colors = [.red]
}
func chartValueSelected(entry: ChartDataEntry) -> LineChartData {
return updateSet(with: entry)
}
func updateSet(with entry: ChartDataEntry) -> LineChartData {
var dataEntries: [ChartDataEntry] = []
var dataEntries1: [ChartDataEntry] = []
for count in (0..<self.data.weeklyGraph.count) {
if count < self.data.weeklyGraph.count && count < Int(entry.x) {
dataEntries.append(ChartDataEntry.init(x: Double(count), y: self.data.weeklyGraph[count].yield))
} else {
dataEntries1.append(ChartDataEntry.init(x: Double(count), y: self.data.weeklyGraph[count].yield))
}
}
set = LineChartDataSet(entries: dataEntries, label:"")
set1 = LineChartDataSet(entries: dataEntries1, label:"")
setup(set)
updateSetAfterTouch(set1)
let data = LineChartData(dataSets: [set1, set])
return data
}
}
As you can see when value is selected and I am using it to create two ChartDataEntry and the two LineChartDataSet that one continue the other (in the x axis ). This is the image when we first entering(looks fine):
This is the image when selecting:
After creating two LineChartDataSet add a different colour for each set.
func updateGraph(){
//create 2 ChartDataEntry arrays for both sets
var dataEntries : [ChartDataEntry] = []
var dataEntries1 : [ChartDataEntry] = []
//use ur condition here to devide the data in to two groups
for i in 0..<numbers.count {
if i < 3{
let value = ChartDataEntry(x: Double(i), y: numbers[i]) // here we set the X and Y status in a data chart entry
dataEntries.append(value)
}else{
let value = ChartDataEntry(x: Double(i), y: numbers[i]) // here we set the X and Y status in a data chart entry
dataEntries1.append(value)
}
}
//I add the last element of the first array to the begining of the first array to stop discontinue
dataEntries1.insert(dataEntries.last!, at: 0)
//create data set 1
let set1 = LineChartDataSet(entries: dataEntries, label: "Number")
set1.colors = [UIColor.blue]
//create data set 2
let set2 = LineChartDataSet(entries: dataEntries1, label: "Number")
set2.colors = [UIColor.red]
//This is the object that will be added to the chart
let data = LineChartData(dataSets: [set1, set2])
chtChart.data = data //finally - it adds the chart data to the chart and causes an update
}
My Output looks like follows.
Full ViewController Code
class ViewController: UIViewController {
#IBOutlet weak var txtTextBox: UITextField!
#IBOutlet weak var chtChart: LineChartView!
var numbers : [Double] = [] //This is where we are going to store all the numbers. This can be a set of numbers that come from a Realm database, Core data, External API's or where ever else
override func viewDidLoad() {
super.viewDidLoad()
var dataEntries : [ChartDataEntry] = []
numbers = [2,4,7,3,4,5,8,9,1,2,9]
for i in 0..<numbers.count{
let value = ChartDataEntry(x: Double(i), y: numbers[i]) // here we set the X and Y status in a data chart entry
dataEntries.append(value)
}
let dataSet = LineChartDataSet(entries: dataEntries, label: "Number")
dataSet.drawHorizontalHighlightIndicatorEnabled = false
dataSet.drawVerticalHighlightIndicatorEnabled = true
dataSet.drawHorizontalHighlightIndicatorEnabled = true
dataSet.fillAlpha = 0.15
dataSet.lineWidth = 2
dataSet.circleRadius = 0
dataSet.drawCircleHoleEnabled = false
dataSet.drawCirclesEnabled = false
dataSet.drawValuesEnabled = false
dataSet.highlightColor = .blue
dataSet.colors = [UIColor.green]
let data = LineChartData(dataSets: [dataSet])
chtChart.data = data
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func btnbutton(_ sender: Any) {
// let input = Double(txtTextBox.text!) //gets input from the textbox - expects input as double/int
// numbers.append(input!) //here we add the data to the array.
updateGraph()
txtTextBox.text = ""
}
func updateGraph(){
//create 2 ChartDataEntry arrays for both sets
var dataEntries : [ChartDataEntry] = []
var dataEntries1 : [ChartDataEntry] = []
//use ur condition here to devide the data in to two groups
for i in 0..<numbers.count {
if i < 3{
let value = ChartDataEntry(x: Double(i), y: numbers[i]) // here we set the X and Y status in a data chart entry
dataEntries.append(value)
}else{
let value = ChartDataEntry(x: Double(i), y: numbers[i]) // here we set the X and Y status in a data chart entry
dataEntries1.append(value)
}
}
//I add the last element of the first array to the begining of the first array to stop discontinue
dataEntries1.insert(dataEntries.last!, at: 0)
//create data set 1
let set1 = LineChartDataSet(entries: dataEntries, label: "Number")
set1.colors = [UIColor.blue]
set1.drawHorizontalHighlightIndicatorEnabled = false
set1.drawVerticalHighlightIndicatorEnabled = true
set1.drawHorizontalHighlightIndicatorEnabled = true
set1.fillAlpha = 0.15
set1.lineWidth = 2
set1.circleRadius = 0
set1.drawCircleHoleEnabled = false
set1.drawCirclesEnabled = false
set1.drawValuesEnabled = false
set1.highlightColor = .blue
//create data set 2
let set2 = LineChartDataSet(entries: dataEntries1, label: "Number")
set2.colors = [UIColor.red]
set2.drawHorizontalHighlightIndicatorEnabled = false
set2.drawVerticalHighlightIndicatorEnabled = true
set2.drawHorizontalHighlightIndicatorEnabled = true
set2.fillAlpha = 0.15
set2.lineWidth = 2
set2.circleRadius = 0
set2.drawCircleHoleEnabled = false
set2.drawCirclesEnabled = false
set2.drawValuesEnabled = false
set2.highlightColor = .blue
//This is the object that will be added to the chart
let data = LineChartData(dataSets: [set1, set2])
chtChart.data = data //finally - it adds the chart data to the chart and causes an update
}
}

How to create a limit line for each bar chart in iOS swift

How to create a limit line for each bar chart in iOS swift. I'm using Chart Library
Here is my code: -
var chartView: BarChartView!
var entries: [BarChartDataEntry] = []
var data: [Double]?
func draw() {
for index in 0..<data.count {
let dataEntry = BarChartDataEntry(x: Double(index), y: data[index])
entries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: entries, label: "")
let chartData = BarChartData(dataSet: chartDataSet)
chartData.setValueFont(UIFont(name: "Roobert-SemiBold", size: 12.0))
chartData.barWidth = 0.3
chartView.data = chartData
var limitLine = ChartLimitLine()
limitLine = ChartLimitLine(limit: 15, label: "15")
limitLine.lineColor = .blue
limitLine.valueTextColor = .blue
limitLine.lineDashLengths = [3.0]
chartView.leftAxis.addLimitLine(limitLine)
var limitLine = ChartLimitLine()
limitLine = ChartLimitLine(limit: 12, label: "12")
limitLine.lineColor = .orange
limitLine.valueTextColor = .orange
limitLine.lineDashLengths = [3.0]
chartView.leftAxis.addLimitLine(limitLine)
}
Output: -
Requirement: -
please refer above images,
For example, the limit line here is starting from the highest bar to the lowest bar but I would like to show this limit line only for the average bar. Is there a way?

How do I display labels on bottom axis (xVals) correctly on iOS Chart?

I have been following very simple tutorial of iOS Charts.
The values in my chart are now showing correctly, however the label on the bottom is not showing.
override func viewDidLoad() {
results = ["Won", "Drawn", "Lost"]
let games = [totalWins, totalDraws, totalLosses]
setChart(dataPoints: results, values: games)
}
// CHART FUNCTION ************
func setChart(dataPoints: [String], values: [Double]){
barChartView.noDataText = "you need to provide some data for the chart."
var dataEntries: [BarChartDataEntry] = Array()
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played")
//let chartData = BarChartData(xVals: self.results, dataSet: dataEntries)
let chartData = BarChartData()
self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
barChartView.leftAxis.granularityEnabled = true
barChartView.rightAxis.enabled = false
barChartView.leftAxis.granularity = 1.0
chartData.addDataSet(chartDataSet)
barChartView.data = chartData
}
// END OF CHART FUNCTION ***********
As you can see it is displaying numbers, rather than "Won, Drawn, Lost"
I believe this is because I need to assign the labels in a command like this:
let chartData = BarChartData(xVals: self.results, dataSet: dataSet)
chartView.data = chartData
But I get errors and I don't know what needs to go in dataSet as I took that solution from another thread and can't seem to amend it to work.
Temp image:
You have interchanges x and y values.
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
And to get the X-Axis at the bottom, you have to add the following.
self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
Please find my working code for a sample dataset.
class BarChartViewController: UIViewController {
#IBOutlet weak var barChartView: BarChartView!
override func viewDidLoad() {
super.viewDidLoad()
let values = [11.00, 90.95, 250.00, 40.90, 60.88, 99.99, 25.00]
setChart(values: values)
}
func setChart(values: [Double]) {
var daysEntries: [BarChartDataEntry] = []
for i in 0..<values.count {
let dataEntry = BarChartDataEntry(x : Double(i), y : values[i])
daysEntries.append(dataEntry)
}
let data = BarChartData()
let ds1 = BarChartDataSet(values: daysEntries, label: "Days")
ds1.colors = [NSUIColor.red]
data.addDataSet(ds1)
self.barChartView.data = data
self.barChartView.gridBackgroundColor = NSUIColor.white
self.barChartView.chartDescription?.text = "Barchart Days and Gross"
self.barChartView.rightAxis.enabled = true
self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
self.barChartView.xAxis.axisRange = 5.0
}
override func viewWillAppear(_ animated: Bool) {
self.barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
}
}
Thanks
Sriram
I had the same issue and I fixed it with these three lines :
self.xAxis.granularity = 1
self.xAxis.granularityEnabled = true
self.xAxis.labelCount = // number of points on X axis
// xAxis.labelFont = UIFont.systemFont(ofSize: 13.0)
// xAxis.labelTextColor = .lightGray
xAxis.labelPosition = .bottom
xAxis.setLabelCount(dataPoints.count, force: false)
if xAxis.labelPosition == .bottom {
xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
}
xAxis.granularity = 1.0
I hope this would help you with controlling the xAxis label positions.
First method
chartView.setExtraOffsets(left: 20, top: 0, right: 0, bottom: 0)
Second method
chartView.xAxis.avoidFirstLastClippingEnabled = true

How do I remove the dots in LineChartView iOS charts

I'm learning ios-charts. I was using the tutorial found here. The first picture shows the result I am getting. How do you remove the blue circle dots so that it only shows a smooth line like what is shown in the second picture?
Here is snippet of the code
import UIKit
import Charts
class ChartsViewController: UIViewController {
#IBOutlet weak var lineChartView: LineChartView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
setChart(months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Units Sold")
let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
lineChartView.data = lineChartData
}
}
Set the .setDrawCircles = NO; of your LineDataSet set to disable the drawing of circle.
It was on the wiki...
https://github.com/PhilJay/MPAndroidChart/wiki/DataSet-classes-in-detail
to remove circle in a line
lineChartDataSet.drawCirclesEnabled = false
to remove value alone in a circle
lineChartData.drawValuesEnabled = false
In addition to the answers, you can use these properties to make your line more smooth
lineChartDataSet.drawCirclesEnabled = false
lineChartDataSet.drawCubicEnabled = true
or you can use mode property as drawCubicEnabled property is deprecated
lineChartDataSet.mode = .cubicBezier
lineChartDataSet.cubicIntensity = 0.2
lineChartDataSet.circleRadius = 0

Attributes of text above specific bar with ios-charts

How can I change attributes (e.g. font size, text color, etc...) of text above a specific bar in a BarChart?
In this example, I want "-$5,000.00" in red and to increase the font size of every text above bars.
Here's some code:
#IBOutlet weak var barChartView: BarChartView!
// init barChartView --------------------------------------
barChartView.descriptionText = ""
barChartView.legend.enabled = false
// grid lines
barChartView.xAxis.drawAxisLineEnabled = false
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.leftAxis.drawAxisLineEnabled = false
barChartView.leftAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawAxisLineEnabled = false
barChartView.rightAxis.drawGridLinesEnabled = false
// X-axis line
barChartView.xAxis.drawAxisLineEnabled = true
barChartView.xAxis.axisLineColor = axisGridsAndLabelsColor
// X-axis labels
barChartView.xAxis.labelTextColor = axisGridsAndLabelsColor
barChartView.xAxis.labelPosition = .Bottom
// Y-axis labels
accountsBarChartView.leftAxis.labelTextColor = axisGridsAndLabelsColor
accountsBarChartView.rightAxis.drawLabelsEnabled = false
//---------------------------------------------------------
// bar chart's data
var dataPoints = [String]()
var values = [Double]()
var colors = [UIColor]()
// build bar chart's data...
// dataEntries and barChartDataSet
var dataEntries = [ChartDataEntry]()
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let barChartDataSet = BarChartDataSet(yVals: dataEntries, label: "")
barChartDataSet.colors = colors
// valueFormatter
let currencyNumberFormatter = NSNumberFormatter()
currencyNumberFormatter.numberStyle = .CurrencyStyle
currencyNumberFormatter.minimumFractionDigits = 2
currencyNumberFormatter.maximumFractionDigits = 2
barChartDataSet.valueFormatter = currencyNumberFormatter
// barChartData
let barChartData = BarChartData(xVals: dataPoints, dataSet: barChartDataSet)
barChartView.data = barChartData
To set your own colors/font you can use properties valueColors and 'valueFont' of BarChartDataSet class
So it'll be something like this
...
var valueColors = [UIColor]()
// dataEntries and barChartDataSet
var dataEntries = [ChartDataEntry]()
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
if values[i] < 0 {
valueColors.append(UIColor.redColor())
}
else {
valueColors.append(UIColor.greenColor())
}
}
let barChartDataSet = BarChartDataSet(yVals: dataEntries, label: "")
barChartDataSet.colors = colors
barChartDataSet.valueColors = valueColors
barChartDataSet.valueFont = *font you want*
If you want to change the text attributes of labels below bars you can use:
barChartView.xAxis.labelFont = UIFont.systemFont(ofSize: 5)
barChartView.xAxis.labelTextColor = UIColor.red

Resources