Neelam Pursnani
Neelam Pursnani

Reputation: 246

Horizontal Bar Chart: how to add X-Bar Axis Labels

Here I am using Charts Library For Horizontal Bar chart , i have set code as below

var dataEntries: [BarChartDataEntry] = []

for i in 0..<dataPoints.count {

    let dataEntry = BarChartDataEntry(x: Double(i), yValues: [values[i]])

          dataEntries.append(dataEntry)
}

let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")

let chartData = BarChartData(dataSet: chartDataSet)
barChartView.data = chartData

       //Horizontal

        HbarChartView.animate(yAxisDuration: 0.2)
        HbarChartView.data = chartData
        HbarChartView.xAxis.labelPosition = .bottom
        HbarChartView.xAxis.labelTextColor = UIColor.blue
        HbarChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)
        HbarChartView.rightAxis.addLimitLine(ll)
        HbarChartView.setVisibleYRange(minYRange: 0, maxYRange: 10, axis: .left)

X-Axis data is in blue.

I want to set String on X-Axis. How can i do this?

Any help is Appreciated

enter image description here

Or else, Suggest any other Chart Library for Horizontal Bar including Bar Value and Animation

Upvotes: 1

Views: 3198

Answers (1)

muescha
muescha

Reputation: 1539

you can implement IAxisValueFormatter in your own classes:

for this you need only to implement this method:

func stringForValue(_ value: Double, axis: AxisBase?) -> String

Examples:

or you follow the RayWenderlich Tutorial "Using Realm and Charts with Swift 3 in iOS 10":

you have to set some delegates and more but this is the key code snippet for a custom formatter:

// MARK: axisFormatDelegate
extension ViewController: IAxisValueFormatter {

  func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = “HH:mm.ss”
    return dateFormatter.string(from: Date(timeIntervalSince1970: value))
  }
}

Upvotes: 1

Related Questions