Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"chartValueSelected" is not getting called. #2383

Closed
SatishBirajdar opened this issue Apr 21, 2017 · 17 comments
Closed

"chartValueSelected" is not getting called. #2383

SatishBirajdar opened this issue Apr 21, 2017 · 17 comments

Comments

@SatishBirajdar
Copy link

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
    }
}
@liuxuan30
Copy link
Member

liuxuan30 commented Apr 24, 2017

This is nothing changed for the delegate. Please debug on your side to give proofs.
Besides, put setChart(dataPoints: months, values: unitsSold) after you set up your chart property like barChartView.xAxis.labelCount = 4. You are changing chart properties after feeding data, which will cause issues such as the highlight.

@SatishBirajdar
Copy link
Author

SatishBirajdar commented Apr 24, 2017

@liuxuan30 Okay, I have re-arranged barChartView.xAxis.labelCount = 4 and setChart(dataPoints: months, values: unitsSold), thanks for the suggestion. But the issue isn't fixed yet.

In ChartViewBase Class -

delegate!.chartValueSelected?(self, entry: entry!, highlight: h!) = nil , but,
delegate = SatishChartsDemo.QuarterlyForecastOverviewViewController

So my concern:

  1. why is delegate!.chartValueSelected?(self, entry: entry!, highlight: h!) = nil, since we already have func chartValueSelected defined in QuarterlyForecastOverviewViewController class ?

  2. In QuarterlyForecastOverviewViewController, am I defining the function chartValueSelected, correctly?

Thanks in advance.

@thierryH91200
Copy link
Contributor

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")
}

@SatishBirajdar
Copy link
Author

@thierryH91200 Yes, I tried

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

Didn't worked. Can you please provide the link of the sample project in Swift, where function chartValueSelected() is working?

Thanks in advance.

@thierryH91200
Copy link
Contributor

BarChartViewController.swift.zip

@SatishBirajdar
Copy link
Author

@thierryH91200 Thanks, this is really helpful. :)

@anoo-radha
Copy link

anoo-radha commented May 30, 2017

@SatishBirajdar did you get the delegate chartValueSelected to work? I ran into the same problem of the delegate not being called.
Thanks in advance

EDIT: thanx @thierryH91200 . saw that

barChartView.delegate = self

is the answer to cal the delegate function

@SatishBirajdar
Copy link
Author

@anoo-radha Try the .zip sample that @thierryH91200 uploaded

@Renish-GlobeSync
Copy link

Renish-GlobeSync commented Apr 22, 2021

@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

@esma01
Copy link

esma01 commented Sep 20, 2021

Even the sample project is not working! Only the last and middle data points can be selected. For other data points, I see chartValueNothingSelected in the console.

@tolgaytoklar
Copy link

Even the sample project is not working! Only the last and middle data points can be selected. For other data points, I see chartValueNothingSelected in the console.

I have the exact same problem

@esma01
Copy link

esma01 commented Sep 21, 2021

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

@tolgaytoklar
Copy link

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

Thank you so much, it also works for me now.

@FleetPhil
Copy link

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...

betterhee added a commit to betterhee/Coins that referenced this issue Nov 18, 2021
betterhee added a commit to betterhee/Coins that referenced this issue Nov 20, 2021
@Alain1919
Copy link

I've just run into this issue with a line chart and assume it's the same issue, had upgraded from the cocoapod to the Swift Package Manager 4.0.2 and banged my head against the table for several hours trying to find out what was up. Hopefully this is rectified soon!

@trunghvbk
Copy link

trunghvbk commented Jul 11, 2023

Hi, is this issue fixed on the stable version? I found chartValueSelected not called still in ver 4.1.

@MorGoren
Copy link

MorGoren commented Aug 7, 2024

Hi. Ver 4.1.1 still has this problem, is there an update for the fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests