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

Getting the position of Data array of entry: ChartDataEntry via chartValueSelected(args) method #2750

Closed
CosmicYogi opened this issue Aug 25, 2017 · 3 comments

Comments

@CosmicYogi
Copy link

Hi,

I was working on a project and the problem I wanted to solve in my project is solve but I thought that if this thing is implemented then it would make many things very easy in some particular cases so I am putting it here.

If you want then you can get the value of selected point(x,y) poltteed on the chart via a protocol method chartValueSelected(args) like this-

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

        print(entry.x)//will print value of x coodinate for the selected point
        print(entry.y)//will print value of y coodinate for the selected point

}

But what if someone wants to know the position of that x or y coordinate in the arrays used for plotting the values.
like suppose I am making a Line chart.

let a: [Double] = [2,4,3,2,5,9]
let b: [Double] = [10,20,30,40,50,60]

        var dataEntries : [ChartDataEntry] = []
        
        for index in 0..<b.count{
            let dataEntry = ChartDataEntry(x: b[index], y: a[index])
            dataEntries.append(dataEntry)
        }

Now if we use chartValueSelected(args) we will ##get the values of a and b according to the plots but what if suppose I want to know the position of pointer with #respect to the value of coordinate which is selected.

like if I tap the coodinate (2,40) then I get the value 4 because it's on the 4th position or at 4th position in a or b technically.

I want to know that is there any way to do this or I have to tweak the library to extract this.

@liuxuan30
Copy link
Member

liuxuan30 commented Aug 29, 2017

You already have the entry. You also have your array. Why not use indexOf?

If you have duplicated values, then you must narrow down the range and mayge use another array to locate the index.
image

Note in Swift 3, it should be

func index(of: Any)

@khuong291
Copy link

I want to show tooltip at selected entry's position when I select. How to do this? I can not find document in your wiki.

@richo
Copy link

richo commented Sep 7, 2019

Is there no interest in storing the index somewhere? I have a substantial number of data points in my chart, and I'm looking to highlight some data on another view when a value is highlighted (Supporting dragging, so potentially doing this many times per second).

Using indexOf and doing a linear scan of the array on each update is going to be very expensive.

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

4 participants