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 delegate not called #1669

Closed
jinrui1130 opened this issue Oct 17, 2016 · 9 comments
Closed

chartValueSelected delegate not called #1669

jinrui1130 opened this issue Oct 17, 2016 · 9 comments

Comments

@jinrui1130
Copy link

I've read issue #695 and I have the same problem. Since I don't understand step 3 in the answer "3. whether pie chart view has detect your gesture (simply add bp inside @objc private func tapGestureRecognized in PieRadarChartViewBase)", I post my issue here.

My chart is rendered correctly and it has user interactions like tap and pinch zoom. Also chart is able to detect my tap gesture. But somehow

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

cannot be called. Really need some help here.

What I want to implement is that both balloon mark with same X value would show if I click on either a bar or a point on line. (both mark in above two pictures will display at same time when I tap a bar or a point)

Below is my entire controller code:

class FirstViewController: UIViewController,ChartViewDelegate {
@objc(ChartFormatter)
public class ChartFormatter: NSObject, IAxisValueFormatter{

    var months: [String]! = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]


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

        return months[Int(value)]
    }
}

@IBOutlet weak var chartView: CombinedChartView!
var lineChartData = LineChartData()
var barChartData = BarChartData()

override func viewDidLoad() {
    super.viewDidLoad()

    chartView.delegate = self

    chartView.chartDescription?.enabled = true

    chartView.dragEnabled = true
    chartView.setScaleEnabled(true)
    chartView.pinchZoomEnabled = true
    chartView.drawGridBackgroundEnabled = false

    let xAxis:XAxis = chartView.xAxis
    xAxis.labelPosition = .bottom

    xAxis.labelFont = UIFont.systemFont(ofSize: 11.0)
    xAxis.labelTextColor = UIColor.black
    xAxis.drawGridLinesEnabled = true
    xAxis.drawAxisLineEnabled = false
    xAxis.drawLabelsEnabled = false

    let leftAxis:YAxis = chartView.leftAxis;
    leftAxis.labelTextColor = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1)
    leftAxis.axisMaximum = 5.0
    leftAxis.axisMinimum = -5.0
    leftAxis.drawGridLinesEnabled = false
    leftAxis.drawZeroLineEnabled = true
    leftAxis.granularityEnabled = true
    leftAxis.drawLabelsEnabled = false

    let rightAxis:YAxis = chartView.rightAxis
    rightAxis.enabled = false

    let credit: [Double] = [1.0, 4.0, 2.0, 3.0, 3.0, 1.0, 3.0, 0.0, 0.0, 0.0, 0.0]
    let gpa: [Double] = [-1.0, -4.0, -1.5, -1.4, -1.3, -1.0, -3.0, 0.0, 0.0, 0.0, 0.0]
    let chartDescription = ""


    let marker = BalloonMarker(color: UIColor(white: 180/255, alpha: 1.0), font: UIFont.systemFont(ofSize:12.0), textColor: UIColor.white, insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0))

    marker.chartView = chartView
    marker.minimumSize = CGSize(width:80.0, height:40.0)

    chartView.marker = marker

    self.setLineChart(dataPoints: gpa, chartDescription: chartDescription)
    self.setBarChart(dataPoints: credit)

    self.setChartData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func setLineChart(dataPoints: [Double], chartDescription: String) {

    let formatter:ChartFormatter = ChartFormatter()
    let xaxis:XAxis = XAxis()

    var dataEntries: [ChartDataEntry] = Array()

    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(x: Double(i), y: dataPoints[i])
        dataEntries.append(dataEntry)
        formatter.stringForValue(Double(i), axis: xaxis)
    }

    let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Credit")
    self.lineChartData = LineChartData(dataSet: lineChartDataSet)

    xaxis.valueFormatter = formatter
    chartView.xAxis.valueFormatter = xaxis.valueFormatter

    xaxis.drawGridLinesEnabled = false

    lineChartDataSet.mode = .linear
    lineChartDataSet.drawCirclesEnabled = false
    lineChartDataSet.drawValuesEnabled = false

    lineChartDataSet.colors = [UIColor.brown]

    chartView.descriptionText = chartDescription


}

