Charts: ChartViewDelegate Function func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) is not being called for every tap/touch inside the ChartView

What did you do?

I upgraded the Charts(iOS) version to 4.0.1 from 3.6.0 recently. Fixed all the changes in the codebase. Everything seems to be working except for one behavior on the chart.

What did you expect to happen?

Enabled the user interaction on the chart. Added the ChartViewDelegate, and implemented the optional delegate function public func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight)

What happened instead?

However, apparently, this delegate function public func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) is not being triggered every time when the tap inside the chart view (to be specific I was using LineChartView) is made, instead only for some places in the chart view seems to be responding to those touch events.

I am pretty, sure the delegate function used to be called without failing in Charts 3.6.0.

Additional Findings:

I tried the official demo version for iOS source codes for both 3.6.0 and 4.0.1, as I couldn’t get this issue fixed. Looks like, the demo itself is exhibiting this problem, as I could see the delegate function being called with every tap/touch in 3.6.0 but not in the demo version 4.0.1

Charts Environment

Charts version/Branch/Commit Number: 4.0.1 Xcode version: 12.5.1 Swift version: 5.4.2 Platform(s) running Charts: iOS device/simulators macOS version running Xcode: 11.5.2

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 25 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Yeah, that helped me as well. Only I didn’t downgrade the entire library to 3.6.0, but instead created a custom LineChartDataSet with an overridden method:

class CustomLineChartDataSet: LineChartDataSet {

    override func entriesForXValue(_ xValue: Double) -> [ChartDataEntry] {
        var entries = [ChartDataEntry]()

        var low = startIndex
        var high = endIndex - 1

        while low <= high {
            var mid = (high + low) / 2
            var entry = self[mid]

            // if we have a match
            if xValue == entry.x {
                while mid > 0 && self[mid - 1].x == xValue {
                    mid -= 1
                }

                high = endIndex

                // loop over all "equal" entries
                while mid < high {
                    entry = self[mid]
                    if entry.x == xValue {
                        entries.append(entry)
                    } else {
                        break
                    }

                    mid += 1
                }

                break

            } else {
                if xValue > entry.x {
                    low = mid + 1
                } else {
                    high = mid - 1
                }
            }
        }

        return entries
    }

}

@liuxuan30 @jjatie Thanks for having a look at this PR that fixes this issue.

@hrvoje0099 are you using the pull-request branch here? (or use https://github.com/kcome/Charts branch 4719-highlighter-fix in your project) This ChartsDemo-iOS app video shows that PR branch has correct bar chart highlight.

screen-recording-2021-10-14-at-202737_bzAFQSeK.mp4

I downloaded whole project (https://github.com/danielgindi/Charts) i run Demo app. I added this changes from your fix in my projec: image but it didn’t solve my bug completely, the problem I wrote above remained. What else do I need to change to make it like yours ? This is with your fix in demo, look last bar right half: https://www.screencast.com/t/sTnLLP7x

@PhillipMaizza @SergejLogis Downgrading the charts to 3.6.0 worked for me!

This repair does not solve the problem with Bar Charts. Exactly halfway up the bar is the boundary with select. If we press in the left half we will mark that bar, but if we press in the right half or nothing will happen or it will mark the second bar on the right. Its seems that the problem is in xValue. Eg. for coordinates x = 12, y = 50 returns -0.3 for xValue. @kcome

@kcome I’ve tried your solution and it works like charm! 🥰 Much better than the one back-ported from 3.6.0. 🚀🙇‍♂️