Skip to content

Commit

Permalink
Merge pull request #3164 from jjatie/4.0/axis-renderer-cleanup
Browse files Browse the repository at this point in the history
Axis Renderers Cleanup
  • Loading branch information
liuxuan30 authored Jan 4, 2019
2 parents be8b386 + 52480db commit 5689a1c
Show file tree
Hide file tree
Showing 11 changed files with 488 additions and 738 deletions.
9 changes: 4 additions & 5 deletions Source/Charts/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1692,12 +1692,11 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
/// - returns: The DataSet object displayed at the touched position of the chart
@objc open func getDataSetByTouchPoint(point pt: CGPoint) -> BarLineScatterCandleBubbleChartDataSetProtocol?
{
let h = getHighlightByTouchPoint(pt)
if let h = h
{
return data?[h.dataSetIndex] as? BarLineScatterCandleBubbleChartDataSetProtocol
guard let h = getHighlightByTouchPoint(pt) else {
return nil
}
return nil

return data?[h.dataSetIndex] as? BarLineScatterCandleBubbleChartDataSetProtocol
}

/// - returns: The current x-scale factor
Expand Down
92 changes: 56 additions & 36 deletions Source/Charts/Data/Implementations/Standard/ChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral
var leftAxisMin = Double.greatestFiniteMagnitude
var rightAxisMax = -Double.greatestFiniteMagnitude
var rightAxisMin = Double.greatestFiniteMagnitude

// MARK: - Accessibility

/// When the data entry labels are generated identifiers, set this property to prepend a string before each identifier
Expand Down Expand Up @@ -91,6 +91,50 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral
xMin = .greatestFiniteMagnitude

forEach { calcMinMax(dataSet: $0) }

// left axis
let firstLeft = getFirstLeft(dataSets: dataSets)

if firstLeft !== nil
{
leftAxisMax = firstLeft!.yMax
leftAxisMin = firstLeft!.yMin

for dataSet in _dataSets where dataSet.axisDependency == .left
{
if dataSet.yMin < leftAxisMin
{
leftAxisMin = dataSet.yMin
}

if dataSet.yMax > leftAxisMax
{
leftAxisMax = dataSet.yMax
}
}
}

// right axis
let firstRight = getFirstRight(dataSets: dataSets)

if firstRight !== nil
{
rightAxisMax = firstRight!.yMax
rightAxisMin = firstRight!.yMin

for dataSet in _dataSets where dataSet.axisDependency == .right
{
if dataSet.yMin < rightAxisMin
{
rightAxisMin = dataSet.yMin
}

if dataSet.yMax > rightAxisMax
{
rightAxisMax = dataSet.yMax
}
}
}
}

/// Adjusts the current minimum and maximum values based on the provided Entry object.
Expand Down Expand Up @@ -252,7 +296,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral
guard indices.contains(dataSetIndex) else {
return print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n")
}

let set = self[dataSetIndex]
if !set.addEntry(e) { return }
calcMinMax(entry: e, axis: set.axisDependency)
Expand Down Expand Up @@ -345,7 +389,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral
/// If set to true, this means that values can be highlighted programmatically or by touch gesture.
@objc open var isHighlightEnabled: Bool
{
get { return first { $0.highlightEnabled == false } == nil }
get { return allSatisfy { $0.isHighlightEnabled } }
set { forEach { $0.highlightEnabled = newValue } }
}

Expand Down Expand Up @@ -434,10 +478,7 @@ extension ChartData//: RangeReplaceableCollection

