7

How can I change attributes (e.g. font size, text color, etc...) of text above a specific bar in a BarChart?

Bar chart example.

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
horothesun
  • 427
  • 1
  • 6
  • 15
  • create outlets to the text labels and change their attributes? like textLabelXY.fontsize etc? – David Seek Apr 20 '16 at 12:33
  • 1
    what have you tryed so far? – David Seek Apr 20 '16 at 12:33
  • I don't think I can create outlets from labels inside a `BarChartView`. I tried to find some property of `BarChartDataSet`, but the only one involving those labels I found was `valueFormatter`, which operates on `String`s (not on `NSAttributedString`). – horothesun Apr 20 '16 at 13:10
  • well. i don't know the framework you're using. but what is it build in interface builder? or is it just set promatically? – David Seek Apr 20 '16 at 13:12
  • how does the "-$5000" gets into the "label"? – David Seek Apr 20 '16 at 13:12
  • I programmatically built a `BarChartDataSet` for the `BarChartView` from X and Y values. Using the `valueFormatter` property of `BarChartDataSet` I converted `Double` values into `String`s like *"-$5,000.00"*, but I couldn't control font size or text color. – horothesun Apr 20 '16 at 13:29
  • okay. could you please post the code you're using to sent the string to the barchart – David Seek Apr 20 '16 at 13:31
  • I edited the first post with sample code. – horothesun Apr 20 '16 at 14:55
  • what options does f.e. `barChartView.xAxis.`offer? if you enter the "." at the end? are there any options to define the text color? – David Seek Apr 20 '16 at 15:05
  • and you only want to edit the color for a "specific" bar? – David Seek Apr 20 '16 at 15:06
  • i think a possibility should be within the framework. where the text label itself is specified. something like `if string == < 0 { font.color = UIColor.redcolor }` or something like this. but we need to find the class or method that defines the label... – David Seek Apr 20 '16 at 15:10
  • @horothesun can you share how to access the label above bar charts in swift 2 ?? http://stackoverflow.com/questions/41979926/how-to-achieve-trackball-and-access-label-above-bar-on-bar-charts-in-swift-3x – Harshit Goel Feb 03 '17 at 05:00
  • How did you add Bank #1 , Bank#2 and so on? Could you help me , please? [my question](https://stackoverflow.com/questions/46618636/how-to-add-string-labels-to-horizontal-bar-in-charts-framework/) – Amir Shabani Oct 07 '17 at 13:52

2 Answers2

22

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*
keyv
  • 2,430
  • 1
  • 18
  • 15
  • How can i Change the label above bar in swift2 ?? I have achieved this in swift 3 http://stackoverflow.com/questions/41979926/how-to-achieve-trackball-on-charts-in-swift-3x – Harshit Goel Feb 03 '17 at 04:49
  • Is it also possible to change value font like attributedString? – David Aug 10 '17 at 09:18
  • To add an eample so that a newbie can try it easily; barChartDataSet.valueFont = UIFont.systemFont(ofSize: 8) – Totoro Nov 21 '20 at 13:19
2

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

Milo
  • 3,365
  • 9
  • 30
  • 44