Skip to content

Commit

Permalink
algorithm updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jjatie committed Oct 30, 2020
1 parent 9b24868 commit 2e95133
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 132 deletions.
7 changes: 2 additions & 5 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,9 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
// calculate how many digits are needed
setupDefaultFormatter(min: data.yMin, max: data.yMax)

for set in data
for set in data where set.valueFormatter is DefaultValueFormatter
{
if set.valueFormatter is DefaultValueFormatter
{
set.valueFormatter = defaultValueFormatter
}
set.valueFormatter = defaultValueFormatter
}

// let the chart know there is new data
Expand Down
25 changes: 12 additions & 13 deletions Source/Charts/Components/AxisBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
import Foundation
import CoreGraphics

extension Sequence {
func max<T: Comparable>(by keyPath: KeyPath<Element, T>) -> Element? {
self.max { $0[keyPath: keyPath] < $1[keyPath: keyPath] }
}
}

/// Base class for all axes
@objc(ChartAxisBase)
open class AxisBase: ComponentBase
Expand Down Expand Up @@ -133,19 +139,12 @@ open class AxisBase: ComponentBase

@objc open func getLongestLabel() -> String
{
var longest = ""

for i in entries.indices
{
let text = getFormattedLabel(i)

if longest.count < text.count
{
longest = text
}
}

return longest
let longest = entries.indices
.lazy
.map(getFormattedLabel(_:))
.max(by: \.count)

return longest ?? ""
}

/// - Returns: The formatted label at the specified index. This will either use the auto-formatter or the custom formatter (if one is set).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ open class BarChartData: BarLineScatterCandleBubbleChartData
{
fromX += diff
}

}

notifyDataChanged()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ open class BarChartDataEntry: ChartDataEntry
return _positiveSum
}

@objc public var stackSize: Int { yValues?.count ?? 1}

