Charts: "chartValueSelected" is not getting called.

I did all things asked on http://stackoverflow.com/questions/35038724/tap-callback-on-bar-in-barchart-in-ios-charts . @danielgindi did you do any changes in the library, preventing to detect the Bar tap event? Thanks in advance.

import UIKit
import Charts

class QuarterlyForecastOverviewViewController: UIViewController, ChartViewDelegate {
    
    @IBOutlet weak var barChartView: BarChartView!
    
    var months: [String]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        months = ["Jan", "Feb", "Mar", "Apr"]
        let unitsSold = [20.0, 4.0, 6.0, 3.0]
        
        setChart(dataPoints: months, values: unitsSold)
        
        barChartView.notifyDataSetChanged()

        barChartView.pinchZoomEnabled = false
        barChartView.dragEnabled = false
        barChartView.setScaleEnabled(false)
        
        barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
        barChartView.xAxis.labelTextColor = UIColor.gray
        barChartView.xAxis.axisLineColor = UIColor.white
        barChartView.leftAxis.axisMinValue = 0
        barChartView.leftAxis.axisLineColor = UIColor.red
        barChartView.leftAxis.removeAllLimitLines()
        barChartView.leftAxis.drawZeroLineEnabled = false
        barChartView.leftAxis.drawLimitLinesBehindDataEnabled = true
        barChartView.leftAxis.labelTextColor = UIColor.white
        barChartView.rightAxis.enabled = false
        barChartView.extraBottomOffset = 10.0
        barChartView.extraRightOffset = 10.0
        barChartView.highlightPerTapEnabled = true
        barChartView.drawGridBackgroundEnabled = false
        barChartView.xAxis.drawGridLinesEnabled = false
        barChartView.leftAxis.drawGridLinesEnabled = false
        barChartView.descriptionText = ""
        
        barChartView.xAxis.granularity = 1
        barChartView.xAxis.granularityEnabled = true
        barChartView.xAxis.labelCount = 4
        
        barChartView.delegate = self
    }
    
    func setChart(dataPoints: [String], values: [Double]) {
        
        let formatter = BarChartFormatter()
        formatter.setValues(values: dataPoints)
        let xaxis:XAxis = XAxis()
        
        barChartView.noDataText = "You need to provide data for the chart."
        var dataEntries: [BarChartDataEntry] = []
        
        for i in 0..<dataPoints.count
        {
            let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
            dataEntries.append(dataEntry)
        }
        
        let chartDataSet = BarChartDataSet(values: dataEntries, label: "Billing Hours")
        
        let chartData = BarChartData(dataSet: chartDataSet)

        xaxis.valueFormatter = formatter
        barChartView.xAxis.labelPosition = .bottom
        barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0, easingOption: .easeInBounce)
        chartDataSet.colors = [UIColor(red: 230/255, green: 126/255, blue: 34/255, alpha: 1)]
        barChartView.xAxis.drawGridLinesEnabled = false
        barChartView.xAxis.valueFormatter = xaxis.valueFormatter
        barChartView.chartDescription?.enabled = false
        barChartView.legend.enabled = true
        barChartView.rightAxis.enabled = false
        barChartView.data = chartData
  
    }
    
    func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: Highlight) {
        print("Bar selected")
    }
    
}


@objc(BarChartFormatter)
public class BarChartFormatter: NSObject, IAxisValueFormatter
{
    var names = [String]()
    
    public func stringForValue(_ value: Double, axis: AxisBase?) -> String
    {
        return names[Int(value)]
    }
    
    public func setValues(values: [String])
    {
        self.names = values
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (3 by maintainers)

Commits related to this issue

Most upvoted comments

try

    public func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
        print("Bar selected")
    }

and not

func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: Highlight) {
    print("Bar selected")
}

The latest version has a data selection bug. I downgraded to 3.5.0 and the selections work now. @tolgaytoklar

Thank you!!! That’s been killing me for 2 days - only a few points on the chart caused a call to chartValueSelected. I’ve reverted to 3.6.0 and it’s now working…

@SatishBirajdar , @danielgindi , @thierryH91200 I have applied all above mentioned steps but it’s not working. Please review code on below attached link and let Mme know what I’m missing https://stackoverflow.com/questions/67197708/chartvalueselected-chartview-chartviewbase-entry-chartdataentry-highlight