Skip to content

Commit

Permalink
Improved highlight for scatter/bubble, and fixes highlightValueWithX
Browse files Browse the repository at this point in the history
(Closes #1495)
  • Loading branch information
danielgindi committed Oct 10, 2016
1 parent 5cdc3bd commit 32a5952
Show file tree
Hide file tree
Showing 20 changed files with 395 additions and 252 deletions.
6 changes: 3 additions & 3 deletions Source/Charts/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,12 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD

if h === nil || h!.isEqual(self.lastHighlighted)
{
self.highlightValue(highlight: nil, callDelegate: true)
self.highlightValue(nil, callDelegate: true)
self.lastHighlighted = nil
}
else
{
self.highlightValue(highlight: h, callDelegate: true)
self.highlightValue(h, callDelegate: true)
self.lastHighlighted = h
}
}
Expand Down Expand Up @@ -715,7 +715,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
(h !== nil && lastHighlighted !== nil && !h!.isEqual(lastHighlighted)))
{
self.lastHighlighted = h
self.highlightValue(highlight: h, callDelegate: true)
self.highlightValue(h, callDelegate: true)
}
}
}
Expand Down
79 changes: 43 additions & 36 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate

/// Highlights the values at the given indices in the given DataSets. Provide
/// null or an empty array to undo all highlighting.
/// This should be used to programmatically highlight values.
/// This DOES NOT generate a callback to the delegate.
/// This should be used to programmatically highlight values.
/// This method *will not* call the delegate.
open func highlightValues(_ highs: [Highlight]?)
{
// set the indices to highlight
Expand All @@ -468,44 +468,71 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
setNeedsDisplay()
}


/// Highlights the values represented by the provided Highlight object
/// This DOES NOT generate a callback to the delegate.
/// - parameter highlight: contains information about which entry should be highlighted
open func highlightValue(_ highlight: Highlight?)
/// Highlights any y-value at the given x-value in the given DataSet.
/// Provide -1 as the dataSetIndex to undo all highlighting.
/// This method will call the delegate.
/// - parameter x: The x-value to highlight
/// - parameter dataSetIndex: The dataset index to search in
open func highlightValue(x: Double, dataSetIndex: Int)
{
highlightValue(highlight: highlight, callDelegate: false)
highlightValue(x: x, dataSetIndex: dataSetIndex, callDelegate: true)
}

/// Highlights the value at the given x-value in the given DataSet.
/// Highlights the value at the given x-value and y-value in the given DataSet.
/// Provide -1 as the dataSetIndex to undo all highlighting.
open func highlightValue(x: Double, dataSetIndex: Int)
/// This method will call the delegate.
/// - parameter x: The x-value to highlight
/// - parameter y: The y-value to highlight. Supply `NaN` for "any"
/// - parameter dataSetIndex: The dataset index to search in
open func highlightValue(x: Double, y: Double, dataSetIndex: Int)
{
highlightValue(x: x, dataSetIndex: dataSetIndex, callDelegate: true)
highlightValue(x: x, y: y, dataSetIndex: dataSetIndex, callDelegate: true)
}

/// Highlights the value at the given x-value in the given DataSet.
/// Highlights any y-value at the given x-value in the given DataSet.
/// Provide -1 as the dataSetIndex to undo all highlighting.
/// - parameter x: The x-value to highlight
/// - parameter dataSetIndex: The dataset index to search in
/// - parameter callDelegate: Should the delegate be called for this change
open func highlightValue(x: Double, dataSetIndex: Int, callDelegate: Bool)
{
highlightValue(x: x, y: Double.nan, dataSetIndex: dataSetIndex, callDelegate: callDelegate)
}

/// Highlights the value at the given x-value and y-value in the given DataSet.
/// Provide -1 as the dataSetIndex to undo all highlighting.
/// - parameter x: The x-value to highlight
/// - parameter y: The y-value to highlight. Supply `NaN` for "any"
/// - parameter dataSetIndex: The dataset index to search in
/// - parameter callDelegate: Should the delegate be called for this change
open func highlightValue(x: Double, y: Double, dataSetIndex: Int, callDelegate: Bool)
{
guard let data = _data else
{
Swift.print("Value not highlighted because data is nil")
return
}

if dataSetIndex < 0 || dataSetIndex >= data.dataSetCount
{
highlightValue(highlight: nil, callDelegate: callDelegate)
highlightValue(nil, callDelegate: callDelegate)
}
else
{
highlightValue(highlight: Highlight(x: x, dataSetIndex: dataSetIndex), callDelegate: callDelegate)
highlightValue(Highlight(x: x, y: y, dataSetIndex: dataSetIndex), callDelegate: callDelegate)
}
}