public func removeFirst() -> Element
{
guard !(self is CombinedChartData) else
{
fatalError("removeFirst() not supported for CombinedData")
}
assert(!(self is CombinedChartData), "\(#function) not supported for CombinedData")

let element = _dataSets.removeFirst()
notifyDataChanged()
Expand All @@ -446,21 +487,15 @@ extension ChartData//: RangeReplaceableCollection

public func removeFirst(_ n: Int)
{
guard !(self is CombinedChartData) else
{
fatalError("removeFirst(_:) not supported for CombinedData")
}
assert(!(self is CombinedChartData), "\(#function) not supported for CombinedData")

_dataSets.removeFirst(n)
notifyDataChanged()
}

public func removeLast() -> Element
{
guard !(self is CombinedChartData) else
{
fatalError("removeLast() not supported for CombinedData")
}
assert(!(self is CombinedChartData), "\(#function) not supported for CombinedData")

let element = _dataSets.removeLast()
notifyDataChanged()
Expand All @@ -469,43 +504,31 @@ extension ChartData//: RangeReplaceableCollection

public func removeLast(_ n: Int)
{
guard !(self is CombinedChartData) else
{
fatalError("removeLast(_:) not supported for CombinedData")
}
assert(!(self is CombinedChartData), "\(#function) not supported for CombinedData")

_dataSets.removeLast(n)
notifyDataChanged()
}

public func removeSubrange<R>(_ bounds: R) where R : RangeExpression, Index == R.Bound
{
guard !(self is CombinedChartData) else
{
fatalError("removeSubrange<R>(_:) not supported for CombinedData")
}
assert(!(self is CombinedChartData), "\(#function) not supported for CombinedData")

_dataSets.removeSubrange(bounds)
notifyDataChanged()
}

public func removeAll(keepingCapacity keepCapacity: Bool)
{
guard !(self is CombinedChartData) else
{
fatalError("removeAll(keepingCapacity:) not supported for CombinedData")
}
assert(!(self is CombinedChartData), "\(#function) not supported for CombinedData")

_dataSets.removeAll(keepingCapacity: keepCapacity)
notifyDataChanged()
}

public func replaceSubrange<C>(_ subrange: Swift.Range<Index>, with newElements: C) where C : Collection, Element == C.Element
{
guard !(self is CombinedChartData) else
{
fatalError("replaceSubrange<C>(_:) not supported for CombinedData")
}
assert(!(self is CombinedChartData), "\(#function) not supported for CombinedData")

_dataSets.replaceSubrange(subrange, with: newElements)
newElements.forEach { self.calcMinMax(dataSet: $0) }
Expand Down Expand Up @@ -534,13 +557,10 @@ extension ChartData
guard let index = index(forLabel: label, ignoreCase: ignoreCase) else { return nil }
return self[index]
}

public subscript(entry entry: ChartDataEntry) -> Element?
{
guard !(self is CombinedChartData) else
{
fatalError("subscript(entry:) not supported for CombinedData")
}
assert(!(self is CombinedChartData), "\(#function) not supported for CombinedData")

guard let index = index(where: { $0.entryForXValue(entry.x, closestToY: entry.y) === entry }) else { return nil }
return self[index]
Expand Down
25 changes: 17 additions & 8 deletions Source/Charts/Data/Implementations/Standard/ChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ open class ChartDataSet: ChartBaseDataSet
@objc public init(values: [ChartDataEntry], label: String)
{
self.values = values

super.init(label: label)

self.calcMinMax()
Expand Down Expand Up @@ -152,7 +151,7 @@ open class ChartDataSet: ChartBaseDataSet
/// if `i` is out of bounds, it may throw an out-of-bounds exception
open override func entryForIndex(_ i: Int) -> ChartDataEntry?
{
guard i >= values.startIndex, i < values.endIndex else {
guard values.indices.contains(i) else {
return nil
}
return values[i]
Expand Down Expand Up @@ -410,9 +409,9 @@ open class ChartDataSet: ChartBaseDataSet
// TODO: This should return the removed entry to follow Swift convention.
open override func removeEntry(_ entry: ChartDataEntry) -> Bool
{
isIndirectValuesCall = true

guard let i = values.index(where: { $0 === entry }) else { return false }

isIndirectValuesCall = true
values.remove(at: i)

notifyDataSetChanged()
Expand All @@ -425,8 +424,13 @@ open class ChartDataSet: ChartBaseDataSet
// TODO: This should return the removed entry to follow Swift convention.
open override func removeFirst() -> Bool
{
let entry: ChartDataEntry? = values.isEmpty ? nil : values.removeFirst()
return entry != nil
guard !values.isEmpty else { return false }

isIndirectValuesCall = true
values.removeFirst()

notifyDataSetChanged()
return true
}

/// Removes the last Entry (at index size-1) of this DataSet from the entries array.
Expand All @@ -435,8 +439,13 @@ open class ChartDataSet: ChartBaseDataSet
// TODO: This should return the removed entry to follow Swift convention.
open override func removeLast() -> Bool
{
let entry: ChartDataEntry? = values.isEmpty ? nil : values.removeLast()
return entry != nil
guard !values.isEmpty else { return false }

isIndirectValuesCall = true
values.removeLast()

notifyDataSetChanged()
return true
}

/// Checks if this DataSet contains the specified Entry.
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Highlight/ChartHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ open class ChartHighlighter : NSObject, Highlighter
guard let data = self.data else { return vals }

for (i, set) in zip(data.indices, data) where set.isHighlightEnabled
{
{
// extract all y-values from all DataSets at the given x-value.
// some datasets (i.e bubble charts) make sense to have multiple values for an x-value. We'll have to find a way to handle that later on. It's more complicated now when x-indices are floating point.
vals.append(contentsOf: buildHighlights(dataSet: set, dataSetIndex: i, xValue: xValue, rounding: .closest))
Expand Down
8 changes: 4 additions & 4 deletions Source/Charts/Renderers/LineChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,8 @@ open class LineChartRenderer: LineRadarRenderer
for i in 0 ..< dataSets.count
{
guard let dataSet = lineData[i] as? LineChartDataSetProtocol else { continue }
if !dataSet.isVisible || dataSet.entryCount == 0

if !dataSet.isVisible || !dataSet.isDrawCirclesEnabled || dataSet.entryCount == 0
{
continue
}
Expand Down Expand Up @@ -772,8 +772,8 @@ open class LineChartRenderer: LineRadarRenderer

for high in indices
{
guard let set = lineData[high.dataSetIndex] as? LineChartDataSetProtocol
, set.isHighlightEnabled
guard let set = lineData[high.dataSetIndex] as? LineChartDataSetProtocol,
set.isHighlightEnabled
else { continue }

guard let e = set.entryForXValue(high.x, closestToY: high.y) else { continue }
Expand Down
Loading

0 comments on commit 5689a1c

Please sign in to comment.