func setBarChart(dataPoints: [Double]) {
    let formatter:ChartFormatter = ChartFormatter()
    let xaxis:XAxis = XAxis()

    var dataEntries: [BarChartDataEntry] = Array()

    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(x: Double(i), y: dataPoints[i])
        dataEntries.append(dataEntry)
        formatter.stringForValue(Double(i), axis: xaxis)
    }
    let barChartDataSet = BarChartDataSet(values: dataEntries, label: "GPA")
    self.barChartData = BarChartData(dataSet: barChartDataSet)


    xaxis.valueFormatter = formatter
    chartView.xAxis.valueFormatter = xaxis.valueFormatter

    xaxis.drawGridLinesEnabled = false

    barChartDataSet.drawValuesEnabled = false
}

func setChartData() {
    let data:CombinedChartData = CombinedChartData()

    data.lineData = self.lineChartData
    data.barData = self.barChartData
    chartView.data = data
    chartView.data?.highlightEnabled = true
    //chartView.autoScaleMinMaxEnabled = true
    //chartView.setNeedsDisplay()
}

@objc func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: Highlight){
    print("###################")
}

@objc func chartValueNothingSelected(chartView: ChartViewBase){
    NSLog("chartValueNothingSelected");
}
@jinrui1130
Copy link
Author

Here is the tapGesture function, am I missing something?

 @objc fileprivate func tapGestureRecognized(_ recognizer: NSUITapGestureRecognizer)
{
if _data === nil
{
return
}

    if recognizer.state == NSUIGestureRecognizerState.ended
    {
        if !self.isHighLightPerTapEnabled { return }

        let h = getHighlightByTouchPoint(recognizer.location(in: self))

        if h === nil || h!.isEqual(self.lastHighlighted)
        {
            self.highlightValue(nil, callDelegate: true)
            self.lastHighlighted = nil
        }
        else
        {
            self.highlightValue(h, callDelegate: true)
            self.lastHighlighted = h
        }
    }
}

@liuxuan30
Copy link
Member

liuxuan30 commented Oct 18, 2016

check your if callDelegate && delegate != nil and if you got highlight object.
here's the original code

    /// Highlights the value selected by touch gesture.
    open func highlightValue(_ highlight: Highlight?, callDelegate: Bool)
    {
        var entry: ChartDataEntry?
        var h = highlight

        if h == nil
        {
            _indicesToHighlight.removeAll(keepingCapacity: false)
        }
        else
        {
            // set the indices to highlight
            entry = _data?.entryForHighlight(h!)
            if entry == nil
            {
                h = nil
                _indicesToHighlight.removeAll(keepingCapacity: false)
            }
            else
            {
                _indicesToHighlight = [h!]
            }
        }

        if callDelegate && delegate != nil
        {
            if h == nil
            {
                delegate!.chartValueNothingSelected?(self)
            }
            else
            {
                // notify the listener
                delegate!.chartValueSelected?(self, entry: entry!, highlight: h!)
            }
        }

        // redraw the chart
        setNeedsDisplay()
    }

@jinrui1130
Copy link
Author

Hi @liuxuan30 ,

This part of code you mentioned functions well. I set breakpoint at

                delegate!.chartValueNothingSelected?(self)

and it breaks correctly if I have highlight object.

but it cannot link to

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

in my controller.

@danielgindi
Copy link
Collaborator

Please read the #1474 migration notes. The chartValueSelected has changed

@kavoskhajavi
Copy link

kavoskhajavi commented Feb 6, 2017

override func viewDidLoad() {
super.viewDidLoad()
self.barView.delegate = self
}
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {

    print(entry.x)
    print(highlight)
    
}

delet dataSetIndex: Int

@hamz250
Copy link

hamz250 commented Feb 27, 2017

You must use func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) { ... } instead!

@almaskz
Copy link

almaskz commented Mar 27, 2017

hamz250 is right. Swift delegate method signature has changed, thats why you need to implement the method with the header:

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
...
}

The key here is _ (underscore) before the chartView:

@HPRaval
Copy link

HPRaval commented May 30, 2017

i have same signature but still method is not called at all

@optimusbits
Copy link

optimusbits commented May 22, 2018

pieChartView needs to have highlightPerTapEnabled = true to call delegate method:

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
...
}

look at PieRadarCharViewBase.swift:

    @objc private func tapGestureRecognized(_ recognizer: NSUITapGestureRecognizer)
    {
        if recognizer.state == NSUIGestureRecognizerState.ended
        {
      ----> if !self.isHighLightPerTapEnabled { return } <-----
            
            let location = recognizer.location(in: self)
            
            let high = self.getHighlightByTouchPoint(location)
            self.highlightValue(high, callDelegate: true)
        }
    }

and

    @objc open var isHighLightPerTapEnabled: Bool
    {
        return highlightPerTapEnabled
    }

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

8 participants