Yes, this functionality is built into the graphics library. Make sure your class complies with the ChartViewDelegate protocol.
Change sample full code
import UIKit import Charts @objc(BarChartFormatter) public class BarChartFormatter: NSObject, IAxisValueFormatter { var months: [String]! = ["Jan", "Feb", "Mar"] public func stringForValue(_ value: Double, axis: AxisBase?) -> String { return months[Int(value)] } } class ViewController: UIViewController,ChartViewDelegate { @IBOutlet var barChart : BarChartView! let dataArray = ["Jan", "Feb", "Mar"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.title = "Testing" let values = [5.2,10.5,15.3] setBarChart(values: values) barChart.delegate = self } //MARK: Chart delegate methods func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) { let pos = NSInteger(entry.x) print(dataArray[pos]) } func chartValueNothingSelected(_ chartView: ChartViewBase) { } //MARK: Set chart func setBarChart(values: [Double]) { var dataEntries: [BarChartDataEntry] = [] for i in 0..<values.count { let dataEntry = BarChartDataEntry(x: Double(i), y: values[i]) dataEntries.append(dataEntry) } let dataSet = BarChartDataSet(values: dataEntries, label: "") var dataSets : [IChartDataSet] = [IChartDataSet]() dataSets.append(dataSet) let format:BarChartFormatter = BarChartFormatter() let xAxis = barChart.xAxis xAxis.granularity = 1.0 xAxis.labelPosition = .bottom xAxis.valueFormatter = format let chartData = BarChartData(dataSets: dataSets) barChart?.data = chartData } }
Adamm source share