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

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

Closed
1 task done
kiranGrg opened this issue Sep 20, 2021 · 25 comments

Comments

@kiranGrg
Copy link

kiranGrg commented Sep 20, 2021

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

@esma01
Copy link

esma01 commented Sep 20, 2021

I see the same issue. The sample project is also not working! Only the middle and last data points can be selected.

@SergejLogis
Copy link

SergejLogis commented Sep 22, 2021

Same here, 100% accurate description of an issue and the very same observations (Charts demo app)...

@PhillipMaizza
Copy link

I'm experiencing the same issue with line charts.

@esma01
Copy link

esma01 commented Sep 22, 2021

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

@SergejLogis
Copy link

SergejLogis commented Sep 22, 2021

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
    }

}

@PhillipMaizza
Copy link

I downgraded as well. Nice workaround though, @SergejLogis !

@kcome kcome mentioned this issue Sep 23, 2021
@kcome
Copy link
Contributor

kcome commented Sep 23, 2021

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

@SergejLogis
Copy link

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

@hrvoje0099
Copy link

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
Copy link
Contributor

kcome commented Oct 14, 2021

@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

@hrvoje0099
Copy link

hrvoje0099 commented Oct 14, 2021

@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

@faaizdaglawala
Copy link

faaizdaglawala commented Mar 25, 2022

@kcome I am facing crash in this solution.
after zooming the chart and then reloading the chart data crashes the app.

@kcome
Copy link
Contributor

kcome commented Mar 30, 2022

@kcome I am facing crash in this solution. after zooming the chart and then reloading the chart data crashes the app.

@faaizdaglawala I tested the demo app on simulator (xcode 13.3, iOS 15), I don't find crash after zooming and reloading chart data.
Could you give an example project and specific steps of reproducing it? Also could you give specific about "this solution"? Is it some comment in this issue discussion or a branch from some repo?

@kcome
Copy link
Contributor

kcome commented Apr 1, 2022

Hi @hrvoje0099 , I did another test and found the issue you described. And actually I just fixed it in another problem I encountered so today I merged the fix. If that suites you could you have a try? (this is the commit)
(sorry for late reply)

@faaizdaglawala
Copy link

faaizdaglawala commented Apr 1, 2022

@kcome I am facing crash in this solution. after zooming the chart and then reloading the chart data crashes the app.

@faaizdaglawala I tested the demo app on simulator (xcode 13.3, iOS 15), I don't find crash after zooming and reloading chart data. Could you give an example project and specific steps of reproducing it? Also could you give specific about "this solution"? Is it some comment in this issue discussion or a branch from some repo?

@kcome I had one chart view in which there are 2 lines in and then I reload that chart and add one line and one bar chart (CombinedChartView), now the scenario is when I zoom the chart when it is having one line and one bar chart (CombinedChartView), it works and then when I reload that chart to 2 lines then it crashes in subscript method saying index out of range

The crash is in ChartData file Swift/ContiguousArrayBuffer.swift:580: Fatal error: Index out of range
In blow method

public subscript(position: Index) -> Element { get { return dataSets[position] } set { self._dataSets[position] = newValue } }

This are the screenshot links of my charts
https://prnt.sc/Gzklfl20h_WC
https://prnt.sc/gTIj7I4Umn2a

@kcome
Copy link
Contributor

kcome commented Apr 3, 2022

@faaizdaglawala what you described in the comment and screenshots is probably another issue which is not related to this issue and the fix. I could reproduce a crash which is similar to your description, however the crash line is not the same as yours. Could you have a look at this #4798?

@faaizdaglawala
Copy link

@kcome understood your point but when I remove your changes, it starts working.

@pavlo-kravchenko
Copy link

@faaizdaglawala did you find a solution?

@faaizdaglawala
Copy link

@pavel-zlotarenchuk I downgraded the charts to version 3.6.0.

dudenamedjune added a commit to dudenamedjune/Charts that referenced this issue Apr 23, 2022
interactions(FIxes ChartsOrg#4719)

Changed the entriesForXValue and entryIndex method in the ChartDataSet
class to resolve ChartsOrg#4719. A few things were happening, entriesForXValue
was using a predicate for the partition function that would never find
the value it was looking for as it is giving you the correct location in
the tree to start your search and not actually searching; hence why the
values are rarely selected. The prefix array method for lazy rendering
views also needs a different match predicate since the partitioned array
has values that wont need to be highlighted. There is another additional
change I made in this PR related to the closest logic for entryIndex;
prior to this change it was grabbing the next index and not the closest.
Example of the incorrect closest value: xValue = 6.2 would return back
closest as 7 or even 12 whichever one was directly preceding its own
index, now it returns the shortest distance between x values so it can
be more accurate to the users touch.
@dudenamedjune
Copy link

dudenamedjune commented Apr 23, 2022

Have a fix for this 😅 ... was a debugging rodeo

#4817

@PorterHoskins
Copy link

+1

@PorterHoskins
Copy link

PorterHoskins commented May 9, 2022

Downgrading is not an option for people using SPM. It would be really nice to get this fix released.

@zygoat
Copy link

zygoat commented May 9, 2022

@PorterHoskins What prevents you from downgrading?

I import Charts from SPM; albeit my dependencies are configured in Xcode's pbxproj, but the equivalent in Packages.swift would be .upToNextMajor(from: "3.0.0") in the dependency spec.

@PorterHoskins
Copy link

Ok that worked... I was confused by the SPM support documentation in the 4.0 release. Sorry

@pmairoldi
Copy link
Collaborator

Closed by #4721

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