/// Highlights the values represented by the provided Highlight object
/// This method *will not* call the delegate.
/// - parameter highlight: contains information about which entry should be highlighted
open func highlightValue(_ highlight: Highlight?)
{
highlightValue(highlight, callDelegate: false)
}

/// Highlights the value selected by touch gesture.
open func highlightValue(highlight: Highlight?, callDelegate: Bool)
open func highlightValue(_ highlight: Highlight?, callDelegate: Bool)
{
var entry: ChartDataEntry?
var h = highlight
Expand Down Expand Up @@ -785,26 +812,6 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
return _viewPortHandler.contentRect
}

/// Get all Entry objects at the given index across all DataSets.
open func getEntriesAtIndex(_ xValue: Double) -> [ChartDataEntry]
{
var vals = [ChartDataEntry]()

guard let data = _data else { return vals }

for i in 0 ..< data.dataSetCount
{
guard let set = data.getDataSetByIndex(i)
else { continue }
if let e = set.entryForXValue(xValue)
{
vals.append(e)
}
}

return vals
}

/// - returns: The ViewPortHandler of the chart that is responsible for the
/// content area of the chart and its offsets and dimensions.
open var viewPortHandler: ViewPortHandler!
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Charts/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ open class PieChartView: PieRadarChartViewBase

for i in 0 ..< dataSets.count
{
if (dataSets[i].entryForXValue(xValue) !== nil)
if (dataSets[i].entryForXValue(xValue, closestToY: Double.nan) !== nil)
{
return i
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Charts/PieRadarChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ open class PieRadarChartViewBase: ChartViewBase
let location = recognizer.location(in: self)

let high = self.getHighlightByTouchPoint(location)
self.highlightValue(highlight: high, callDelegate: true)
self.highlightValue(high, callDelegate: true)
}
}

Expand Down
22 changes: 15 additions & 7 deletions Source/Charts/Data/Implementations/ChartBaseDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,32 @@ open class ChartBaseDataSet: NSObject, IChartDataSet
fatalError("entryForIndex is not implemented in ChartBaseDataSet")
}

open func entryForXValue(_ x: Double, rounding: ChartDataSetRounding) -> ChartDataEntry?
open func entryForXValue(
_ x: Double,
closestToY y: Double,
rounding: ChartDataSetRounding) -> ChartDataEntry?
{
fatalError("entryForXValue(x, rounding) is not implemented in ChartBaseDataSet")
fatalError("entryForXValue(x, closestToY, rounding) is not implemented in ChartBaseDataSet")
}

open func entryForXValue(_ x: Double) -> ChartDataEntry?
open func entryForXValue(
_ x: Double,
closestToY y: Double) -> ChartDataEntry?
{
fatalError("entryForXValue(x) is not implemented in ChartBaseDataSet")
fatalError("entryForXValue(x, closestToY) is not implemented in ChartBaseDataSet")
}

open func entriesForXValue(_ x: Double) -> [ChartDataEntry]
{
fatalError("entriesForXValue is not implemented in ChartBaseDataSet")
}

open func entryIndex(x: Double, rounding: ChartDataSetRounding) -> Int
open func entryIndex(
x xValue: Double,
closestToY y: Double,
rounding: ChartDataSetRounding) -> Int
{
fatalError("entryIndex(x, rounding) is not implemented in ChartBaseDataSet")
fatalError("entryIndex(x, closestToY, rounding) is not implemented in ChartBaseDataSet")
}

open func entryIndex(entry e: ChartDataEntry) -> Int
Expand Down Expand Up @@ -134,7 +142,7 @@ open class ChartBaseDataSet: NSObject, IChartDataSet

open func removeEntry(x: Double) -> Bool
{
if let entry = entryForXValue(x)
if let entry = entryForXValue(x, closestToY: Double.nan)
{
return removeEntry(entry)
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Charts/Data/Implementations/Standard/ChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ open class ChartData: NSObject
}
else
{
return dataSets[highlight.dataSetIndex].entryForXValue(highlight.x)
return dataSets[highlight.dataSetIndex].entryForXValue(highlight.x, closestToY: highlight.y)
}
}

Expand Down Expand Up @@ -537,7 +537,7 @@ open class ChartData: NSObject
return false
}

if let entry = _dataSets[dataSetIndex].entryForXValue(xValue)
if let entry = _dataSets[dataSetIndex].entryForXValue(xValue, closestToY: Double.nan)
{
return removeEntry(entry, dataSetIndex: dataSetIndex)
}
Expand All @@ -557,7 +557,7 @@ open class ChartData: NSObject
{
let set = _dataSets[i]

if (e === set.entryForXValue(e.x))
if e === set.entryForXValue(e.x, closestToY: e.y)
{
return set
}
Expand Down
Loading

0 comments on commit 32a5952

Please sign in to comment.