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

Highlight enhancements (Closes #654, closes #702) #1012

Merged
merged 1 commit into from
May 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Charts/Classes/Charts/BarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class BarChartView: BarLineChartViewBase, BarChartDataProvider
return nil
}

return self.highlighter?.getHighlight(x: Double(pt.x), y: Double(pt.y))
return self.highlighter?.getHighlight(x: pt.x, y: pt.y)
}

/// - returns: the bounding box of the specified Entry in the specified DataSet. Returns null if the Entry could not be found in the charts data.
Expand Down
2 changes: 1 addition & 1 deletion Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,7 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar
return nil
}

return self.highlighter?.getHighlight(x: Double(pt.x), y: Double(pt.y))
return self.highlighter?.getHighlight(x: pt.x, y: pt.y)
}

/// - returns: the x and y values in the chart at the given touch point
Expand Down
11 changes: 10 additions & 1 deletion Charts/Classes/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,13 @@ public class ChartViewBase: NSUIView, ChartDataProvider, ChartAnimatorDelegate
highlightValue(highlight: highlight, callDelegate: false)
}

/// Highlights the value at the given x-index in the given DataSet.
/// Provide -1 as the x-index to undo all highlighting.
public func highlightValue(xIndex xIndex: Int, dataSetIndex: Int)
{
highlightValue(xIndex: xIndex, dataSetIndex: dataSetIndex, callDelegate: true)
}

/// Highlights the value at the given x-index in the given DataSet.
/// Provide -1 as the x-index to undo all highlighting.
public func highlightValue(xIndex xIndex: Int, dataSetIndex: Int, callDelegate: Bool)
Expand Down Expand Up @@ -482,7 +489,9 @@ public class ChartViewBase: NSUIView, ChartDataProvider, ChartAnimatorDelegate
{
// set the indices to highlight
entry = _data?.getEntryForHighlight(h!)
if (entry === nil || entry!.xIndex != h?.xIndex)
if (entry == nil ||
entry?.xIndex != h?.xIndex ||
(entry?.value != h!.value && !isnan(h!.value)))
{
h = nil
entry = nil
Expand Down
2 changes: 1 addition & 1 deletion Charts/Classes/Charts/HorizontalBarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public class HorizontalBarChartView: BarChartView
return nil
}

return self.highlighter?.getHighlight(x: Double(pt.y), y: Double(pt.x))
return self.highlighter?.getHighlight(x: pt.y, y: pt.x)
}

public override var lowestVisibleXIndex: Int
Expand Down
2 changes: 1 addition & 1 deletion Charts/Classes/Charts/PieRadarChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ public class PieRadarChartViewBase: ChartViewBase
// get the dataset that is closest to the selection (PieChart only has one DataSet)
if (self.isKindOfClass(RadarChartView))
{
dataSetIndex = ChartUtils.closestDataSetIndex(valsAtIndex, value: Double(distance / (self as! RadarChartView).factor), axis: nil)
dataSetIndex = ChartUtils.closestDataSetIndexByValue(valsAtIndex: valsAtIndex, value: Double(distance / (self as! RadarChartView).factor), axis: nil) ?? -1
}

if (dataSetIndex < 0)
Expand Down
10 changes: 10 additions & 0 deletions Charts/Classes/Data/Implementations/ChartBaseDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public class ChartBaseDataSet: NSObject, IChartDataSet
fatalError("yValForXIndex is not implemented in ChartBaseDataSet")
}

public func yValsForXIndex(x: Int) -> [Double]
{
fatalError("yValsForXIndex is not implemented in ChartBaseDataSet")
}

public func entryForIndex(i: Int) -> ChartDataEntry?
{
fatalError("entryForIndex is not implemented in ChartBaseDataSet")
Expand All @@ -86,6 +91,11 @@ public class ChartBaseDataSet: NSObject, IChartDataSet
fatalError("entryForXIndex is not implemented in ChartBaseDataSet")
}

public func entriesForXIndex(x: Int) -> [ChartDataEntry]
{
fatalError("entriesForXIndex is not implemented in ChartBaseDataSet")
}

public func entryIndex(xIndex x: Int, rounding: ChartDataSetRounding) -> Int
{
fatalError("entryIndex is not implemented in ChartBaseDataSet")
Expand Down
13 changes: 12 additions & 1 deletion Charts/Classes/Data/Implementations/Standard/ChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,18 @@ public class ChartData: NSObject
}
else
{
return _dataSets[highlight.dataSetIndex].entryForXIndex(highlight.xIndex)
// The value of the highlighted entry could be NaN - if we are not interested in highlighting a specific value.

let entries = _dataSets[highlight.dataSetIndex].entriesForXIndex(highlight.xIndex)
for e in entries
{
if e.value == highlight.value || isnan(highlight.value)
{
return e
}
}

return nil
}
}

Expand Down
36 changes: 28 additions & 8 deletions Charts/Classes/Data/Implementations/Standard/ChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ public class ChartDataSet: ChartBaseDataSet
else { return Double.NaN }
}

/// - returns: all of the y values of the Entry objects at the given xIndex. Returns NaN if no value is at the given x-index.
public override func yValsForXIndex(x: Int) -> [Double]
{
let entries = self.entriesForXIndex(x)

var yVals = [Double]()
for e in entries
{
yVals.append(e.value)
}

return yVals
}

/// - returns: the entry object found at the given index (not x-index!)
/// - throws: out of bounds
/// if `i` is out of bounds, it may throw an out-of-bounds exception
Expand Down Expand Up @@ -181,7 +195,9 @@ public class ChartDataSet: ChartBaseDataSet
return entryForXIndex(x, rounding: .Closest)
}

public func entriesForXIndex(x: Int) -> [ChartDataEntry]
/// - returns: all Entry objects found at the given xIndex with binary search.
/// An empty array if no Entry object at that index.
public override func entriesForXIndex(x: Int) -> [ChartDataEntry]
{
var entries = [ChartDataEntry]()

Expand All @@ -190,7 +206,7 @@ public class ChartDataSet: ChartBaseDataSet

while (low <= high)
{
var m = Int((high + low) / 2)
var m = (high + low) / 2
var entry = _yVals[m]

if (x == entry.xIndex)
Expand All @@ -215,15 +231,19 @@ public class ChartDataSet: ChartBaseDataSet

m += 1
}
}

if (x > _yVals[m].xIndex)
{
low = m + 1

break
}
else
{
high = m - 1
if (x > _yVals[m].xIndex)
{
low = m + 1
}
else
{
high = m - 1
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public class CombinedChartData: BarLineScatterCandleBubbleChartData
data.append(bubbleData)
}

return data;
return data
}

public override func notifyDataChanged()
Expand Down Expand Up @@ -205,4 +205,41 @@ public class CombinedChartData: BarLineScatterCandleBubbleChartData

super.notifyDataChanged() // recalculate everything
}


/// Get the Entry for a corresponding highlight object
///
/// - parameter highlight:
/// - returns: the entry that is highlighted
public override func getEntryForHighlight(highlight: ChartHighlight) -> ChartDataEntry?
{
let dataObjects = allData

if highlight.dataIndex >= dataObjects.count
{
return nil
}

let data = dataObjects[highlight.dataIndex]

if highlight.dataSetIndex >= data.dataSetCount
{
return nil
}
else
{
// The value of the highlighted entry could be NaN - if we are not interested in highlighting a specific value.

let entries = data.getDataSetByIndex(highlight.dataSetIndex).entriesForXIndex(highlight.xIndex)
for e in entries
{
if e.value == highlight.value || isnan(highlight.value)
{
return e
}
}

return nil
}
}
}
7 changes: 7 additions & 0 deletions Charts/Classes/Data/Interfaces/IChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public protocol IChartDataSet
/// - returns: the value of the Entry object at the given xIndex. Returns NaN if no value is at the given x-index.
func yValForXIndex(x: Int) -> Double

/// - returns: all of the y values of the Entry objects at the given xIndex. Returns NaN if no value is at the given x-index.
func yValsForXIndex(x: Int) -> [Double]

/// - returns: the entry object found at the given index (not x-index!)
/// - throws: out of bounds
/// if `i` is out of bounds, it may throw an out-of-bounds exception
Expand All @@ -55,6 +58,10 @@ public protocol IChartDataSet
/// nil if no Entry object at that index.
func entryForXIndex(x: Int) -> ChartDataEntry?

/// - returns: all Entry objects found at the given xIndex with binary search.
/// An empty array if no Entry object at that index.
func entriesForXIndex(x: Int) -> [ChartDataEntry]

/// - returns: the array-index of the specified entry
///
/// - parameter x: x-index of the entry to search for
Expand Down
Loading