Error404 Error404 - 1 year ago 97
Swift Question

Is it possible to handle tap events on a column of BarChart?

I know that on

is possible to handle events on the charts. On its documentation are all well documented.

Nevertheless I am not able to find any documentation about the same events on
. I know that its creator tells that we should follow the documentaiton of
for his library also but I am not able to handle that events on Swift 3.0.

I also could not find any examples handle these events for

so, Is it possible to handle tap event on

EDIT: According to the feedback of
I am going to put here the function in which I set the data to the chart.

func enterData(valuesChart: [BarChartDataEntry]){
let chartDataSet = BarChartDataSet(values: valuesChart, label: "Total Values")

let charData = BarChartData(dataSets: [chartDataSet])
barChartView?.data = charData
barChartView?.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)

Thanks in advance!

Answer Source

Yeah, this functionality is built in to the charting library. Make sure your class conforms to the ChartViewDelegate protocol.

Edit full code sample

import UIKit
import Charts

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() {
        // 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)

    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])

        let dataSet = BarChartDataSet(values: dataEntries, label: "")
        var dataSets : [IChartDataSet] = [IChartDataSet]()

        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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download