@objc open func calcPosNegSum()
{
(_negativeSum, _positiveSum) = _yVals?.reduce(into: (0,0)) { (result, y) in
Expand Down Expand Up @@ -207,16 +209,6 @@ open class BarChartDataEntry: ChartDataEntry
/// - Returns:
private static func calcSum(values: [Double]?) -> Double
{
guard let values = values
else { return 0.0 }

var sum = 0.0

for f in values
{
sum += f
}

return sum
values?.reduce(into: 0, +=) ?? 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,17 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData
/// stacks. All values belonging to a stack are calculated separately.
private func calcEntryCountIncludingStacks(entries: [BarChartDataEntry])
{
_entryCountStacks = 0

entries.forEach { _entryCountStacks += $0.yValues?.count ?? 1 }
_entryCountStacks = entries.lazy
.map(\.stackSize)
.reduce(into: 0, +=)
}

/// calculates the maximum stacksize that occurs in the Entries array of this DataSet
private func calcStackSize(entries: [BarChartDataEntry])
{
for e in entries where (e.yValues?.count ?? 0) > _stackSize
{
_stackSize = e.yValues!.count
}
_stackSize = entries.lazy
.map(\.stackSize)
.max(by: \.self) ?? 1
}

open override func calcMinMax(entry e: ChartDataEntry)
Expand Down
1 change: 1 addition & 0 deletions Source/Charts/Highlight/BarHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ open class BarHighlighter: ChartHighlighter
@objc open func getClosestStackIndex(ranges: [Range]?, value: Double) -> Int
{
guard let ranges = ranges else { return 0 }

if let stackIndex = ranges.firstIndex(where: { $0.contains(value) }) {
return stackIndex
} else {
Expand Down
18 changes: 5 additions & 13 deletions Source/Charts/Highlight/RadarHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,12 @@ open class RadarHighlighter: PieRadarHighlighter
let highlights = getHighlights(forIndex: index)

let distanceToCenter = Double(chart.distanceToCenter(x: x, y: y) / chart.factor)

var closest: Highlight?
var distance = Double.greatestFiniteMagnitude

for high in highlights
{
let cdistance = abs(high.y - distanceToCenter)
if cdistance < distance
{
closest = high
distance = cdistance
}

func closestToCenter(lhs: Highlight, rhs: Highlight) -> Bool {
abs(lhs.y - distanceToCenter) < abs(rhs.y - distanceToCenter)
}


let closest = highlights.min(by: closestToCenter(lhs:rhs:))
return closest
}

Expand Down
33 changes: 17 additions & 16 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,13 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
// Create and append the corresponding accessibility element to accessibilityOrderedElements
if let chart = dataProvider as? BarChartView
{
let element = createAccessibleElement(withIndex: j,
container: chart,
dataSet: dataSet,
dataSetIndex: index,
stackSize: stackSize)
{ (element) in
let element = createAccessibleElement(
withIndex: j,
container: chart,
dataSet: dataSet,
dataSetIndex: index,
stackSize: stackSize
) { (element) in
element.accessibilityFrame = barRect
}

Expand Down Expand Up @@ -536,8 +537,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
// if we have stacks

var bufferIndex = 0

for index in 0 ..< Int(ceil(Double(dataSet.entryCount) * animator.phaseX))
let lastIndex = ceil(Double(dataSet.entryCount) * animator.phaseX)

for index in 0 ..< Int(lastIndex)
{
guard let e = dataSet.entryForIndex(index) as? BarChartDataEntry else { continue }

Expand All @@ -548,19 +550,18 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
let x = rect.origin.x + rect.size.width / 2.0

// we still draw stacked bars, but there is one non-stacked in between
if let vals = vals
if let values = vals
{
// draw stack values
var transformed = [CGPoint]()

var posY = 0.0
var negY = -e.negativeSum

for k in vals.indices
for value in values
{
let value = vals[k]
var y: Double

let y: Double

if value == 0.0 && (posY == 0.0 || negY == 0.0)
{
// Take care of the situation of a 0.0 value, which overlaps a non-zero bar
Expand All @@ -582,9 +583,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

trans.pointValuesToPixel(&transformed)

for (val, transformed) in zip(vals, transformed)
for (value, transformed) in zip(values, transformed)
{
let drawBelow = (val == 0.0 && negY == 0.0 && posY > 0.0) || val < 0.0
let drawBelow = (value == 0.0 && negY == 0.0 && posY > 0.0) || value < 0.0
let y = transformed.y + (drawBelow ? negOffset : posOffset)

guard viewPortHandler.isInBoundsRight(x) else { break }
Expand All @@ -597,7 +598,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
drawValue(
context: context,
value: formatter.stringForValue(
val,
value,
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler),
Expand Down
3 changes: 2 additions & 1 deletion Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
accessibleChartElements.append(element)
}

for case let (i, set) as (Int, BubbleChartDataSetProtocol) in bubbleData.enumerated() where set.isVisible
let sets = bubbleData.dataSets as! [BubbleChartDataSet]
for case let (i, set) in zip(sets.indices, sets) where set.isVisible
{
drawDataSet(context: context, dataSet: set, dataSetIndex: i)
}
Expand Down
18 changes: 5 additions & 13 deletions Source/Charts/Renderers/CombinedChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,30 @@ open class CombinedChartRenderer: NSObject, DataRenderer
{
_renderers.append(BarChartRenderer(dataProvider: chart, animator: animator, viewPortHandler: viewPortHandler))
}
break


case .line:
if chart.lineData !== nil
{
_renderers.append(LineChartRenderer(dataProvider: chart, animator: animator, viewPortHandler: viewPortHandler))
}
break


case .candle:
if chart.candleData !== nil
{
_renderers.append(CandleStickChartRenderer(dataProvider: chart, animator: animator, viewPortHandler: viewPortHandler))
}
break


case .scatter:
if chart.scatterData !== nil
{
_renderers.append(ScatterChartRenderer(dataProvider: chart, animator: animator, viewPortHandler: viewPortHandler))
}
break


case .bubble:
if chart.bubbleData !== nil
{
_renderers.append(BubbleChartRenderer(dataProvider: chart, animator: animator, viewPortHandler: viewPortHandler))
}
break
}
}

Expand Down Expand Up @@ -190,10 +185,7 @@ open class CombinedChartRenderer: NSObject, DataRenderer
/// e.g. if you provide [DrawOrder.Bar, DrawOrder.Line], the bars will be drawn behind the lines.
open var drawOrder: [CombinedChartView.DrawOrder]
{
get
{
return _drawOrder
}
get { _drawOrder }
set
{
if !newValue.isEmpty
Expand Down
Loading

0 comments on commit 2e95133

Please sign in to comment.