Skip to content

Commit

Permalink
Bugfix: Highlights on xIndex that miss yValues were still highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Jun 11, 2015
1 parent afe191a commit 4e73990
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 41 deletions.
33 changes: 22 additions & 11 deletions Charts/Classes/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -387,31 +387,38 @@ public class ChartViewBase: UIView, ChartAnimatorDelegate
/// Highlights the value selected by touch gesture.
public func highlightValue(#highlight: ChartHighlight?, callDelegate: Bool)
{
if (highlight == nil)
var entry: ChartDataEntry?;
var h = highlight;

if (h == nil)
{
_indicesToHightlight.removeAll(keepCapacity: false);
}
else
{
// set the indices to highlight
_indicesToHightlight = [highlight!];
_indicesToHightlight = [h!];
entry = _data.getEntryForHighlight(h!);
if (entry === nil || entry!.xIndex != h?.xIndex)
{
h = nil;
entry = nil;
}
}

// redraw the chart
setNeedsDisplay();

if (callDelegate && delegate != nil)
{
if (highlight == nil)
if (h == nil)
{
delegate!.chartValueNothingSelected!(self);
}
else
{
var e = _data.getEntryForHighlight(highlight!);

// notify the listener
delegate!.chartValueSelected!(self, entry: e, dataSetIndex: highlight!.dataSetIndex, highlight: highlight!);
delegate!.chartValueSelected!(self, entry: entry!, dataSetIndex: h!.dataSetIndex, highlight: h!);
}
}
}
Expand All @@ -436,8 +443,12 @@ public class ChartViewBase: UIView, ChartAnimatorDelegate
if (xIndex <= Int(_deltaX) && xIndex <= Int(_deltaX * _animator.phaseX))
{
let e = _data.getEntryForHighlight(highlight);

var pos = getMarkerPosition(entry: e, dataSetIndex: dataSetIndex);
if (e === nil)
{
continue;
}

var pos = getMarkerPosition(entry: e!, dataSetIndex: dataSetIndex);

// check bounds
if (!_viewPortHandler.isInBounds(x: pos.x, y: pos.y))
Expand All @@ -446,7 +457,7 @@ public class ChartViewBase: UIView, ChartAnimatorDelegate
}

// callbacks to update the content
marker!.refreshContent(entry: e, dataSetIndex: dataSetIndex);
marker!.refreshContent(entry: e!, dataSetIndex: dataSetIndex);

let markerSize = marker!.size;
if (pos.y - markerSize.height <= 0.0)
Expand Down Expand Up @@ -709,10 +720,10 @@ public class ChartViewBase: UIView, ChartAnimatorDelegate
for (var i = 0, count = _data.dataSetCount; i < count; i++)
{
var set = _data.getDataSetByIndex(i);
var e = set!.entryForXIndex(xIndex);
var e = set.entryForXIndex(xIndex);
if (e !== nil)
{
vals.append(e);
vals.append(e!);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Charts/Classes/Data/ChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public class ChartData: NSObject
///
/// :param: highlight
/// :returns: the entry that is highlighted
public func getEntryForHighlight(highlight: ChartHighlight) -> ChartDataEntry
public func getEntryForHighlight(highlight: ChartHighlight) -> ChartDataEntry?
{
return _dataSets[highlight.dataSetIndex].entryForXIndex(highlight.xIndex);
}
Expand Down
4 changes: 2 additions & 2 deletions Charts/Classes/Data/ChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ public class ChartDataSet: NSObject
{
let e = self.entryForXIndex(x);

if (e !== nil) { return e.value }
if (e !== nil) { return e!.value }
else { return Double.NaN }
}

/// Returns the first Entry object found at the given xIndex with binary search.
/// If the no Entry at the specifed x-index is found, this method returns the Entry at the closest x-index.
/// Returns nil if no Entry object at that index.
public func entryForXIndex(x: Int) -> ChartDataEntry!
public func entryForXIndex(x: Int) -> ChartDataEntry?
{
var index = self.entryIndex(xIndex: x);
if (index > -1)
Expand Down
4 changes: 2 additions & 2 deletions Charts/Classes/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ public class BarChartRenderer: ChartDataRendererBase
if (index < barData.yValCount && index >= 0
&& CGFloat(index) < (CGFloat(delegate!.barChartRendererChartXMax(self)) * _animator.phaseX) / CGFloat(setCount))
{
var e = barData.getDataSetByIndex(dataSetIndex)!.entryForXIndex(index) as! BarChartDataEntry!;
var e = set.entryForXIndex(index) as! BarChartDataEntry!;

if (e === nil)
if (e === nil || e.xIndex != index)
{
continue;
}
Expand Down
20 changes: 12 additions & 8 deletions Charts/Classes/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public class BubbleChartRenderer: ChartDataRendererBase
var entryFrom = dataSet.entryForXIndex(_minX);
var entryTo = dataSet.entryForXIndex(_maxX);

var minx = max(dataSet.entryIndex(entry: entryFrom, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo, isEqual: true) + 1, entries.count);
var minx = max(dataSet.entryIndex(entry: entryFrom!, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo!, isEqual: true) + 1, entries.count);

_sizeBuffer[0].x = 0.0;
_sizeBuffer[0].y = 0.0;
Expand Down Expand Up @@ -170,8 +170,8 @@ public class BubbleChartRenderer: ChartDataRendererBase
var entryFrom = dataSet.entryForXIndex(_minX);
var entryTo = dataSet.entryForXIndex(_maxX);

var minx = max(dataSet.entryIndex(entry: entryFrom, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo, isEqual: true) + 1, entries.count);
var minx = max(dataSet.entryIndex(entry: entryFrom!, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo!, isEqual: true) + 1, entries.count);

let positions = delegate!.bubbleChartRenderer(self, transformerForAxis: dataSet.axisDependency).generateTransformedValuesBubble(entries, phaseX: phaseX, phaseY: phaseY, from: minx, to: maxx);

Expand Down Expand Up @@ -223,16 +223,20 @@ public class BubbleChartRenderer: ChartDataRendererBase

if (dataSet === nil || !dataSet.highlightEnabled)
{
continue
continue;
}

var entryFrom = dataSet.entryForXIndex(_minX);
var entryTo = dataSet.entryForXIndex(_maxX);

var minx = max(dataSet.entryIndex(entry: entryFrom, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo, isEqual: true) + 1, dataSet.entryCount);
var minx = max(dataSet.entryIndex(entry: entryFrom!, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo!, isEqual: true) + 1, dataSet.entryCount);

let entry = bubbleData.getEntryForHighlight(indice) as! BubbleChartDataEntry
let entry: BubbleChartDataEntry! = bubbleData.getEntryForHighlight(indice) as! BubbleChartDataEntry;
if (entry === nil || entry.xIndex != indice.xIndex)
{
continue;
}

let trans = delegate!.bubbleChartRenderer(self, transformerForAxis: dataSet.axisDependency);

Expand Down
10 changes: 5 additions & 5 deletions Charts/Classes/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public class CandleStickChartRenderer: ChartDataRendererBase
var entryFrom = dataSet.entryForXIndex(_minX);
var entryTo = dataSet.entryForXIndex(_maxX);

var minx = max(dataSet.entryIndex(entry: entryFrom, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo, isEqual: true) + 1, entries.count);
var minx = max(dataSet.entryIndex(entry: entryFrom!, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo!, isEqual: true) + 1, entries.count);

CGContextSaveGState(context);

Expand Down Expand Up @@ -197,8 +197,8 @@ public class CandleStickChartRenderer: ChartDataRendererBase
var entryFrom = dataSet.entryForXIndex(_minX);
var entryTo = dataSet.entryForXIndex(_maxX);

var minx = max(dataSet.entryIndex(entry: entryFrom, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo, isEqual: true) + 1, entries.count);
var minx = max(dataSet.entryIndex(entry: entryFrom!, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo!, isEqual: true) + 1, entries.count);

var positions = trans.generateTransformedValuesCandle(entries, phaseY: _animator.phaseY);

Expand Down Expand Up @@ -255,7 +255,7 @@ public class CandleStickChartRenderer: ChartDataRendererBase

var e = set.entryForXIndex(xIndex) as! CandleChartDataEntry!;

if (e === nil)
if (e === nil || e.xIndex != xIndex)
{
continue;
}
Expand Down
24 changes: 15 additions & 9 deletions Charts/Classes/Renderers/LineChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public class LineChartRenderer: ChartDataRendererBase
var entryFrom = dataSet.entryForXIndex(_minX);
var entryTo = dataSet.entryForXIndex(_maxX);

var minx = max(dataSet.entryIndex(entry: entryFrom, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo, isEqual: true) + 1, entries.count);
var minx = max(dataSet.entryIndex(entry: entryFrom!, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo!, isEqual: true) + 1, entries.count);

var phaseX = _animator.phaseX;
var phaseY = _animator.phaseY;
Expand Down Expand Up @@ -264,8 +264,8 @@ public class LineChartRenderer: ChartDataRendererBase
var entryFrom = dataSet.entryForXIndex(_minX);
var entryTo = dataSet.entryForXIndex(_maxX);

var minx = max(dataSet.entryIndex(entry: entryFrom, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo, isEqual: true) + 1, entries.count);
var minx = max(dataSet.entryIndex(entry: entryFrom!, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo!, isEqual: true) + 1, entries.count);

// more than 1 color
if (dataSet.colors.count > 1)
Expand Down Expand Up @@ -456,8 +456,8 @@ public class LineChartRenderer: ChartDataRendererBase
var entryFrom = dataSet.entryForXIndex(_minX);
var entryTo = dataSet.entryForXIndex(_maxX);

var minx = max(dataSet.entryIndex(entry: entryFrom, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo, isEqual: true) + 1, entries.count);
var minx = max(dataSet.entryIndex(entry: entryFrom!, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo!, isEqual: true) + 1, entries.count);

var positions = trans.generateTransformedValuesLine(
entries,
Expand Down Expand Up @@ -525,8 +525,8 @@ public class LineChartRenderer: ChartDataRendererBase
var circleHoleRadius = circleHoleDiameter / 2.0;
var isDrawCircleHoleEnabled = dataSet.isDrawCircleHoleEnabled;

var entryFrom = dataSet.entryForXIndex(_minX);
var entryTo = dataSet.entryForXIndex(_maxX);
var entryFrom = dataSet.entryForXIndex(_minX)!;
var entryTo = dataSet.entryForXIndex(_maxX)!;

var minx = max(dataSet.entryIndex(entry: entryFrom, isEqual: true), 0);
var maxx = min(dataSet.entryIndex(entry: entryTo, isEqual: true) + 1, entries.count);
Expand Down Expand Up @@ -611,7 +611,13 @@ public class LineChartRenderer: ChartDataRendererBase
continue;
}

var y = CGFloat(set.yValForXIndex(xIndex)) * _animator.phaseY; // get the y-position
let yValue = set.yValForXIndex(xIndex);
if (isnan(yValue))
{
continue;
}

var y = CGFloat(yValue) * _animator.phaseY; // get the y-position

_highlightPtsBuffer[0] = CGPoint(x: CGFloat(xIndex), y: CGFloat(chartYMax));
_highlightPtsBuffer[1] = CGPoint(x: CGFloat(xIndex), y: CGFloat(chartYMin));
Expand Down
9 changes: 7 additions & 2 deletions Charts/Classes/Renderers/RadarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,13 @@ public class RadarChartRenderer: ChartDataRendererBase
var xIndex = indices[i].xIndex;

var e = set.entryForXIndex(xIndex);
var j = set.entryIndex(entry: e, isEqual: true);
var y = (e.value - _chart.chartYMin);
if (e === nil || e!.xIndex != xIndex)
{
continue;
}

var j = set.entryIndex(entry: e!, isEqual: true);
var y = (e!.value - _chart.chartYMin);

var p = ChartUtils.getPosition(center: center, dist: CGFloat(y) * factor,
angle: sliceangle * CGFloat(j) + _chart.rotationAngle);
Expand Down
8 changes: 7 additions & 1 deletion Charts/Classes/Renderers/ScatterChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,13 @@ public class ScatterChartRenderer: ChartDataRendererBase
continue;
}

var y = CGFloat(set.yValForXIndex(xIndex)) * _animator.phaseY; // get the y-position
let yValue = set.yValForXIndex(xIndex);
if (isnan(yValue))
{
continue;
}

var y = CGFloat(yValue) * _animator.phaseY; // get the y-position

pts[0] = CGPoint(x: CGFloat(xIndex), y: CGFloat(chartYMax));
pts[1] = CGPoint(x: CGFloat(xIndex), y: CGFloat(chartYMin));
Expand Down

0 comments on commit 4e73990

Please sign in to comment.