Skip to content

Commit

Permalink
addEntryOrdered to automatically place the entry in the correct index (
Browse files Browse the repository at this point in the history
…#173)

addEntry is unordered, and currently assumes that it is the last entry.
In the future when we will have charts with optionally unordered data, addEntry will be useful, while not addEntryOrdered is useful when adding entries in the middle of the dataset.
  • Loading branch information
danielgindi committed Jul 9, 2015
1 parent 484a37c commit a0f0dbe
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Charts/Classes/Data/ChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ public class ChartDataSet: NSObject
/// Returns the number of entries this DataSet holds.
public var valueCount: Int { return _yVals.count; }

/// Adds an entry to the end of the yVals array
/// :param: e the entry to add
public func addEntry(e: ChartDataEntry)
{
var val = e.value
Expand Down Expand Up @@ -311,6 +313,50 @@ public class ChartDataSet: NSObject
_yVals.append(e)
}

/// Adds an entry, and inserting it at the appropriate index in the yVals array, according to it's x-index.
/// :param: e the entry to add
public func addEntryOrdered(e: ChartDataEntry)
{
var val = e.value

if (_yVals == nil)
{
_yVals = [ChartDataEntry]()
}

if (_yVals.count == 0)
{
_yMax = val
_yMin = val
}
else
{
if (_yMax < val)
{
_yMax = val
}
if (_yMin > val)
{
_yMin = val
}
}

_yValueSum += val

if _yVals.last?.xIndex > e.xIndex
{
var closestIndex = entryIndex(xIndex: e.xIndex)
if _yVals[closestIndex].xIndex < e.xIndex
{
closestIndex++
}
_yVals.insert(e, atIndex: closestIndex)
return;
}

_yVals.append(e)
}

public func removeEntry(entry: ChartDataEntry) -> Bool
{
var removed = false
Expand Down

0 comments on commit a0f0dbe

Please sign in to comment.