Reputation: 156
I have a LineChartDataSet from which I can get the yMax & yMin values like so:
let max = lineDataSet.yMax
let min = lineDataSet.yMin
With the older version (2.3) of the Charts Library, I could get the y-values as an array and find the index of the highest value like so:
let values = lineDataSet.yVals
let index = values.index(of: yMax)
How can I get the x value of the max y-value in the LineChartDataSet?
So if the max value in my DataSet is 100, how can I get the associated x-value?
(The new version 3.0, does not let me get the y-values as an array anymore)
Upvotes: 0
Views: 1420
Reputation: 130172
You can still do the same:
let entries = lineDataSet.values
let index = entries.map { $0.y }.index(of: yMax)
or
let entries = lineDataSet.values
let index = entries.index(where: { $0.y == yMax })
Upvotes: 1
Reputation: 156
After working on this problem for a bit, this is what I came up with:
for i in 0 ..< lineDataSet.entryCount {
let value = lineDataSet?.entryForIndex(i)
if value?.y == lineDataSet.yMax {
xValueOfyMax = (value?.x)!
break
}
}
The same thing can be done to find the min value.
If you need to find the most recent x-value (since there may be more than one value that matches), you can reverse the for-loop like this:
for i in (0 ..< lineDataSet.entryCount).reversed() {
etc...
}
Upvotes: 0